我想从网上获取图像并将其上传到Amazon s3。虽然这样做,我想检查图像尺寸。 我在Python 3以下代码:将PIL图像对象上传到Amazon s3 python
from PIL import Image
import requests
# Get response
response = requests.get(url, stream= True)
# Open image
im = Image.open(response.raw)
# Get size
size = im.size
# Upload image to s3
S3.Client.upload_fileobj(
im, # This is what i am trying to upload
AWS_BUCKET_NAME,
key,
ExtraArgs={
'ACL': 'public-read'
}
)
的问题是,PIL图像对象不支持读取。当我尝试上传PIL图像对象im
时出现以下错误。
ValueError: Fileobj must implement read
它工作时,我只是尝试上传'response.raw',但我需要得到的图像尺寸。如何将PIL图像对象更改为文件类对象?有没有更容易的方式来获得尺寸,同时仍然能够上传图像到S3?
所以问题是,在获取图像的尺寸后,如何将图像上传到s3?
这并不能改变什么。图像打开正常。当我想将图像上传到s3时,会出现问题。 – john
您是否尝试将'f'加载到aws?用'f'代替'im' – AndMar
是的,我试过了。首先,'response'没有一个名为'read'的属性,我假设你的意思是'response.raw.read()'。当我尝试这样做时,我上传了一个空的file_obj,因为当用Image.open()打开时,f已被清空。 – john