如何使用python flask将修改后的图像直接上传到s3 bucket
我试图简单地修改通过表单上传的图像(调整大小),然后直接上传到s3 bucket。下面我使用的示例在我将文件保存在本地时有效,但在尝试上载到s3时遇到问题如何使用python flask将修改后的图像直接上传到s3 bucket,python,python-3.x,amazon-s3,flask,python-imaging-library,Python,Python 3.x,Amazon S3,Flask,Python Imaging Library,我试图简单地修改通过表单上传的图像(调整大小),然后直接上传到s3 bucket。下面我使用的示例在我将文件保存在本地时有效,但在尝试上载到s3时遇到问题 def _image_resize(temp_path, file, image_base, extension): image = Image.open(file) wpercent = (image_base / float(image.size[0])) hsize = int((float(image.siz
def _image_resize(temp_path, file, image_base, extension):
image = Image.open(file)
wpercent = (image_base / float(image.size[0]))
hsize = int((float(image.size[1]) * float(wpercent)))
image = image.resize((image_base, hsize), Image.ANTIALIAS)
modified_file_path = os.path.join(
temp_path, file.filename + '.' + extension + '.png'
)
image.save(modified_file_path)
with open(modified_file_path, 'rb') as data:
upload_file_to_s3(data, Config.S3_BUCKET_NAME)
return
def upload_file_to_s3(file, bucket_name, acl="public-read"):
"""
Docs: http://boto3.readthedocs.io/en/latest/guide/s3.html
"""
try:
s3.upload_fileobj(
file,
bucket_name,
ExtraArgs={
"ACL": acl
}
)
except Exception as e:
print("Something Happened: ", e)
return e
return
使用
upload\u fileobj
功能在bot3
包中设置字节日期,如下所示:
import boto3
s3 = boto3.resource('s3')
bucket = s3.Bucket('mybucket')
obj = bucket.Object('mykey')
with open('filename', 'rb') as data:
obj.upload_fileobj(data)
如果必须使用AWS CLI和AWS configure设置API密钥,如AWS访问密钥和密钥,请参见更新功能。对我来说仍然不起作用。你应该提供更多信息,说明为什么不起作用,如果发生错误。上传文件obj需要字节类型而不是文件。如果你想上传文件,那么你需要上传文件。