1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 |
## List contents of S3 bucket from boto3 import client bucket_name = "my_bucket" prefix = "my_key/sub_key/lots_o_files" # put / to list all entries s3_conn = client('s3') ## Assumes boto.cfg setup https://boto3.amazonaws.com/v1/documentation/api/latest/guide/configuration.html s3_result = s3_conn.list_objects_v2(Bucket=bucket_name, Prefix=prefix, Delimiter = "/") if 'Contents' not in s3_result: #print(s3_result) return [] file_list = [] for key in s3_result['Contents']: file_list.append(key['Key']) print(f"List count = {len(file_list)}") while s3_result['IsTruncated']: continuation_key = s3_result['NextContinuationToken'] s3_result = s3_conn.list_objects_v2(Bucket=bucket_name, Prefix=prefix, Delimiter="/", ContinuationToken=continuation_key) for key in s3_result['Contents']: file_list.append(key['Key']) print(f"List count = {len(file_list)}") return file_list |
The script is contributed by: https://stackoverflow.com/users/985088/hephaestus