flask upload file to s3

Solutions on MaxInterview for flask upload file to s3 by the best coders in the world

showing results for - "flask upload file to s3"
Dianna
10 Jun 2017
1from werkzeug import secure_filename
2
3@user_api.route('upload-profile-photo', methods=['PUT'])
4@Auth.auth_required
5def upload_profile_photo():
6    """
7    Upload User Profile Photo
8    """
9    key = Auth.auth_user()
10    bucket = 'profile-photos'
11    content_type = request.mimetype
12    image_file = request.files['file']
13
14    client = boto3.client('s3',
15                          region_name='sfo2',
16                          endpoint_url='https://example.xxx.amazonaws.com',
17                          aws_access_key_id=os.environ['ACCESS_KEY'],
18                          aws_secret_access_key=os.environ['SECRET_KEY'])
19
20    filename = secure_filename(image_file.filename)  # This is convenient to validate your filename, otherwise just use file.filename
21
22    client.put_object(Body=image_file,
23                      Bucket=bucket,
24                      Key=filename,
25                      ContentType=content_type)
26
27    return custom_response({'message': 'image uploaded'}, 200)
28
29