A snippet of code to enumerate all Amazon S3 bucket items.
import boto3
def get_boto3_s3_client(region='us-east-2'):
"""Get a Boto3 S3 client using the local credential
file s3sak.txt and return it.
"""
with open('s3sak.txt', 'r') as fd:
lines = list(fd.readlines())
access_key = lines[0].strip()
secret_access_key = lines[1].strip()
c = boto3.client(
service_name='s3',
region_name=region,
aws_access_key_id=access_key,
aws_secret_access_key=secret_access_key
)
return c
def main():
s3c = get_boto3_s3_client()
bucket_name = 'radio248'
for node in s3_enumerate(s3c, bucket_name):
print(node['Key'])
def s3_enumerate(s3c, bucket_name):
start_after = None
while True:
if start_after is None:
resp = s3c.list_objects_v2(
Bucket=bucket_name,
)
else:
resp = s3c.list_objects_v2(
Bucket=bucket_name,
StartAfter=start_after
)
nodes = resp['Contents']
for node in nodes:
yield node
if len(nodes) == 1000:
start_after = nodes[-1]['Key']
else:
break
if __name__ == '__main__':
main()
Leave a Reply