【发布时间】:2017-06-22 15:01:45
【问题描述】:
我正在尝试编写一个 Flask 应用程序以上传到 AWS S3 存储桶。在哪里,当我在 PyCharm 中本地运行它时,它工作正常。但是,一旦我将它部署到 AWS(在端口 80 上部署 Flask 应用程序),我现在收到一个错误...
botocore.exceptions.ClientError: An error occurred (SignatureDoesNotMatch) when calling the ListBuckets operation: The request sture we calculated does not match the signature you provided. Check your key and signing method.
当密钥在本地工作时......但不在 AWS EC2 实例上工作。我最初的一些想法可能是端口问题或 boto3 问题。虽然我不确定,因为它可以在本地工作,而不是在 AWS 上。
有什么帮助吗?这是我的代码...删除了 c 的键和 URL
app.py
from flask import Flask, render_template, flash
from werkzeug.utils import secure_filename
from flask_wtf import FlaskForm
from flask_wtf.file import FileField, FileRequired
from tools import s3_upload
'''
Author: xxx
'''
app = Flask(__name__)
app.config.from_object('config')
# Flask Secret Key
app.secret_key = 'xxxxx'
# Limits what file types can be uploaded
def allowed_file(filename):
return '.' in filename and \
filename.rsplit('.', 1)[1] in app.config['ALLOWED_EXTENSIONS']
# Initializes upload form
class UploadForm(FlaskForm):
example = FileField(validators=[FileRequired()])
# Route for root, handles on click action for upload form
@app.route('/', methods=['POST', 'GET'])
def upload_page():
form = UploadForm()
if form.validate_on_submit():
file = form.example.data
filename = secure_filename(file.filename)
output = s3_upload(file,filename)
flash('{src} uploaded to S3'.format(src=form.example.data.filename))
return render_template('index.html', form=form)
if __name__ == '__main__':
app.run()
tools.py
import boto3
from flask import current_app as app
'''
Author: xxx
'''
def s3_upload(source_file, source_filename):
# What directory on Amazon S3 Bucket to upload to.
upload_dir = app.config["S3_UPLOAD_DIRECTORY"]
#Connect to S3 and upload file
s3 = boto3.client('s3')
s3.upload_fileobj(source_file, app.config["S3_BUCKET"], upload_dir + "/" + source_filename)
config.py
S3_KEY = 'xxx'
S3_SECRET = 'xxxx'
S3_UPLOAD_DIRECTORY = 'xxxx'
S3_BUCKET = 'xxxx'
ALLOWED_EXTENSIONS = ['txt', 'pdf', 'png', 'jpg', 'jpeg', 'gif']
SECRET_KEY = "xxxx"
DEBUG = True
【问题讨论】:
标签: python amazon-web-services amazon-s3 amazon-ec2 flask