使用 S3 Java SDK 上传到 GCS:`您在 Content-MD5 或 x-goog-hash 中指定的 MD5 与我们计算的不匹配`

Uploading to GCS using S3 Java SDK: `The MD5 you specified in Content-MD5 or x-goog-hash did not match what we computed`

我正在尝试使用适用于 Java 1.x 的 AWS SDK 上传到 Google 云存储。我启用了互操作性模式并将 AWS_ACCESS_KEY_IDAWS_SECRET_ACCESS_KEY 放入环境中。我可以列出对象,但上传对我不起作用。

我使用的代码是

    AmazonS3 client =
        AmazonS3ClientBuilder.standard()
            .withEndpointConfiguration(
                new AwsClientBuilder.EndpointConfiguration(
                    "http://storage.googleapis.com", "auto"))
            .build();

    TransferManager tm = TransferManagerBuilder.standard()
        .withS3Client(client)
        .build();
    Upload upload = tm.upload(bucketName, key, new File(filename));
    upload.waitForCompletion();

但是我得到的结果是:

com.amazonaws.services.s3.model.AmazonS3Exception: The MD5 you specified in Content-MD5 or x-goog-hash did not match what we computed. (Service: Amazon S3; Status Code: 400; Error Code: BadDigest; Request ID: null; S3 Extended Request ID: null; Proxy: null), S3 Extended Request ID: null
        at com.amazonaws.http.AmazonHttpClient$RequestExecutor.handleErrorResponse(AmazonHttpClient.java:1819)
        at com.amazonaws.http.AmazonHttpClient$RequestExecutor.handleServiceErrorResponse(AmazonHttpClient.java:1403)
        at com.amazonaws.http.AmazonHttpClient$RequestExecutor.executeOneRequest(AmazonHttpClient.java:1372)
        at com.amazonaws.http.AmazonHttpClient$RequestExecutor.executeHelper(AmazonHttpClient.java:1145)
        at com.amazonaws.http.AmazonHttpClient$RequestExecutor.doExecute(AmazonHttpClient.java:802)
        at com.amazonaws.http.AmazonHttpClient$RequestExecutor.executeWithTimer(AmazonHttpClient.java:770)
        at com.amazonaws.http.AmazonHttpClient$RequestExecutor.execute(AmazonHttpClient.java:744)
        at com.amazonaws.http.AmazonHttpClient$RequestExecutor.access0(AmazonHttpClient.java:704)
        at com.amazonaws.http.AmazonHttpClient$RequestExecutionBuilderImpl.execute(AmazonHttpClient.java:686)
        at com.amazonaws.http.AmazonHttpClient.execute(AmazonHttpClient.java:550)
        at com.amazonaws.http.AmazonHttpClient.execute(AmazonHttpClient.java:530)
        at com.amazonaws.services.s3.AmazonS3Client.invoke(AmazonS3Client.java:5437)
        at com.amazonaws.services.s3.AmazonS3Client.invoke(AmazonS3Client.java:5384)

相同的代码适用于 S3(减去 EndpointConfiguration)

GCS 文档说:

In the Cloud Storage XML API, chunked transfer encoding and V4 signatures cannot currently be used simultaneously. Some Amazon S3 tools use chunked transfer encoding along with signatures by default; you should disable chunked transfer encoding in such cases.

我找到了两种解决此错误的方法

  1. 在构建客户端时可以.withChunkedEncodingDisabled(true)

     AmazonS3 client =
         AmazonS3ClientBuilder.standard()
             .withChunkedEncodingDisabled(true)  // <<<<<<<
             .withEndpointConfiguration(
                 new AwsClientBuilder.EndpointConfiguration(
                     "http://storage.googleapis.com", "auto"))
             .build();
    
  2. 您可以切换到 https 端点:

     AmazonS3 client =
         AmazonS3ClientBuilder.standard()
             .withEndpointConfiguration(
                 new AwsClientBuilder.EndpointConfiguration(
                     "https://storage.googleapis.com", "auto"))
             .build();