[英]JPG files uploaded with lambda to S3 are corrupt
我有這個簡單的python lambda,可以下載JPG圖片並將其上傳到S3存儲桶。
url = 'https://somesite.com/11/frame.jpg?abs_begin=2019-08-29T05:18:26Z'
s3 = boto3.client('s3')
with contextlib.closing(requests.get(url, stream=True, verify=False)) as response:
fp = BytesIO(response.content)
s3.upload_fileobj(fp, bucket_name, 'my-dir/' + 'test_img.jpg')
但是,在我的存儲桶中查看時,文件大小為162個字節。 從瀏覽器GUI將其下載到我的本地磁盤macOS時提示: The file "test_img.jpg" could not be opened.
並且It may be damaged or use a file format that Preview doesn't recognise.
知道是什么原因造成的嗎?
您確定該網站正在為您提供JPEG文件嗎? 我建議以某種方式檢查response.status_code
,我通常只是在其中放一個raise_for_status()
並讓調用代碼處理異常
此外,如果您實際上正在流傳輸內容,則只需要傳遞stream=True
,就可以一次閱讀所有內容,並且請求流是浪費的。 建議使用流式傳輸,否則您需要將整個文件保存在內存中,這可能會浪費
如果要檢查自己是否正在獲取圖像,可以在上載到S3之前使用Pillow
打開圖像,例如:
import tempfile
import requests
from PIL import Image # pip install -U Pillow
# dummy image
url = 'https://picsum.photos/id/1053/1500/1000'
# get a temp file in case we get a large image
with tempfile.TemporaryFile() as fd:
with requests.get(url, stream=True) as response:
# make sure HTTP request succeeded
response.raise_for_status()
for data in response.iter_content(8192):
fd.write(data)
# seek back to beginning of file and load to make sure it's OK
fd.seek(0)
with Image.open(fd) as img:
# will raise an exception on failure
img.verify()
print(f'got a {img.format} image of size {img.size}' )
# let S3 do its thing
s3.upload_fileobj(fd, bucket_name, 'my-dir/test_img.jpg')
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.