浏览器使用带有签名版本4的HTML POST表单上传到S3

时间:2018-10-29 08:24:31

标签: python-3.x forms amazon-web-services amazon-s3

我当前正在尝试将浏览器上传到here的s3上载。但是,我总是得到一个错误,说The authorization mechanism you have provided is not supported. Please use AWS4-HMAC-SHA256.。我在StackOverflow上找到了几个答案,但是似乎没有人仅使用表单和python来完成它,所以我仍然对这个问题感到困惑。

这是我当前的HTML代码:(我检查了访问密钥,签名,策略和实际存储区)

<html>

<head>
    <title>S3 POST Form</title>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
</head>

<body>
    <form action="https://some-bucket.s3.eu-central-1.amazonaws.com/" method="post" enctype="multipart/form-data">
        <input type="hidden" name="key" value="uploads/${filename}"> <input type="hidden" name="AWSAccessKeyId" value="ACCESSKEY">
        <input type="hidden" name="acl" value="private">
        <input type="hidden" name="success_action_redirect" value="http://www.google.de">
        <input type="hidden" name="policy" value="SOMEPOLICY">
        <input type="hidden" name="signature" value="SIGNATURE">
        <input type="hidden" name="Content-Type" value="image/jpeg">
        <!-- Include any additional input fields here -->
        File to upload to S3: <input name="file" type="file">
        <br>
        <input type="submit" value="Upload File to S3">
    </form>
</body>

</html>

我事先借助python脚本计算了策略和签名:(再次从本文中复制,我只是将有效期设置为2009年至2019年)

import base64
import hmac, hashlib
import json

policy_document = {"expiration": "2019-01-01T00:00:00Z", "conditions": [ {"bucket": "some-bucket/"}, ["starts-with", "$key", "uploads/"], {"acl": "private"}, {"success_action_redirect": "http://www.google.de"}, ["starts-with", "$Content-Type", ""], ["content-length-range", 0, 1048576]]}

s = json.dumps(policy_document)


policy = base64.b64encode(s.encode('utf-8'))

print('POLICY: ', policy)

key= 'ACCESSKEY'

signature = base64.b64encode(hmac.new(key.encode('utf-8'), policy, hashlib.sha256).digest())

print ('SIGNATURE: ', signature)

基本上,这完全是来自AWS article的代码,但是我仍然收到此错误。

我该如何解决?

0 个答案:

没有答案