SHARE
TWEET

Untitled

a guest Aug 13th, 2019 54 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import uuid
  2. import sys
  3. import boto3
  4.  
  5.  
  6. def create_bucket_name(bucket_prefix):
  7.     """Create a unique S3 bucket name"""
  8.     return ''.join([bucket_prefix, str(uuid.uuid4())])
  9.  
  10.  
  11. def create_bucket(bucket_prefix, s3_connection):
  12.     """Create a ready-to-go s3 bucket"""
  13.     session = boto3.session.Session()
  14.     current_region = session.region_name
  15.     bucket_name = create_bucket_name(bucket_prefix)
  16.     bucket_response = s3_connection.create_bucket(
  17.         Bucket=bucket_name,
  18.         CreateBucketConfiguration={'LocationConstraint': current_region}
  19.     )
  20.  
  21.     return bucket_name, bucket_response
  22.  
  23.  
  24. if __name__ == '__main__':
  25.     if sys.argv[1]:
  26.         bucket_prefix = sys.argv[1]
  27.     else:
  28.         exit()
  29.     s3_client = boto3.client('s3')
  30.     s3_resource = boto3.resource('s3')
  31.  
  32.     # Generate bucket
  33.     bucket_name, bucket_response = create_bucket(bucket_prefix, s3_resource)
  34.     print(bucket_name)
  35.     print(bucket_response)
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top