繁体   English   中英

尝试从Flask应用程序中的EC2实例将文件上传到Amazon S3时出现ClientError(SignatureDoesNotMatch)

[英]ClientError (SignatureDoesNotMatch) when trying to upload file to Amazon S3 from an EC2 instance in a Flask app

我正在尝试编写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上运行。

有什么帮助吗? 这是我的代码...删除了键和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

尝试将客户端对象更改为:

client = boto3.client('s3', config=boto3.session.Config(signature_version='s3v4'))

事实证明,该问题是AWS的关键问题,也是我使用此处在网络上被阻止的端口引起的网络安全问题。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM