我是非常新的GC平臺,我試圖用兩種方法創建一個Java API:一個返回特定存儲桶中所有文件的列表,另一個返回檢索來自該存儲桶的指定文件。目標是能夠迭代文件列表,以便從存儲桶中下載每個文件。實質上,我想要在Android設備上鏡像存儲桶的內容,因此API將從Android應用程序中生成的客戶端庫中調用。 我的getFileList()
方法返回一個ListResult
對象。如何從中提取文件列表?Google雲端點存儲桶下載器
@ApiMethod(name = "getFileList", path = "getFileList", httpMethod = ApiMethod.HttpMethod.GET)
public ListResult getFileList(@Named("bucketName") String bucketName) {
GcsService gcsService = GcsServiceFactory.createGcsService(RetryParams.getDefaultInstance());
ListResult result = null;
try {
result = gcsService.list(bucketName, ListOptions.DEFAULT);
return result;
} catch (IOException e) {
return null; // Handle this properly.
}
}
另外,我正在努力確定我的API方法的返回類型應該是什麼。我不能使用一個字節數組,因爲返回類型不能是簡單的類型,據我所知。這是我與它其中:
@ApiMethod(name = "getFile", path = "getFile", httpMethod = ApiMethod.HttpMethod.GET)
public byte[] getFile(@Named("bucketName") String bucketName, ListItem file) {
GcsService gcsService = GcsServiceFactory.createGcsService();
GcsFilename gcsFilename = new GcsFilename(bucketName, file.getName());
ByteBuffer byteBuffer;
try {
int fileSize = (int) gcsService.getMetadata(gcsFilename).getLength();
byteBuffer = ByteBuffer.allocate(fileSize);
GcsInputChannel gcsInputChannel = gcsService.openReadChannel(gcsFilename, 0);
gcsInputChannel.read(byteBuffer);
return byteBuffer.array();
} catch (IOException e) {
return null; // Handle this properly.
}
}
我的谷歌文檔,這個東西在丟失,很擔心,我在這,因爲所有我想要做的是安全地下載一個來從完全錯誤的方向一堆文件!
太好了。非常感謝!我會讓你知道我是怎麼得到的...... – user3352488
只是對'getStorage()'方法做了一個小改動,用於lazy init;我在我的代碼中有這個,並首先將其剝離; –