get_all_s3_keys
Get all S3 Keys by Prefix by Boto3
1
def scan_s3_objects(bucket, prefix):
2
"""
3
Get a list of all keys in an S3 bucket.
4
5
Args:
6
- bucket: S3 bucket name
7
- prefix: S3 prefix to be scanned
8
Returns:
9
- List of objects.
10
"""
11
s3 = boto3.client('s3')
12
keys = []
13
14
kwargs = {'Bucket': bucket, 'Prefix': prefix}
15
while True:
16
resp = s3.list_objects_v2(**kwargs)
17
if 'Contents' not in resp:
18
return []
19
20
for obj in resp['Contents']:
21
keys.append(obj['Key'])
22
23
try:
24
kwargs['ContinuationToken'] = resp['NextContinuationToken']
25
except KeyError:
26
break
27
28
return keys
Copied!
Last modified 1yr ago
Copy link