我正在尝试在s3中将对象上传到存储桶中。我正在使用REST API(没有SDK),我已经按照文档进行操作: https://docs.aws.amazon.com/AmazonS3/latest/API/sig-v4-header-based-auth.html
和
https://docs.aws.amazon.com/AmazonS3/latest/API/RESTObjectPUT.html
此文档中:
示例:PUT对象(部分)
对于内容为“欢迎使用Amazon S3”的文本文件,我的规范应该看起来像这样:
PUT
/test%24file.text
date:Fri, 24 May 2013 00:00:00 GMT
host:examplebucket.s3.amazonaws.com
x-amz-content-sha256:44ce7dd67c959e0d3524ffac1771dfbba87d2b6b4b4e99e42034a8b803f8b072
x-amz-date:20130524T000000Z
x-amz-storage-class:REDUCED_REDUNDANCY
date;host;x-amz-content-sha256;x-amz-date;x-amz-storage-class
44ce7dd67c959e0d3524ffac1771dfbba87d2b6b4b4e99e42034a8b803f8b072
当我执行请求(更新时间和签名)时,出现以下错误
<?xml version="1.0" encoding="UTF-8"?>
<Error>
<Code>SignatureDoesNotMatch</Code>
<Message>The request signature we calculated does not match the signature you provided. Check your key and signing method.</Message>
<AWSAccessKeyId>AKIA5BJ3BZ3BKLL6FV5Y</AWSAccessKeyId>
<StringToSign>AWS4-HMAC-SHA256
20190626T012158Z
20190626/us-west-2/s3/aws4_request
b58f679417e49a3f9a2be1fe6d13b642207660a328949d9e2a374eb92d8f97ef</StringToSign>
<SignatureProvided>cfa3661fb2a9367fb9690a4a4d76e0cb21f436d5f79a645ba8d3c146c8250fc8</SignatureProvided>
<StringToSignBytes>41 57 53 34 2d 48 4d 41 43 2d 53 48 41 32 35 36 0a 32 30 31 39 30 36 32 36 54 30 31 32 31 35 38 5a 0a 32 30 31 39 30 36 32 36 2f 75 73 2d 77 65 73 74 2d 32 2f 73 33 2f 61 77 73 34 5f 72 65 71 75 65 73 74 0a 62 35 38 66 36 37 39 34 31 37 65 34 39 61 33 66 39 61 32 62 65 31 66 65 36 64 31 33 62 36 34 32 32 30 37 36 36 30 61 33 32 38 39 34 39 64 39 65 32 61 33 37 34 65 62 39 32 64 38 66 39 37 65 66</StringToSignBytes>
<CanonicalRequest>PUT
/
date:Wed, 26 Jun 2019 01:21:58 GMT
host:mulesofttdx19.s3-us-west-2.amazonaws.com
x-amz-content-sha256:e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855
x-amz-date:20190626T012158Z
x-amz-storage-class:REDUCED_REDUNDANCY
date;host;x-amz-content-sha256;x-amz-date;x-amz-storage-class
e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855</CanonicalRequest>
<CanonicalRequestBytes>50 55 54 0a 2f 0a 0a 64 61 74 65 3a 57 65 64 2c 20 32 36 20 4a 75 6e 20 32 30 31 39 20 30 31 3a 32 31 3a 35 38 20 47 4d 54 0a 68 6f 73 74 3a 6d 75 6c 65 73 6f 66 74 74 64 78 31 39 2e 73 33 2d 75 73 2d 77 65 73 74 2d 32 2e 61 6d 61 7a 6f 6e 61 77 73 2e 63 6f 6d 0a 78 2d 61 6d 7a 2d 63 6f 6e 74 65 6e 74 2d 73 68 61 32 35 36 3a 65 33 62 30 63 34 34 32 39 38 66 63 31 63 31 34 39 61 66 62 66 34 63 38 39 39 36 66 62 39 32 34 32 37 61 65 34 31 65 34 36 34 39 62 39 33 34 63 61 34 39 35 39 39 31 62 37 38 35 32 62 38 35 35 0a 78 2d 61 6d 7a 2d 64 61 74 65 3a 32 30 31 39 30 36 32 36 54 30 31 32 31 35 38 5a 0a 78 2d 61 6d 7a 2d 73 74 6f 72 61 67 65 2d 63 6c 61 73 73 3a 52 45 44 55 43 45 44 5f 52 45 44 55 4e 44 41 4e 43 59 0a 0a 64 61 74 65 3b 68 6f 73 74 3b 78 2d 61 6d 7a 2d 63 6f 6e 74 65 6e 74 2d 73 68 61 32 35 36 3b 78 2d 61 6d 7a 2d 64 61 74 65 3b 78 2d 61 6d 7a 2d 73 74 6f 72 61 67 65 2d 63 6c 61 73 73 0a 65 33 62 30 63 34 34 32 39 38 66 63 31 63 31 34 39 61 66 62 66 34 63 38 39 39 36 66 62 39 32 34 32 37 61 65 34 31 65 34 36 34 39 62 39 33 34 63 61 34 39 35 39 39 31 62 37 38 35 32 62 38 35 35</CanonicalRequestBytes>
<RequestId>51CE31BA9D71033A</RequestId>
<HostId>DzMCaanrDg/dPzsbdTESZtj0XtAAIAWqFXaGXfuipcpfLk1l38itMovqQA93NbBjdn8jIRxRFGc=</HostId>
</Error>
似乎规范请求不接受名称(我尝试了具有相同结果的不同选项),因为它的字符串像这样响应()
/
代替
/test%24file.text
我不明白如何传递文件名,或者是否缺少其他内容。
我将所有标头设置如下:
request.setMethod('PUT');
request.setEndpoint(canonicalUriFor);
request.setHeader('Host', 'mulesofttdx19.s3-us-west-2.amazonaws.com');
request.setHeader('Date', iso_aws_DateTime);
request.setHeader('Authorization', strAuthorizationHeader);
request.setHeader('x-amz-date',iso8601time);
request.setHeader('x-amz-storage-class','REDUCED_REDUNDANCY');
request.setHeader('X-Amz-Content-Sha256',NewAttachmentSync.hexEncodedHash(payload));