Fra*_*nkN 1 html rest google-app-engine json amazon-s3
我有一个在 GAE 上运行的 html 页面,我想用它来允许用户将文本文件从他们的计算机上传到我在 s3 上的存储桶。下面是代码:
----- HTML 代码-----
<!DOCTYPEhtml PUBLIC "-//W3C//DTDXHTML 1.0 transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-trnasitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>S3 POST Form</title>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<link href="/static/def.css" rel="stylesheet" type="text/css"/>
<title> Upload Page </title>
</head>
<body>
<form action="https://mys3bucket.s3.amazonaws.com/" method="post" enctype="multipart/form-data">
<input type="hidden" name="key" value="uploads/${filename}">
<input type="hidden" name="AWSAccessKeyId" value="Put access key id here">
<input type="hidden" name="acl" value="public-read">
<input type="hidden" name="success_action_redirect" value="https://mys3bucket.s3.amazonaws.com/complete.htm">
<input type="hidden" name="policy" value="***---How do I calculate this?---***">
<input type="hidden" name="signature" value="***---How do I calculate this?---***">
<!-- 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>
Run Code Online (Sandbox Code Playgroud)
我在计算策略和签名值时遇到问题。
我四处搜索,总体主题似乎是策略文档是一个 JSON 文件,但我不确切知道 JSON 文件中需要哪些字段,也不知道如何将该 json 文件插入策略字段。这些字段的一般主题似乎是它们需要使用 HMAC 和 base64 编码进行计算,如果是这样,我该如何去做呢?
我发现了一些应该对所有内容进行编码的 python 代码,但它似乎不起作用。
谢谢。
F
------ 下面是 JSON 文件 ---------- (已更新)
{
"expiration": "2014-06-009T12:00:00.000Z",
"conditions": [
{
"bucket": "https://mys3bucket.s3.amazonaws.com/"
},
{
"$key": "uploads/${filename}"
},
{
"AWSAccessKeyId": "axaxaxaxaxax"
},
{
"acl": "public-read"
},
{
"redirect": "http://mys3bucket.s3.amazonaws.com/complete.htm"
}
]
}
Run Code Online (Sandbox Code Playgroud)
- - - 错误信息 - - - -
这是我上传应用程序并运行它时收到的错误消息:
<Error>
<Code>InvalidPolicyDocument</Code>
<Message>
Invalid Policy: Expecting String Literal but found Field Reference
</Message>
<RequestId>6E2CADE36E18DDBA</RequestId>
<HostId>
gxxd2YJLcIz+wAVzasBc9yOpW0GOok1dAWwH2apv28epGhuAXGS/z4WiAtznUTLt
</HostId>
</Error>
Run Code Online (Sandbox Code Playgroud)
------ BASE 64 Python 程序 ------
下面是我发现的一个小程序,应该为我编码策略和签名,这是正确的吗?如果是的话,我哪里出错了?
import base64
import hmac, hashlib
secret_access_key = "ssxsxsxsxsxsxsxsxsxsx"
with open('GAEPOST.json') as file:
policy_document = file.read()
policy = base64.b64encode(policy_document)
signature = base64.b64encode(hmac.new(secret_access_key, policy, hashlib.sha1).digest())
print "This is the signature: " + signature
print "This is the policy: " + policy
Run Code Online (Sandbox Code Playgroud)
我刚刚遇到了同样的问题。
Invalid Policy: Expecting String Literal but found Field Reference当您将“变量”作为条件的键(如$key本例所示)时会发生
{
"expiration": "2015-6-18T09:02:17.000Z",
"conditions": [
{"acl": "public-read"},
{"bucket": "my-bucket"},
{"$key": "my-file.png"}, // INVALID CONDITION
["eq", "$Content-Type", "image/png"]
]
}
Run Code Online (Sandbox Code Playgroud)
您必须输入{"key": "my-file.png"}(no $) 或["eq", "$key", "my-file.png"](with $)。这两个条件都是有效且平等的。
所以你的例子这个条件是无效的:
{"$key": "uploads/${filename}"}
Run Code Online (Sandbox Code Playgroud)
并应替换为其中之一
["eq", "$key": "uploads/${filename}"]
["starts-with", "$key": "uploads/"]
Run Code Online (Sandbox Code Playgroud)
为了解决您在评论 ( ["eq", "$bucket", "<bucket url>"]) 中描述的第二个问题:您不要将完整的存储桶 url 放入此条件中,而只需将存储桶的名称放入其中。
{"bucket": "https://mys3bucket.s3.amazonaws.com/"}因此,不要使用{"bucket": "mys3bucket"}or的条件["eq", "$bucket": "mys3bucket"]。
我将链接文档以获取确切的语法,无论亚马逊不断更改其 URL,并且该链接可能会在几个月内失效。
| 归档时间: |
|
| 查看次数: |
703 次 |
| 最近记录: |