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

时间:2018-05-22 05:16:41

标签: amazon-web-services amazon-s3

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' ..

1 个答案:

答案 0 :(得分: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