AWS
S3
lambda
boto3

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

More than 1 year has passed since last update.

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ドキュメント