如何使用 Java 从 Google Cloud Storage 下载文件?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/25141998/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-02 07:26:35  来源:igfitidea点击:

How to download a file from Google Cloud Storage with Java?

javagoogle-cloud-storage

提问by Tom Jonckheere

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.

我正在开发一个应用程序,为用户提供一个界面,他们可以从我们的 Google Cloud Storage 下载文件。我编写了单元测试,我可以连接到存储并下载了一个文件。

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:

现在我(几乎)完成了我的界面,我想测试整个应用程序。但是现在我注意到我并没有真正下载文件,而是下载了一个包含有关我要下载的文件的 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",
 "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();
    }

采纳答案by jterrace

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

在此处查看示例 Java 代码:https: //developers.google.com/storage/docs/json_api/v1/objects/get

That is the simplest method, using getMediaHttpDownloader().

这是最简单的方法,使用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

关于媒体和另一种方法的更多信息,可恢复下载,在这里:https: //code.google.com/p/google-api-java-client/wiki/MediaDownload

回答by Anand Mohan

With the latest libraries:

使用最新的

Storage storage = StorageOptions.newBuilder()
            .setProjectId(projectId)
            .setCredentials(GoogleCredentials.fromStream(new FileInputStream(serviceAccountJSON)))
            .build()
            .getService();
Blob blob = storage.get(BUCKET_URL, RELATIVE_OBJECT_LOCATION);
ReadChannel readChannel = blob.reader();
FileOutputStream fileOuputStream = new FileOutputStream(outputFileName);
fileOuputStream.getChannel().transferFrom(readChannel, 0, Long.MAX_VALUE);
fileOuputStream.close();