我正在尝试创建一个AWS POST策略,并使用Flask模板发送POST请求,以使用Web浏览器上传文件。
打开端点网页时,我可以成功打开html文件。但是,一旦提交表单,我最终将收到“字段中格式错误的Unicode代码序列”或“ POST要求每个请求恰好上传一个文件”。错误,具体取决于文件类型。
我的假设是我 1.制定了错误的政策 2.创建了错误的签名 3.或两者
我尝试了多种方法来修复它,但无法弄清楚。 我正在使用Python3
'''
<body>
<h1>Upload VCF File</h1>
<form id="upload_form" action="https://{{ bucket_name }}.s3.amazonaws.com/" method="post" enctype="multipart/form-data">
<input type="hidden" name="key" value="{{ s3key }}">
<input type="hidden" name="acl" value="{{ acl }}">
<input type="hidden" name="AWSAccessKeyId" value="{{ aws_key }}">
<input type="hidden" name="acl" value="{{ acl }}">
<input type="hidden" name="policy" value="{{ policy }}">
<input type="hidden" name="x-amz-signature" value="{{ signature }}">
Select input file: <input id="upload_file" type="file" name="file" />
<input type="submit" value="Upload Input File" />
</form>
</body>
'''
这是我基于Flask的Python文件 '''
@app.route('/annotate', methods=['GET'])
# This method is called when a user see "/annotate" on your server.
def annotate():
#Define S3 policy fields and conditions
s3 = boto3.client('s3')
bucket_namee = 'gas-inputs'
now = datetime.datetime.now()
exptime = now + datetime.timedelta(minutes=15)
exptime_ = exptime.strftime('%Y-%m-%dT%H:%M:%S.000Z')
datelong = datetime.datetime.utcnow().strftime('%Y%m%dT000000Z')
AWS_SECRET_ACCESS_KEY = '<secret key>'
today = datetime.date.today().strftime("%Y%m%d")
s3keyy = '/jhwang11/${filename}'
aws_keyy = '<key>'
acll = "public-read"
#Create Policy
bucket_policy = { "expiration": str(exptime_),
"conditions": [
{"acl": acll },
{"bucket": bucket_namee },
["starts-with", "$key", s3keyy],
{"x-amz-credential":'<AWS Key>/{}/us-east-1/s3/aws4_request'.format(today)},
{"x-amz-algorithm": "AWS4-HMAC-SHA256"},
{"x-amz-date":datelong}
]
}
bucket_policy = json.dumps(bucket_policy)
#Generate signed POST request
policyy = base64.b64encode(bucket_policy.encode('utf-8'))
signaturee = base64.b64encode(hmac.new(AWS_SECRET_ACCESS_KEY.encode('utf-8'), policyy, hashlib.sha256).digest())
return render_template("annotate.html", id = uqid, bucket_name = bucket_namee, acl=acll, aws_key = aws_keyy, policy =policyy, s3key = s3keyy, signature = signaturee)
'''