使用适用于 .NET 的 AWS SDK 上传到 Google 云存储`预期哈希值不等于计算哈希值`

Uploading to Google Cloud Storage using AWS SDK for .NET `Expected hash not equal to calculated hash`

我正在尝试使用适用于 .NET 的 AWS 开发工具包上传到 Google 云存储。我在 GCS 上启用了互操作性模式,并将 AWSSecretKeyAWSAccessKey 放入我的 App.config 中。我可以列出对象,但上传对我不起作用。

我使用的代码是

            AmazonS3Config c = new AmazonS3Config();
            c.ServiceURL = "https://storage.googleapis.com";
            client = new AmazonS3Client(c);
            PutObjectRequest request = new PutObjectRequest()
            {
                ContentBody = "asdf",
                BucketName = bucketName,
                Key = keyName
            };
            client.PutObject(request);

但是我得到的结果是:

Unhandled Exception:
Amazon.Runtime.AmazonClientException: Expected hash not equal to calculated hash
  at Amazon.S3.Internal.AmazonS3ResponseHandler.CompareHashes (System.String etag, System.Byte[] hash) [0x0003f] in <c4952b4c8b494e049894eba742a57891>:0
  at Amazon.S3.Internal.AmazonS3ResponseHandler.ProcessResponseHandlers (Amazon.Runtime.IExecutionContext executionContext) [0x00129] in <c4952b4c8b494e049894eba742a57891>:0
  at Amazon.S3.Internal.AmazonS3ResponseHandler.PostInvoke (Amazon.Runtime.IExecutionContext executionContext) [0x00000] in <c4952b4c8b494e049894eba742a57891>:0
  at Amazon.S3.Internal.AmazonS3ResponseHandler.InvokeSync (Amazon.Runtime.IExecutionContext executionContext) [0x00007] in <c4952b4c8b494e049894eba742a57891>:0
  at Amazon.Runtime.Internal.PipelineHandler.InvokeSync (Amazon.Runtime.IExecutionContext executionContext) [0x0000e] in <b8d5dfd6d5c84f7e87493f5d86f0adaa>:0
  at Amazon.Runtime.Internal.ErrorHandler.InvokeSync (Amazon.Runtime.IExecutionContext executionContext) [0x0001f] in <b8d5dfd6d5c84f7e87493f5d86f0adaa>:0
  at Amazon.Runtime.Internal.PipelineHandler.InvokeSync (Amazon.Runtime.IExecutionContext executionContext) [0x0000e] in <b8d5dfd6d5c84f7e87493f5d86f0adaa>:0
  at Amazon.Runtime.Internal.CallbackHandler.InvokeSync (Amazon.Runtime.IExecutionContext executionContext) [0x00007] in <b8d5dfd6d5c84f7e87493f5d86f0adaa>:0
  at Amazon.Runtime.Internal.PipelineHandler.InvokeSync (Amazon.Runtime.IExecutionContext executionContext) [0x0000e] in <b8d5dfd6d5c84f7e87493f5d86f0adaa>:0
  at Amazon.Runtime.Internal.Signer.InvokeSync (Amazon.Runtime.IExecutionContext executionContext) [0x00006] in <b8d5dfd6d5c84f7e87493f5d86f0adaa>:0
  at Amazon.Runtime.Internal.PipelineHandler.InvokeSync (Amazon.Runtime.IExecutionContext executionContext) [0x0000e] in <b8d5dfd6d5c84f7e87493f5d86f0adaa>:0
  at Amazon.Runtime.Internal.EndpointDiscoveryHandler.InvokeSync (Amazon.Runtime.IExecutionContext executionContext) [0x00030] in <b8d5dfd6d5c84f7e87493f5d86f0adaa>:0
  at Amazon.Runtime.Internal.PipelineHandler.InvokeSync (Amazon.Runtime.IExecutionContext executionContext) [0x0000e] in <b8d5dfd6d5c84f7e87493f5d86f0adaa>:0
  at Amazon.Runtime.Internal.CredentialsRetriever.InvokeSync (Amazon.Runtime.IExecutionContext executionContext) [0x00007] in <b8d5dfd6d5c84f7e87493f5d86f0adaa>:0
  at Amazon.Runtime.Internal.PipelineHandler.InvokeSync (Amazon.Runtime.IExecutionContext executionContext) [0x0000e] in <b8d5dfd6d5c84f7e87493f5d86f0adaa>:0
  at Amazon.Runtime.Internal.RetryHandler.InvokeSync (Amazon.Runtime.IExecutionContext executionContext) [0x00057] in <b8d5dfd6d5c84f7e87493f5d86f0adaa>:0
  at Amazon.Runtime.Internal.PipelineHandler.InvokeSync (Amazon.Runtime.IExecutionContext executionContext) [0x0000e] in <b8d5dfd6d5c84f7e87493f5d86f0adaa>:0
  at Amazon.Runtime.Internal.CallbackHandler.InvokeSync (Amazon.Runtime.IExecutionContext executionContext) [0x00007] in <b8d5dfd6d5c84f7e87493f5d86f0adaa>:0
  at Amazon.Runtime.Internal.PipelineHandler.InvokeSync (Amazon.Runtime.IExecutionContext executionContext) [0x0000e] in <b8d5dfd6d5c84f7e87493f5d86f0adaa>:0
  at Amazon.S3.Internal.AmazonS3KmsHandler.InvokeSync (Amazon.Runtime.IExecutionContext executionContext) [0x00007] in <c4952b4c8b494e049894eba742a57891>:0
  at Amazon.Runtime.Internal.PipelineHandler.InvokeSync (Amazon.Runtime.IExecutionContext executionContext) [0x0000e] in <b8d5dfd6d5c84f7e87493f5d86f0adaa>:0
  at Amazon.Runtime.Internal.EndpointResolver.InvokeSync (Amazon.Runtime.IExecutionContext executionContext) [0x00007] in <b8d5dfd6d5c84f7e87493f5d86f0adaa>:0
  at Amazon.Runtime.Internal.PipelineHandler.InvokeSync (Amazon.Runtime.IExecutionContext executionContext) [0x0000e] in <b8d5dfd6d5c84f7e87493f5d86f0adaa>:0
  at Amazon.S3.Internal.AmazonS3PostMarshallHandler.InvokeSync (Amazon.Runtime.IExecutionContext executionContext) [0x00007] in <c4952b4c8b494e049894eba742a57891>:0
  at Amazon.Runtime.Internal.PipelineHandler.InvokeSync (Amazon.Runtime.IExecutionContext executionContext) [0x0000e] in <b8d5dfd6d5c84f7e87493f5d86f0adaa>:0
  at Amazon.Runtime.Internal.Marshaller.InvokeSync (Amazon.Runtime.IExecutionContext executionContext) [0x00006] in <b8d5dfd6d5c84f7e87493f5d86f0adaa>:0
  at Amazon.Runtime.Internal.PipelineHandler.InvokeSync (Amazon.Runtime.IExecutionContext executionContext) [0x0000e] in <b8d5dfd6d5c84f7e87493f5d86f0adaa>:0
  at Amazon.S3.Internal.AmazonS3PreMarshallHandler.InvokeSync (Amazon.Runtime.IExecutionContext executionContext) [0x00007] in <c4952b4c8b494e049894eba742a57891>:0
  at Amazon.Runtime.Internal.PipelineHandler.InvokeSync (Amazon.Runtime.IExecutionContext executionContext) [0x0000e] in <b8d5dfd6d5c84f7e87493f5d86f0adaa>:0
  at Amazon.Runtime.Internal.CallbackHandler.InvokeSync (Amazon.Runtime.IExecutionContext executionContext) [0x00007] in <b8d5dfd6d5c84f7e87493f5d86f0adaa>:0
  at Amazon.Runtime.Internal.PipelineHandler.InvokeSync (Amazon.Runtime.IExecutionContext executionContext) [0x0000e] in <b8d5dfd6d5c84f7e87493f5d86f0adaa>:0
  at Amazon.S3.Internal.AmazonS3ExceptionHandler.InvokeSync (Amazon.Runtime.IExecutionContext executionContext) [0x00012] in <c4952b4c8b494e049894eba742a57891>:0
  at Amazon.Runtime.Internal.PipelineHandler.InvokeSync (Amazon.Runtime.IExecutionContext executionContext) [0x0000e] in <b8d5dfd6d5c84f7e87493f5d86f0adaa>:0
  at Amazon.Runtime.Internal.ErrorCallbackHandler.InvokeSync (Amazon.Runtime.IExecutionContext executionContext) [0x00012] in <b8d5dfd6d5c84f7e87493f5d86f0adaa>:0
  at Amazon.Runtime.Internal.PipelineHandler.InvokeSync (Amazon.Runtime.IExecutionContext executionContext) [0x0000e] in <b8d5dfd6d5c84f7e87493f5d86f0adaa>:0
  at Amazon.Runtime.Internal.MetricsHandler.InvokeSync (Amazon.Runtime.IExecutionContext executionContext) [0x0002b] in <b8d5dfd6d5c84f7e87493f5d86f0adaa>:0
  at Amazon.Runtime.Internal.RuntimePipeline.InvokeSync (Amazon.Runtime.IExecutionContext executionContext) [0x00006] in <b8d5dfd6d5c84f7e87493f5d86f0adaa>:0
  at Amazon.Runtime.AmazonServiceClient.Invoke[TResponse] (Amazon.Runtime.AmazonWebServiceRequest request, Amazon.Runtime.Internal.InvokeOptionsBase options) [0x0007d] in <b8d5dfd6d5c84f7e87493f5d86f0adaa>:0
  at Amazon.S3.AmazonS3Client.PutObject (Amazon.S3.Model.PutObjectRequest request) [0x0001c] in <c4952b4c8b494e049894eba742a57891>:0
  at GettingStartedGuide.S3Sample.Main (System.String[] args) [0x00059] in <52a410cf4a3b4fedb3f3e1dd35779339>:0

相同的代码在 S3 上工作正常(减去设置 ServiceURL)

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.

我发现要解决此错误,我需要将 UseChunkEncoding = false 添加到 PutObjectRequest:

            PutObjectRequest request = new PutObjectRequest()
            {
                ContentBody = "asdf",
                BucketName = bucketName,
                Key = keyName,
                UseChunkEncoding = false,  // <<<<<<
            };
            client.PutObject(request);