Last active
January 5, 2021 17:54
-
-
Save bahrmichael/cdf1bb3c560cadc9d89cfadc5e3d7c51 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' | |
# 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=key) | |
print(f"Inserted {key}") | |
# Read 3 records and print them | |
for i in range(3): | |
list_response = client.list_objects_v2( | |
Bucket=bucket_name, | |
MaxKeys=1, | |
StartAfter=str(uuid4()), | |
) | |
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') | |
}) | |
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