Last active
January 5, 2021 17:54
-
-
Save bahrmichael/58158ffebe0a422fb24680f2831515b0 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import boto3 | |
from uuid import uuid4 | |
client = boto3.client('s3') | |
bucket_name = 'my-bucket-name' | |
categories = ['easy', 'medium', 'difficult'] | |
for category in categories: | |
# Create 100 records with a random key | |
for i in range(100): | |
key = str(uuid4()) | |
client.put_object(Body=str(i).encode(), Bucket=bucket_name, Key=f"{category}/{key}") | |
print(f"Inserted {key} for category {category}") | |
for category in categories: | |
# Read 3 records and print them | |
for i in range(3): | |
start_after = f"{category}/{uuid4()}" | |
list_response = client.list_objects_v2( | |
Bucket=bucket_name, | |
MaxKeys=1, | |
Prefix=category, | |
StartAfter=start_after | |
) | |
if 'Contents' in list_response: | |
key = list_response['Contents'][0]['Key'] | |
item_response = client.get_object( | |
Bucket=bucket_name, | |
Key=key | |
) | |
print({ | |
'Key': key, | |
'Content': item_response['Body'].read().decode('utf-8'), | |
'StartAfter': start_after | |
}) | |
else: | |
print("Didn't find an item. Please try again.") | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment