Created
December 11, 2020 04:15
-
-
Save AgnijDave/2a925660b9f16bbd12329d8bd19476b0 to your computer and use it in GitHub Desktop.
Python Boto3 Implementation with AWS Lambdas
This file contains hidden or 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 = boto3.resource('s3') | |
bucket = s3.Bucket('xyz_bucket') | |
your_bucketname = 'xyz_bucket' | |
## CHECK WHETHER A BUCKET EXISTS | |
if bucket.creation_date: | |
print('It Already Exists') | |
else: | |
try: | |
## This snippet will either create a new bucket or return the already existing bucket present in your account | |
b = s3.create_bucket(Bucket=your_bucketname) | |
## This exception is for a bucket already existing with a similar name for a different User | |
## A point to note is no two same users can have a bucket of the same name | |
## https://docs.aws.amazon.com/AmazonS3/latest/dev/BucketRestrictions.html | |
except Exception as e: | |
if e.response['Error']['Code'] == 'BucketAlreadyExists': | |
print('It Already Exists for another User, You will have to select another bucket name'') | |
else: | |
raise e | |
## PUT | |
s3object = s3.Object(your_bucketname, 'abc.json') | |
s3object.put( | |
Body=(bytes(json.dumps(Analytics).encode('UTF-8'))) | |
) | |
## DELETE | |
## first all elements present in the bucket will have to be deleted | |
b = bucket.objects.all().delete() | |
if b: | |
print(b) | |
response = client.delete_bucket(Bucket=your_bucketname) | |
print(response) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment