如何使用带有 Python Boto 客户端的 CloudFront 签名者生成预签名 PUT URL(如果可能)?

How to generate Pre-signed PUT URL (if possible) using CloudFront signer with Python Boto client?

我一直在使用 python boto 客户端开发 S3 存储应用程序。客户端从服务器请求预签名上传(预签名 PUT)和下载(预签名 GET)URLs 文件。

使用 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。这允许对 PUT 和 GET 方法使用相同的 URL 来分别上传和下载文件。

        "Action": [
            "s3:PutObject",
            "s3:GetObject"
        ]

复制包含密钥的 *.pem 文件、再次下载文件并替换旧文件时似乎也出现了问题。