Upload the image downloaded by requests directly to S3

Purpose

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.

Method

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

Upload the image downloaded by requests directly to S3
Save the graph drawn by pyqtgraph to an image
Dot according to the image
I want to save the photos sent by LINE to S3
Image upload & download to Azure Storage. With Python + requests + REST API
I tried moving the image to the specified folder by right-clicking and left-clicking
Prepare a URL to display the image uploaded by Active Storage by yourself
Try to face the integration by parts
PUT gzip directly to S3 in Python
Use boto to upload / download files to s3.
Import by directly specifying the directory path
Upload scraped artifacts in Scrapy Cloud to S3
Try adding fisheye lens distortion to the image
Upload text file to rental server by ftp
Crop the image to rounded corners with pythonista
Transform the image by projective transformation-Hack the monitor screen-
I tried to get an image by scraping
I tried to correct the keystone of the image
How to erase the characters output by Python
Download the VGG Face2 dataset directly to the server
Upload images to S3 with GUI using tkinter
[Python] I will upload the FTP to the FTP server.
Log in by passing the login session cookie obtained by Python requests to the Selenium browser.