Amazon Simple Storage Service (S3) is an object storage service that stores data as objects within buckets called S3 buckets. An S3 bucket is a storage location to hold files referred to as data objects.
Common operations for S3 includes creation of bucket, reading the data object from buckets and writing the data to buckets. One of the common way of performing all these operations is via boto3, which is a Python SDK for AWS.
Boto3 installation
We can install boto3 via pip install boto3
List existing buckets
import boto3
# Retrieve the list of existing buckets
s3 = boto3.client('s3')
response = s3.list_buckets()
# Output the bucket names
print('Existing buckets:')
for bucket in response['Buckets']:
print(f' {bucket["Name"]}')
S3 Bucket Creation
import logging
import boto3
from botocore.exceptions import ClientError
def create_bucket(bucket_name, region=None):
"""Create an S3 bucket in a specified region
If a region is not specified, the bucket is created in the S3 default
region (us-east-1).
:param bucket_name: Bucket to create
:param region: String region to create bucket in, e.g., 'us-west-2'
:return: True if bucket created, else False
"""
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)
except ClientError as e:
logging.error(e)
return False
return True