生成策略和签名值以将数据直接从HTML post请求存储到Amazon S3存储桶

问题描述 投票:0回答:1
import base64
import hmac, hashlib
AWS_SECRET_ACCESS_KEY = 'AKIAIHHMU7Y4L2INOFRQ'

policy_document = {
    "expiration": "2019-01-01T00:00:00Z",
    "conditions": [ {"bucket": "report-generation1"},
                    ["starts-with", "$key", ""],
                    {"acl": "private"},
                    {"success_action_redirect": "localhost/";},
                    ["starts-with", "$Content-Type", ""],
                    ["content-length-range", 0, 1048576]
                   ]
                  }

policy = base64.b64encode(policy_document)

signature = base64.b64encode(hmac.new(AWS_SECRET_ACCESS_KEY, policy, hashlib.sha1).digest())

我需要生成策略和签名值,以便将数据直接从HTML post请求存储到Amazon S3存储桶。

上述程序给出错误:

TypeError:需要类似字节的对象,而不是'dict'..

amazon-web-services amazon-s3
1个回答
0
投票

啊!搞定了。

你有一个迷路的分号。此外,b64encode需要字符串而不是字典,所以只需将对象放在引号内。

import base64
import hmac, hashlib
AWS_SECRET_ACCESS_KEY = '<your-secret-access-key>'

policy_document = '{"expiration": "2019-01-01T00:00:00Z", "conditions": [ {"bucket": "report-generation1"}, ["starts-with", "$key", ""], {"acl": "private"}, {"success_action_redirect": "localhost/"}, ["starts-with", "$Content-Type", ""], ["content-length-range", 0, 1048576]]}'

policy = base64.b64encode(policy_document)

signature = base64.b64encode(hmac.new(AWS_SECRET_ACCESS_KEY, policy, hashlib.sha1).digest())

此外,请注意您提供了Access Key而不是Secret Access Key

© www.soinside.com 2019 - 2024. All rights reserved.