leemengtaiwan
10/16/2017 - 1:15 PM

Flask寫images到S3並存到本地端

<form method=post enctype=multipart/form-data>
  <input type=file name=file>
  <input type=submit value=Upload>
</form>

記得要用file.seek(0)

References:

import boto3

@app.route('/', methods=['GET', 'POST'])
def make_prediction():
    file = request.files['file']
    
    # save image to S3
    client = boto3.client('s3',
        aws_access_key_id='aws_access_key_id',
        aws_secret_access_key='aws_secret_access_key'
    )
    
    client.put_object(
        Body=file.read(),
        Bucket=S3_BUCKET_NAME,
        Key=filename)

    # save image locally
    file.seek(0) # re-read the whole file from the start
    file_path = os.path.join(app.config['UPLOAD_FOLDER'], filename)
    file.save(file_path)