问题描述
我正在开发一个应用程序,为用户提供一个界面,可以从我们的Google云端存储中下载文件。我编写了单元测试,我可以连接到存储并下载文件。
现在我(几乎)完成了我的界面,我想测试整个应用程序。但现在我注意到我并没有真正下载该文件,我下载了一个文件,其中包含有关我要下载的文件的META数据。类似于:
{
kind:storage#object,
id: xxxxxxxxxxx / Homer.png / xxxxxxxxxxxx,
selfLink:https://www.googleapis.com/storage/xxxxxxxxxxxxxxxx/Homer.png,
name:Homer.png ,
bucket:xxxxxxxxxxxxxxx,
generation:xxxxxxxxxxxxxxx,
metageneration:1,
contentType:image / png ,
更新:2014-07-17T08:37:28.026Z,
storageClass:STANDARD,
size:xxxxx,
md5Hash:xxxxxxxxxxxxxxxxxxxxx,
mediaLink:https://www.googleapis.com/download/storage/xxxxxxxxxxxxxxx/o/Homer.png?generation=xxxxxxxxxxxxxxxxx&alt=media,
所有者:{
entity:user-xxxxxxxxxxxxxxxxxxxxxxxxxxxx,
entityId:xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
},
crc32c:xxxxxxxxxx ,
etag:xxxxxxxxxxxxx
}
我想知道我做错了,这是我用来下载文件的代码:
public byte [] getFileAsByteArray(String bucketName,String fileName)
throws GoogleAppManagerException {
Storage storage = null;
try {
storage = getStorage();
} catch(GeneralSecurityException e){
抛出新的GoogleAppManagerException(SECURITY_EXCEPTION + e.getStackTrace(),e);
} catch(IOException e){
抛出新的GoogleAppManagerException(IO_EXCEPTION + e.getStackTrace(),e);
}
Get get = null;
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
try {
get = storage.objects()。get(bucketName,fileName);
get.executeAndDownloadTo(outputStream);
} catch(IOException e1){
抛出新的GoogleAppManagerException(IO_EXCEPTION + e1.getStackTrace(),e1);
}
返回outputStream.toByteArray();
}
如你所说,目前你'下载元数据。您需要使用媒体下载来下载对象的数据。
在这里查看示例Java代码:
getMediaHttpDownloader()。
有关媒体和其他方法的更多信息,可恢复下载,此处:
I'm developping an application that provides users with an interface where they can download files from our Google Cloud Storage. I wrote unit tests and I could connect to the storage and a file was downloaded.
Now that I'm (almost) finished with my interface, I wanted to test the whole application. But now I notice I don't really download the file, I download a file with META data about the file I want to download. Something like:
{
"kind": "storage#object",
"id": "xxxxxxxxxxx/Homer.png/xxxxxxxxxxxx",
"selfLink": "https://www.googleapis.com/storage/xxxxxxxxxxxxxxxx/Homer.png",
"name": "Homer.png",
"bucket": "xxxxxxxxxxxxxxx",
"generation": "xxxxxxxxxxxxxxx",
"metageneration": "1",
"contentType": "image/png",
"updated": "2014-07-17T08:37:28.026Z",
"storageClass": "STANDARD",
"size": "xxxxx",
"md5Hash": "xxxxxxxxxxxxxxxxxxxxx",
"mediaLink": "https://www.googleapis.com/download/storage/xxxxxxxxxxxxxxx/o/Homer.png?generation=xxxxxxxxxxxxxxxxx&alt=media",
"owner": {
"entity": "user-xxxxxxxxxxxxxxxxxxxxxxxxxxxx",
"entityId": "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"
},
"crc32c": "xxxxxxxxxx",
"etag": "xxxxxxxxxxxxx"
}
I'm wondering what I'm doing wrong, this is the code I'm using to download the file:
public byte[] getFileAsByteArray(String bucketName, String fileName)
throws GoogleAppManagerException {
Storage storage = null;
try {
storage = getStorage();
} catch (GeneralSecurityException e) {
throw new GoogleAppManagerException(SECURITY_EXCEPTION + e.getStackTrace(), e);
} catch (IOException e) {
throw new GoogleAppManagerException(IO_EXCEPTION + e.getStackTrace(), e);
}
Get get = null;
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
try {
get = storage.objects().get(bucketName, fileName);
get.executeAndDownloadTo(outputStream);
} catch (IOException e1) {
throw new GoogleAppManagerException(IO_EXCEPTION + e1.getStackTrace(), e1);
}
return outputStream.toByteArray();
}
As you say, currently you're downloading the metadata. You need to use media download to download the object's data.
Take a look at the sample Java code here:https://developers.google.com/storage/docs/json_api/v1/objects/get
That is the simplest method, using getMediaHttpDownloader()
.
There is more info on media and the other method, resumable download, here:https://code.google.com/p/google-api-java-client/wiki/MediaDownload
这篇关于如何使用Java从Google云端存储下载文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!