You can use Python boto3 client or resource to create a new S3 bucket in AWS. In the default configuration, you need to pass at least the new bucket name and the AWS region name.
Note: The S3 bucket name must be unique globally.
Step 1: Install and configure boto3
https://cloudaffaire.com/how-to-install-python-boto3-sdk-for-aws/
https://cloudaffaire.com/how-to-configure-python-boto3-sdk-for-aws/
https://pypi.org/project/argparse/
Step 3: Create a boto3 script that creates a new AWS S3 bucket
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 |
## Create a script in python boto3 cat << EOF > create_bucket.py import argparse import boto3 from botocore.exceptions import ClientError parser = argparse.ArgumentParser(description='Create a s3 bucket') parser.add_argument('--bucket_name', type=str, help='The name of the bucket, must be uniqe globally') parser.add_argument('--region', type=str, help='AWS region name the bucket will be created') args = parser.parse_args() def create_bucket(bucket_name, region=None): try: if region is None: s3_client = boto3.client('s3') s3_client.create_bucket(Bucket=bucket_name) else: s3_client = boto3.client('s3', region_name=region) location = {'LocationConstraint': region} s3_client.create_bucket(Bucket=bucket_name, CreateBucketConfiguration=location) print("S3 bucket created successfully!") except ClientError as e: print(e) bucket_name = args.bucket_name region = args.region create_bucket(bucket_name, region) EOF |
Step 3: Execute the script to create an S3 bucket using Python boto3
1 2 |
## Create a s3 bucket using python boto3 python3 create_bucket.py --bucket_name cloudaffaire1 --region ap-south-1 |