我按照此问题的说明https://github.com/aws/aws-sdk-go/issues/467清楚地记录了如何为PUT请求创建预先签名的网址。目标是预设网址,这样我就可以直接从浏览器上传图像
密钥和密钥当然是我当前的凭据,可以通过SDK直接使用PutObject请求
creds := credentials.NewStaticCredentials("key", "secret", "")
cfg := aws.NewConfig().WithRegion("us-west-2").WithCredentials(creds)
srv := s3.New(session.New(), cfg)
params := &s3.PutObjectInput{
Bucket: aws.String("my-bucket"),
Key: aws.String("/local/test/filename"),
}
req, _ := srv.PutObjectRequest(params)
url, err := req.Presign(15 * time.Hour)
if err != nil {
fmt.Println("error signing request", err)
}
fmt.Println("URL", url)
```
然后,我接受该URL并发出curl请求。我收到了这个回复
<?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>redacted</AWSAccessKeyId>
<StringToSign>redacted</StringToSign>
<SignatureProvided>redacted</SignatureProvided>
<StringToSignBytes>redacted</StringToSignBytes>
<CanonicalRequest>PUT
/local/test/filename
X-Amz-Algorithm=AWS4-HMAC-SHA256&X-Amz-Credential=redacted%2Fus-west-2%2Fs3%2F%20aws4_request&X-Amz-Date=20161129T012909Z&X-Amz-Expires=54000&X-Amz-SignedHeaders=host
host:redacted.s3-us-west-2.amazonaws.com
host
UNSIGNED-PAYLOAD</CanonicalRequest>
<CanonicalRequestBytes>redacted</CanonicalRequestBytes>
<RequestId>redacted</RequestId>
<HostId>redacted</HostId>
</Error>
为什么预先签名的URL为我提供了一个据称不匹配的签名?同样,这些相同的凭证当前正在我的服务器上直接使用PutObject命令
答案 0 :(得分:1)
我的Bucket策略配置不正确。我不得不用IAM政策交叉引用我的密钥/秘密,并确保它们列在政策的“委托人”部分