在s3存储桶中创建的文件,但文件为空

时间:2018-09-27 03:20:11

标签: python-3.x amazon-s3 boto3

从浏览器上传文件时。在s3存储桶中创建了文件名,但文件内容为空。

    @app.route("/", methods=['GET', 'POST'])
    def index():
        if request.method == 'POST':
            file = request.files['file']
            file.filename = time.strftime("%Y%m%d-%H%M%S") ## filename=TIMESTAMP

            # Read the contents of the file
            file_contents = file.open()
            s3.Object('mybucket', file.filename).put(file_contents)

            # Connect to s3 bucket
            s3=boto3.resource('s3')
        return render_template('index.html')

1 个答案:

答案 0 :(得分:0)

更改此内容将其修复:

file_contents = file.open() ---> file_contents = file.read()

s3.Object('mybucket', file.filename).put(file_contents) --> s3.Object('mybucket', file.filename).put(Body=file_contents)
相关问题