Boto3 + Flask app,没有错误,但文件不会上传到s3存储桶?

时间:2015-12-11 22:31:25

标签: python amazon-web-services amazon-s3 flask boto3

正如标题所说,我正在运行烧瓶,以便将文件上传到s3存储桶。一切顺利,即使是在我的浏览器的底角显示上传的点,但我的桶中没有任何内容!

App.py如下 -

from flask import Flask, request, send_file, render_template
from werkzeug.utils import secure_filename
import config as cfg
import boto3, os, time
from boto3.s3.transfer import S3Transfer

session = boto3.session.Session(aws_access_key_id='AKIA*********KQ', aws_secret_access_key='xtrtC***********BaYqv9f9q', region_name='us-west-2')
s3 = session.resource(service_name='s3')

AWS_BUCKET = ('flaskbucket')
app = Flask(__name__)
app.debug = False

@app.route("/upload", methods=['POST', 'GET'])
def upload():

    uploaded_files = request.files.getlist("files[]")
    for upload_file in uploaded_files:
        if upload_file and allowed_file(upload_file.filename):
            filename = secure_filename(str(time.time()) + upload_file.filename)

            dir_name = 'uploads/'
            if not os.path.exists(dir_name):
                os.makedirs(dir_name)

            file_path = os.path.join(dir_name, filename)

            app.logger.info("Saving file: %s", file_path)

            upload_file.save(file_path)
            transfer = S3Transfer(boto3.client(s3, cfg.AWS_REGION, aws_access_key_id=cfg.AWS_APP_ID,
                                               aws_secret_access_key=cfg.AWS_APP_SECRET))

            transfer.upload_file(file_path, AWS_BUCKET, file_path)

    return render_template('index.html')


def allowed_file(filename):
    return not (not ('.' in filename) or not (filename.rsplit('.', 1)[1] in cfg.ALLOWED_EXTENSIONS))

    @app.after_request
def after_request(response):
    response.headers.add('Access-Control-Allow-Origin', '*')
    response.headers.add('Access-Control-Allow-Headers', "Authorization, Content-Type")
    response.headers.add('Access-Control-Expose-Headers', "Authorization")
    response.headers.add('Access-Control-Allow-Methods', "GET, POST, PUT, DELETE, OPTIONS")
    response.headers.add('Access-Control-Allow-Credentials', "true")
    response.headers.add('Access-Control-Max-Age', 60 * 60 * 24 * 20)
    return response

if __name__ == "__main__":
    app.run('0.0.0.0')

调试时没有错误,但是当我使用AWS记录存储桶时,我确实从烧瓶应用程序获得输入。我想我只是不确定如何前进而没有任何迹象表明出现了什么问题。

谢谢!

编辑: AWS日志文件向我显示了这个 -

<Error>
<Code>AccessDenied</Code>
<Message>Access Denied</Message>
<RequestId>13707ECF15B9DCE6</RequestId>
<HostId>...</HostId>
</Error>

0 个答案:

没有答案