如何解决如何使用CloudFront签名者和Python Boto客户端生成预签名的PUT URL如果可能?
我一直在使用python boto客户端开发S3存储应用程序。客户端请求从服务器获取文件的预签名上载(Pre-signed PUT)和下载(预签名GET)URL。
使用boto3 s3会话,可以使用
response = session.generate_presigned_url(
"put_object",Params={
"Bucket": client.aws_bucket,"Key": s3_object,},ExpiresIn=client.url_expiration,)
但是,现在我正在尝试使用boto3的CloudFront API实现相同的功能。我跟随this example生成了下载URL。 (如建议的文档所述,在aws控制台中设置了密钥。
如果我使用s3预签名PUT URL上传,则无法下载通过CloudFront预签名URL生成的文件。导致以下错误(某些哈希值已更改为隐藏详细信息):
<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>removed access key</AWSAccessKeyId>
<StringToSign>AWS4-HMAC-SHA256 20200909T010545Z 20200909/us-east-1/s3/aws4_request dec845474b8be721379ebb7b43a31ad34c658eaf3c9668a428fc0bc0dff02b63</StringToSign>
<SignatureProvided>cd87ad2fc7d6248f8046dbda7aa3db8914c8704d2ab4e939aeeecabf98c8ea37</SignatureProvided>
<StringToSignBytes>41 57 53 34 2d 48 4d 41 43 2d 53 48 41 32 35 36 0a 32 30 32 30 30 39 30 39 54 30 31 30 35 34 35 5a 0a 32 30 32 30 30 39 30 39 2f 75 73 2d 65 61 73 74 2d 31 2f 73 33 2f 61 77 73 34 5f 72 65 71 75 65 73 74 0a 64 65 63 38 34 35 34 37 34 62 38 62 65 37 32 31 33 37 39 65 62 62 37 62 34 33 61 33 31 61 64 33 34 63 36 35 38 65 61 66 33 63 39 36 36 38 61 34 32 38 66 63 30 62 63 30 64 66 66 30 32 62 36 33</StringToSignBytes>
<CanonicalRequest>GET somefile.png host:example.com x-amz-content-sha256:e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855 x-amz-date:20200909T010545Z host;x-amz-content-sha256;x-amz-date e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855</CanonicalRequest>
<CanonicalRequestBytes>47 45 54 0a 2f 62 33 64 61 61 37 37 62 34 63 30 34 61 39 35 35 31 62 38 37 38 31 64 30 33 31 39 31 66 65 30 39 38 66 33 32 35 65 36 37 2f 73 73 2e 70 6e 67 0a 0a 68 6f 73 74 3a 62 6f 69 6e 67 2e 70 61 6e 61 63 65 61 68 65 61 6c 74 68 2e 61 69 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 32 30 30 39 30 39 54 30 31 30 35 34 35 5a 0a 0a 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 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>14670F5525B7189A</RequestId>
<HostId>2xVb5KggcsomehostidpartJjLHVlD0ZDA7TIMWuThXJyYrR/B9g3+RbhPZ7xjoHzKGI=</HostId>
</Error>
是否可以使用CloudFront API生成预签名的PUT URL?我找不到任何有用的东西。还是客户端应该始终使用通过s3会话对象生成的预先签名的PUT URL进行上传?
解决方法
几天后,我终于注意到了错误。从cloudFront控件中,该策略必须同时包含PutObject和GetObject。这允许将相同的URL与PUT和GET方法一起使用来分别上传和下载文件。
"Action": [
"s3:PutObject","s3:GetObject"
]
在复制包含密钥的*.pem
文件,再次下载文件并替换旧文件时,似乎也存在问题。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。