Last active
April 25, 2017 21:15
-
-
Save morenoh149/8e59b00bc48d42803f5136edceb6a663 to your computer and use it in GitHub Desktop.
Demonstrate how to fill and empty an existing s3 bucket.
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
S3_ACCESS_KEY = 'foo' | |
S3_SECRET_KEY = 'foo' | |
S3_BUCKET = 'foo' |
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 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() |
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 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