首页 > 解决方案 > 使用适用于 .NET 的 AWS SDK 上传到 Google Cloud Storage `预期的哈希值不等于计算的哈希值`

问题描述

我正在尝试使用适用于 .NET 的 AWS SDK 上传到 Google Cloud Storage。我在 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)

标签: google-cloud-storageaws-sdk-net

解决方案


GCS 文档说:

在 Cloud Storage XML API 中,分块传输编码和 V4 签名目前不能同时使用。一些 Amazon S3 工具默认使用分块传输编码和签名;在这种情况下,您应该禁用分块传输编码。

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

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

推荐阅读