1
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

AWS S3に保存してあるファイルをPYTHONで直接読みこむ方法

Last updated at Posted at 2022-11-20

s3からファイルに書き込まない、ダウンロードしないで直接読み込んで何かする

CSVの場合

  • utf-8
  • csv.readerを使う
import csv
import io

import boto3

s3_client = boto3.client("s3")
csv_s3_object = s3_client.get_object(Bucket="bucket name", Key="key~csvのパス~")
with io.TextIOWrapper(
    io.BytesIO(csv_s3_object["Body"].read()), encoding="utf-8"
) as csv_file_io:
    reader = csv.reader(csv_file_io, delimiter=",")
    for record in reader:
        print(record[0], record[1])

jpgの場合

  • Image.openする
import os
from io import BytesIO

import boto3
from PIL import Image

s3_client = boto3.client("s3")
storage_bucketname = os.environ["xxxxx"]


def handler(event, context):
    print(event)
    arguments = event["arguments"]
    s3_object = s3_client.get_object(
        Bucket=storage_bucketname,
        Key=arguments["key"],
    )
    with BytesIO(s3_object["Body"].read()) as img_io:
        pillow_object = Image.open(img_io)


1
2
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
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?