【问题标题】:Generate policy and signature values to store data directly from HTML post request to Amazon S3 bucket生成策略和签名值以将数据直接从 HTML 发布请求存储到 Amazon S3 存储桶
【发布时间】:2018-05-22 13:44:44
【问题描述】:
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 发布请求存储到 Amazon S3 存储桶。

上述程序报错:

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

【问题讨论】:

  • 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", ""], ["内容长度范围", 0, 1048576] }
  • 哪一行产生了错误?此外,您的秘密访问密钥是一长串随机字符,而不是您显示的那个。 (您显示的是访问密钥,而不是秘密访问密钥。)

标签: amazon-web-services amazon-s3


【解决方案1】:

啊!搞定了。

你有一个杂散的分号。此外,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

【讨论】:

    猜你喜欢
    • 2016-08-05
    • 2011-09-10
    • 2023-02-26
    • 2013-12-21
    • 2023-03-31
    • 2013-01-08
    • 1970-01-01
    • 1970-01-01
    • 2012-06-02
    相关资源
    最近更新 更多