从谷歌云存储Java API下载比gsutil慢

时间:2016-05-23 22:03:27

标签: java google-cloud-storage

我在Java中使用Google云端存储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()的选项(因为我们正在下载拉链,它已经被压缩了) - 两者都没有任何明显的效果

1 个答案:

答案 0 :(得分:1)

gsutil加快速度有两个主要原因:

  1. MediaHttpDownloader一次下载大量字节,默认情况下为32MB。因此,在每32MB之间,当客户端等待服务响应时,需要额外的往返。

  2. 默认情况下,gsutil将对象拆分为multiple slices并并行下载每个切片。这会绕过任何单流带宽或连接限制。

  3. 由于您的代码是GCP内部的,因此网络应该不是一个因素(但它仍然是一个因素)。我不认为这可以解释10倍的降级,因此澄清您所看到的确切转移率会很有用。

相关问题