LoginSignup
6
7

More than 5 years have passed since last update.

Lambdaで特定のBucket(S3)にある全Objectリストを取得する方法

Posted at

list_objecthsで取得できるのは一度に1000件なので、while で回す

s3_objects.py
import boto3

BUCKET_NAME = ''


def list_handler(event, context):
    s3_client = boto3.client('s3')

    contents = []
    next_token = ''
    while True:
        if next_token == '':
            response = s3_client.list_objects_v2(Bucket=BUCKET_NAME)
        else:
            response = s3_client.list_objects_v2(Bucket=BUCKET_NAME, ContinuationToken=next_token)

        contents.extend(response['Contents'])
        if 'NextContinuationToken' in response:
            next_token = response['NextContinuationToken']
        else:
            break

    print(contents)
    print(len(contents))

参考:BotoAPIドキュメント

6
7
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
6
7