Python 过帐到AWS S3签名不匹配错误

Python 过帐到AWS S3签名不匹配错误,python,rest,amazon-s3,base64,Python,Rest,Amazon S3,Base64,这是我的表格 <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /> </head> <body> <form action="http://mybucket.s3.amazonaws.com/" method="post" enctype="multipart/form-data">

这是我的表格

<html>
  <head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
  </head>
  <body>

  <form action="http://mybucket.s3.amazonaws.com/" method="post" enctype="multipart/form-data">
    Key to upload: <input type="input" name="key" value="user/eric/" /><br />
    <input type="hidden" name="acl" value="public-read" />



    <input type="hidden" name="AWSAccessKeyId" value="myAWSId" />
    <input type="hidden" name="Policy" value="Base64EncodingOfPolicy"/>
    <input type="hidden" name="Signature" value="Signature Calculated as urlencode(base64(HMAC-SHA1(secret, policy base64 encoded string same as utf-8 encoded)))" />
    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>

</html>
这是base64编码策略

eyAiZXhwaXJhdGlvbiI6ICIyMDE0LTEyLTAxVDEyOjAwOjAwLjAwMFoiLA0KDQogICJjb25kaXRp
b25zIjogWw0KDQogICAgeyJhY2wiOiAicHVibGljLXJlYWQiIH0sDQoNCiAgICB7ImJ1Y2tldCI6
ICJoYWJpdHN1c2VybWVkaWEiIH0sDQoNCiAgICBbInN0YXJ0cy13aXRoIiwgIiRrZXkiLCAidXNl
ci9lcmljLyJdLA0KDQogIF0NCg0KfQ==
我尝试过使用base64编码的字符串,包括和不包括换行符。在使用上述编码和换行时,我是否应该记住一些特定的东西

即使在几次尝试解释所有可能的排列之后,我仍然不断得到
signaturedesnotmatch
错误

我还使用了签名验证工具,检查了AWS接受的内容和匹配的内容


我如何调试这个?如果您已经成功地使用REST API发布到S3,您可以共享该代码段吗?

好的,过了一会儿我就可以解决这个问题了

这是我生成签名的python代码

import base64
import hmac
from hashlib import sha1
import urllib


input = open("policy.txt", "rb")
policy = input.read()
policy_encoded = base64.b64encode(policy).encode("UTF-8")
secret = "<my_aws_secret>"

print 'Encoded Policy  %s' %(policy_encoded)

hashed = hmac.new(secret,policy_encoded, sha1)


#This is the required value
signature = base64.b64encode(hashed.digest())

#This is not required for a HTTP POST form based request, only when it has to be passed in urlencoded format
signature_urlencoded = urllib.quote_plus(base64.b64encode(hashed.digest()))

print 'Signature urlencoded  %s' %(signature)
导入base64
进口hmac
从hashlib导入sha1
导入URL库
输入=打开(“policy.txt”、“rb”)
policy=input.read()
policy_encoded=base64.b64编码(policy).encode(“UTF-8”)
secret=“”
打印“编码策略%s%”(策略\u编码)
hashed=hmac.new(秘密,策略编码,sha1)
#这是所需的值
signature=base64.b64encode(hashed.digest())
#对于基于HTTP POST表单的请求,这不是必需的,只有当它必须以urlencoded格式传递时才需要
签名\u urlencoded=urllib.quote\u plus(base64.b64encode(hashed.digest())
打印“签名已编码%s%”(签名)
我的policy.txt是

{ "expiration": "2014-12-01T12:00:00.000Z",

"conditions": [

        {"acl": "public-read" },

        {"bucket": "<mybucket>" },

        {"success_action_status" : "201"},

        ["starts-with", "$key", "uploads/"],
        ]

}
{“到期”:“2014-12-01T12:00:00.000Z”,
“条件”:[
{“acl”:“公共读取”},
{“bucket”:“},
{“成功行动状态”:“201”},
[“以“,“$key”,“uploads/”开头],
]
}
我的表格看起来像

<form action="http://habitsusermedia.s3.amazonaws.com/" method="post" enctype="multipart/form-data">

                <input type="hidden" name="acl" value="public-read" />
                <input type="input" name="key" value="uploads/${filename}" />
                <input type="hidden" name="success_action_status" value="201" />

                <input type="hidden" name="AWSAccessKeyId" value="<aws_access_key>" />

                <input type="hidden" name="Policy" value="<policy_encoded_as_base64>"/>

                <input type="hidden" name="Signature" value="<signature>" />

                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>

<form action="http://habitsusermedia.s3.amazonaws.com/" method="post" enctype="multipart/form-data">

                <input type="hidden" name="acl" value="public-read" />
                <input type="input" name="key" value="uploads/${filename}" />
                <input type="hidden" name="success_action_status" value="201" />

                <input type="hidden" name="AWSAccessKeyId" value="<aws_access_key>" />

                <input type="hidden" name="Policy" value="<policy_encoded_as_base64>"/>

                <input type="hidden" name="Signature" value="<signature>" />

                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>