Skip to content

Commit

Permalink
Finalized s3 code example for Cloud9 tutorial.
Browse files Browse the repository at this point in the history
  • Loading branch information
Laren-AWS committed Dec 9, 2019
1 parent fde2384 commit ee4d97d
Showing 1 changed file with 17 additions and 12 deletions.
29 changes: 17 additions & 12 deletions python/example_code/s3/s3.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,32 +14,35 @@
# snippet-service:[s3]
# snippet-keyword:[Python]
# snippet-sourcesyntax:[python]
# snippet-sourcesyntax:[python]
# snippet-keyword:[Amazon S3]
# snippet-keyword:[Code Sample]
# snippet-keyword:[list_buckets]
# snippet-keyword:[create_bucket]
# snippet-keyword:[delete_bucket]
# snippet-sourcetype:[full-example]
# snippet-sourcedate:[2017-01-19]
# snippet-sourceauthor:[AWS]
# snippet-sourcedate:[2019-12-04]
# snippet-sourceauthor:[AWS-C9]

# ABOUT THIS PYTHON SAMPLE: This sample is part of the SDK for Cloud9 User Guide topic at
# https://docs.aws.amazon.com/cloud9/latest/user-guide/sample-python.html

# snippet-start:[s3.python.bucket_operations.list_create_delete]
import boto3
import sys
from botocore.exceptions import ClientError


def list_my_buckets(s3):
print("Buckets:\n", *[b.name for b in s3.buckets.all()], sep="\n\t")
print('Buckets:\n\t', *[b.name for b in s3.buckets.all()], sep="\n\t")


def create_and_delete_my_bucket(bucket_name, region, cleanup):
def create_and_delete_my_bucket(bucket_name, region, keep_bucket):
s3 = boto3.resource('s3', region_name=region)

list_my_buckets(s3)

try:
print(f"\nCreating new bucket: {bucket_name}\n")
print('\nCreating new bucket:', bucket_name)
bucket = s3.create_bucket(
Bucket=bucket_name,
CreateBucketConfiguration={
Expand All @@ -53,14 +56,14 @@ def create_and_delete_my_bucket(bucket_name, region, cleanup):
bucket.wait_until_exists()
list_my_buckets(s3)

if cleanup:
print(f"\nDeleting: {bucket.name}\n")
if not keep_bucket:
print('\nDeleting bucket:', bucket.name)
bucket.delete()

bucket.wait_until_not_exists()
list_my_buckets(s3)

# snippet-end:[s3.python.bucket_operations.list_create_delete]
else:
print('\nKeeping bucket:', bucket.name)


def main():
Expand All @@ -69,12 +72,14 @@ def main():
parser = argparse.ArgumentParser()
parser.add_argument('bucket_name', help='The name of the bucket to create.')
parser.add_argument('region', help='The region in which to create your bucket.')
parser.add_argument('--cleanup', help='Deletes the created bucket before exiting.', action='store_true')
parser.add_argument('--keep_bucket', help='Keeps the created bucket. When not specified, the bucket is deleted.',
action='store_true')

args = parser.parse_args()

create_and_delete_my_bucket(args.bucket_name, args.region, args.cleanup)
create_and_delete_my_bucket(args.bucket_name, args.region, args.keep_bucket)


if __name__ == '__main__':
main()
# snippet-end:[s3.python.bucket_operations.list_create_delete]

0 comments on commit ee4d97d

Please sign in to comment.