I see a procedure to upload a locally saved image file to S3, but as the title suggests, this article aims to upload the downloaded image directly without saving it to a file.
By passing the binary data obtained by requests.get
to ʻio.BytesIO`, it will be treated as a file object.
import requests
import io
import boto3
#Download image
res = requests.get('Image URL')
res.raise_for_status()
#Convert the acquired binary data to a file object
img = io.BytesIO(res.content)
#Upload to S3
s3 = boto3.client('s3')
s3.upload_fileobj(img, 'bucket_name', 's3/path')
Recommended Posts