LoginSignup
1
4

More than 3 years have passed since last update.

requestsでダウンロードした画像を直接S3にアップロードする

Posted at

目的

ローカルに保存された画像ファイルををS3にアップロードする手順は見かけるのですが、本記事は表題の通り、ダウンロードした画像をファイルに保存することなく直接アップロードすることを目的としています。

方法

requests.getで取得したバイナリデータをio.BytesIOに渡すことでファイルオブジェクトとして扱います。

import requests
import io
import boto3

# 画像をダウンロード
res = requests.get('画像のURL')
res.raise_for_status()

# 取得したバイナリデータをファイルオブジェクトに変換
img = io.BytesIO(res.content)

# S3にアップロード
s3 = boto3.client('s3')
s3.upload_fileobj(img, 'bucket_name', 's3/path')
1
4
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
4