Creating an S3 Bucket with Specified Name

  • Share this:

Code introduction


This function creates an S3 bucket with the specified name. If the bucket already exists, an exception will be raised.


Technology Stack : boto3

Code Type : The type of code

Code Difficulty : Intermediate


                
                    
import boto3
import random

def create_random_s3_bucket(bucket_name):
    """
    This function creates a random S3 bucket with the given name.
    """
    s3 = boto3.client('s3')
    try:
        s3.create_bucket(Bucket=bucket_name)
        print(f"Bucket '{bucket_name}' created successfully.")
    except Exception as e:
        print(f"Error: {e}")

# JSON Explanation                
              
Tags: