You can download this code by clicking the button below.
This code is now available for download.
This function creates a specified AWS S3 bucket. If the bucket already exists or an error occurs during the creation process, the function returns the corresponding status and message.
Technology Stack : boto3 (for AWS service interaction), S3 (Amazon Simple Storage Service)
Code Type : The type of code
Code Difficulty : Intermediate
import boto3
import random
def create_random_bucket(bucket_name, region_name='us-west-2'):
"""
This function creates a random bucket in a specified AWS region.
"""
s3 = boto3.client('s3', region_name=region_name)
try:
s3.create_bucket(Bucket=bucket_name)
return {"status": "success", "message": f"Bucket {bucket_name} created in {region_name}"}
except Exception as e:
return {"status": "error", "message": str(e)}
# JSON output