Boto 3 Resource
Feb 4, 2022
Resources are a higher-level abstraction compared to clients. They are generated from a JSON resource description that is present in the boto library itself. E.g. this is the resource definition for S3. Resources provide an object-oriented interface for interacting with various AWS services.
How to Create Boto 3 Resource:-
session=boto3.Session(aws_access_key_id=’’,aws_secret_access_key=’’)#Provide your access key and secret access key
s3 = session.resource(‘s3’)
How to get list of Buckets in Boto 3 Resource:-
Bucket_List=[]
for bucket in s3.buckets.all():
Bucket_List.append(bucket.name)
#Using this we can get all list of buckets present
How to Create Bucket in Boto 3 Resouce:-
s3.create_bucket(Bucket=Bucket_Name,CreateBucketConfiguration={
'LocationConstraint':'us-west-1'})
print(f"Bucket Name '{Bucket_Name}'is Created")
How to Get Contents from Bucket in Resource:-
Bucket_Data=[]
try:
my_bucket = s3.Bucket(Bucket_Name)
except Exception as e:
print(e)
for my_bucket_object in my_bucket.objects.all():
Bucket_Data.append(my_bucket_object.key)