Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
Demonstrate how to fill and empty an existing s3 bucket.
S3_ACCESS_KEY = 'foo'
S3_SECRET_KEY = 'foo'
S3_BUCKET = 'foo'
import boto3
from s3_constants import S3_ACCESS_KEY, S3_SECRET_KEY, S3_BUCKET
# empty existing bucket
def empty_s3_bucket():
client = boto3.client(
's3',
aws_access_key_id=S3_ACCESS_KEY,
aws_secret_access_key=S3_SECRET_KEY,
)
response = client.list_objects_v2(Bucket=S3_BUCKET)
if 'Contents' in response:
for item in response['Contents']:
print('deleting file', item['Key'])
client.delete_object(Bucket=S3_BUCKET, Key=item['Key'])
while response['KeyCount'] == 1000:
response = client.list_objects_v2(
Bucket=S3_BUCKET,
StartAfter=response['Contents'][0]['Key'],
)
for item in response['Contents']:
print('deleting file', item['Key'])
client.delete_object(Bucket=S3_BUCKET, Key=item['Key'])
empty_s3_bucket()
import boto3
from s3_constants import S3_ACCESS_KEY, S3_SECRET_KEY, S3_BUCKET
# fill bucket with objects, assumes bucket exists already
def fill_s3_bucket(num_of_objects):
client = boto3.client(
's3',
aws_access_key_id=S3_ACCESS_KEY,
aws_secret_access_key=S3_SECRET_KEY,
)
for i in range(num_of_objects):
print('creating file', '{:04d}'.format(i))
client.put_object(Bucket=S3_BUCKET, Body=b'foo', Key='file_' + '{0:04d}'.format(i))
fill_s3_bucket(3000)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment