python - 创建 StringToSign

我正在阅读将查询字符串传递给 Amazon 的 S3 以进行身份​​验证的文档,但似乎无法理解 StringToSign 的创建和使用方式。我正在寻找一个具体示例来说明 (1) 如何构造 StringToSign,以及 (2) 一旦我有了签名,如何调用表单。

为了举例,假设以下是我的信息:

Content-type='image/jpeg'
Bucket='test-bucket'
Key = 'filename'
ACL = 'public-read'
Expiration = '(never expires)'
Access Key = '12345'
Secret Password = '1a2b3c'
File = <file the user uploads>

我如何从中获取 StringToSign 值?一旦有了它,我将如何创建以下表单:

<form action="??" method="post" enctype='multipart/form-data' class="upload-form">
    <input name="file" type="file"> 
</form>

并供引用:http://docs.amazonwebservices.com/AmazonS3/latest/dev/index.html?RESTAuthentication.html#RESTAuthenticationQueryStringAuth .谢谢。

最佳答案

根据您的描述,您似乎希望使用 POST 支持基于浏览器的上传。有一个section of the AWS documentation which talks about this .

作为概述,请记住您要么必须使您的存储桶公开可写,要么包括一个策略文档。我假设您将包含一份政策文件(如果您不想,请查看文档):

Policy 文档只是 JSON 的一个片段,用于对请求进行身份验证,并给出了上传数据之前必须满足的一系列条件。例如:

"expiration": "2020-12-01T12:00:00.000Z",
"conditions": [
    {"acl": "public-read" },
    {"bucket": "test-bucket" },
    ["eq", "$key", "filename"],
  ]
}

这表示上传操作将被允许到 2020 年,因为该存储桶仅可公开读取,存储桶名称为“test-bucket”且 key 正好等于“文件名”。

现在,要构造你的签名,你需要上面的 JSON 文档,UTF-8 编码,然后 base64,然后使用你的 secret 访问 key (使用 hmac sha1)签署整个事情,最后 base64 整个事情

policy_data = ... # stuff above
enc_policy = base64.b64_encode(policy_data.encode('utf8'))
signed = base64.b64_encode(hmac.new(AWS_SECRET, enc_policy, hashlib.sha1))

最后,您的表单将如下所示:

 <form action="http://test-bucket.s3.amazonaws.com/" method="post" enctype="multipart/form-data">
    Key to upload: <input type="input" name="key" value="filename" /><br />
    <input type="hidden" name="acl" value="public-read" />
    <input type="hidden" name="success_action_redirect" value="http://test-bucket.s3.amazonaws.com/successful_upload.html" />
    Content-Type: <input type="input" name="Content-Type" value="image/jpeg" /><br />
    <input type="hidden" name="AWSAccessKeyId" value="YOUR_ACCESS_KEY_ID" />
    <input type="hidden" name="Policy" value="<enc_policy from above>" />
    <input type="hidden" name="Signature" value="<signed from above>" />
    File: <input type="file" name="file" /> <br />
    <!-- The elements after this will be ignored -->
    <input type="submit" name="submit" value="Upload to Amazon S3" />
  </form>

https://stackoverflow.com/questions/7290684/

相关文章:

iis - Windows 身份验证通过反向代理问题

python - os.walk 某些文件类型到现有的 ZipFile?

xml - Spring:java.util.Locale 类型的 bean 的 Autowirin

csv - 如何使用 RSqlite 将 sqlite 导出为 CSV?

spring - 无法从 InputStream : Invalid Content-Type:te

eclipse - 无法在 Eclipse + EPIC 中查看本地

maven-2 - 来自另一个模块的 Maven 模块属性

google-maps - 我如何知道哪个标记正在被 Google Maps API 拖动事件拖动?

eclipse - 如何从 Eclipse 类路径中排除一个 jar?

regex - Nginx:如何重写除图像以外的所有 URL?