【发布时间】:2019-03-14 23:47:27
【问题描述】:
我在使用 Java 中的 Google Cloud Storage API 时遇到了问题。以下所有情况都发生在 GCP 实例上——所以这一切都在 Google 的网络中。基本上,对于下载大型 zip 文件,gsutil 非常快,但执行类似任务的 Java 代码非常慢,可能慢了 10 倍。作为参考,这将是等效的 gsutil 命令。
gsutil cp gs://mybucket/myfile.zip .
非常基本,没有疯狂的选择。但是,应该或多或少做同样事情的 Java 代码要慢得多:
FileOutputStream fos = new FileOutputStream("myfile.zip");
Storage.Objects.Get get = storageService.objects().get("mybucket", "myfile.zip");
get.setDisableGZipContent(true); //Seems to have no effect
MediaHttpDownloader downloader = get.getMediaHttpDownloader();
downloader.setDirectDownloadEnabled(true); //Seems to have no effect
get.executeMediaAndDownloadTo(fos);
我不明白为什么这非常慢。作为一个愚蠢但悲伤的测试来解决这个问题,我在另一个终端窗口中执行了gsutil,而 Java 代码仍在运行,gsutil 在 Java 代码之前几秒钟下载了相同的文件。只是慢得令人尴尬。这些是我从 Google 使用的库:
<dependency>
<groupId>com.google.api-client</groupId>
<artifactId>google-api-client</artifactId>
<version>1.21.0</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>com.google.apis</groupId>
<artifactId>google-api-services-storage</artifactId>
<version>v1-rev66-1.21.0</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>com.google.http-client</groupId>
<artifactId>google-http-client</artifactId>
<version>1.21.0</version>
<scope>compile</scope>
</dependency>
我已尝试更改 setDirectDownloadEnabled() 和 setDisableGZipContent() 的选项(因为我们正在下载一个 zip,它已经被压缩) - 没有任何明显的效果。
【问题讨论】:
-
你能量化“极慢”吗?下载一个 100 兆字节的对象需要多少秒? gsutil 需要多长时间?
-
我见过 500MB 的速度慢到 70 秒。 gsutil 大约 2-3 完成。