AWS Lambda python boto3でS3のファイル一覧を出力する関数

通常はaws cliのaws s3 lsで事足りるのですが、リストをもとに処理を行う場合shellで実現するよりPythonで処理したいケースがあるため、作成しました。

Lambda pythonコード

import json
import boto3

BUCKET_NAME = 'S3バケット名'

def lambda_handler(event, context):
    s3c = boto3.client('s3')
    next_token = ''
    while True:
        if next_token == '':
            response = s3c.list_objects_v2(Bucket=BUCKET_NAME,Prefix='xxxx')
        else:
            response = s3c.list_objects_v2(Bucket=BUCKET_NAME,Prefix='xxxx', ContinuationToken=next_token)
        for content in response['Contents']:
            keyn = content['Key']
            prt = keyn.split('/')
            print(prt)
        if 'NextContinuationToken' in response:
            next_token = response['NextContinuationToken']
        else:
            break

Prefixは、調べたいフォルダが決まっている場合指定してください。