我想從網上獲取圖像並將其上傳到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