我是非常新的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;我在我的代码中有这个,并首先将其剥离; –