2017-06-22 102 views
0

我正在嘗試編寫一個Flask應用程序以上傳到AWS S3存儲桶。當我在PyCharm中本地運行它時,它工作正常。但是,一旦我把它部署到AWS(部署在端口80上的瓶的應用程序),我現在得到一個錯誤......嘗試從Flask應用程序中的EC2實例將文件上傳到Amazon S3時出現ClientError(SignatureDoesNotMatch)

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上。

任何幫助?這裏是我的代碼...與OFC

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刪除鍵和URL

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 

回答

0

問題竟然是與AWS的一個關鍵問題,並使用被阻止在網絡上此端口我的網絡安全問題。

0

試着改變你的客戶對象:

client = boto3.client('s3', config=boto3.session.Config(signature_version='s3v4')) 
相關問題