Upload flask FileStorage to S3

89 记录 , , 发表评论

Flask usesFileStorage as uploaded file object, it’s full package name is werkzeug.datastructures.FileStorage.

Following is the code to upload it to s3.

def upload_file(file_storage: FileStorage):
    if not file_storage.filename:
        raise ApiParamError("File can't be empty.")

    remote_path = secure_filename(file.filename)
    file_storage.seek(0)  # this is very important!!!!
    bucket.put_object(Body=file_storage, Key=remote_path, ContentType=file_storage.content_type)

    return remote_path

发表回复

您的电子邮箱地址不会被公开。 必填项已用 * 标注

昵称 *