Flask Upload Image to S3 without saving it to local file system

北慕城南 提交于 2020-12-06 07:04:39

问题


I need to upload a a user submitted photo to an s3 bucket. However I keep getting the following error:

TypeError: expected str, bytes or os.PathLike object, not FileStorage

How am I able to store the file as string/bytes instead of FileStorage? The relevent code is as follows:

@user_api.route('upload-profile-photo', methods=['PUT'])
@Auth.auth_required
def upload_profile_photo():
  """
  Upload User Profile Photo
  """
  key = Auth.auth_user()
  bucket = 'profile-photos'
  content_type = request.mimetype
  image_file = request.files['file']
  client = boto3.client('s3',
                        region_name='sfo2',
                        endpoint_url='https://example.xxx.amazonaws.com',
                        aws_access_key_id=os.environ['ACCESS_KEY'],
                        aws_secret_access_key=os.environ['SECRET_KEY'])
  with open(image_file, "rb") as f:
    client.upload_fileobj(
        bucket,
        f,
        key,
        ExtraArgs={'ACL': 'public-read', 'ContentType': content_type}
    )

  return custom_response({'message': 'image uploaded'}, 200)

回答1:


iTo achieve that with a FileStorage, I use the method put_object():

from werkzeug import secure_filename

@user_api.route('upload-profile-photo', methods=['PUT'])
@Auth.auth_required
def upload_profile_photo():
    """
    Upload User Profile Photo
    """
    key = Auth.auth_user()
    bucket = 'profile-photos'
    content_type = request.mimetype
    image_file = request.files['file']

    client = boto3.client('s3',
                          region_name='sfo2',
                          endpoint_url='https://example.xxx.amazonaws.com',
                          aws_access_key_id=os.environ['ACCESS_KEY'],
                          aws_secret_access_key=os.environ['SECRET_KEY'])

    filename = secure_filename(image_file.filename)  # This is convenient to validate your filename, otherwise just use file.filename

    client.put_object(Body=image_file,
                      Bucket=bucket,
                      Key=filename,
                      ContentType=content_type)

    return custom_response({'message': 'image uploaded'}, 200)

Note the call to secure_filename() is optional (you can simply pass image_file.filename), but can be very handy to validate the filename. Otherwise it would be nice to add some exception handlings, but the rough idea is here: no need to open() the file (that would need to be stored locally).

I encourage to have a look at the documentation here, to understand the difference with upload_fileobj()



来源:https://stackoverflow.com/questions/55292986/flask-upload-image-to-s3-without-saving-it-to-local-file-system

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!