Skip to content

Instantly share code, notes, and snippets.

@morenoh149
Last active April 25, 2017 21:15
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save morenoh149/8e59b00bc48d42803f5136edceb6a663 to your computer and use it in GitHub Desktop.
Save morenoh149/8e59b00bc48d42803f5136edceb6a663 to your computer and use it in GitHub Desktop.
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