Boto 3 Client
1 min readFeb 4, 2022
Clients provide a low-level interface to the AWS service. Their definitions are generated by a JSON service description present in the botocore library. The botocore package is shared between boto3 as well as the AWS CLI.
How to Create Boto 3 Client:-
s3=boto3.client(‘s3’,aws_access_key_id=’Your Access Key’,
aws_secret_access_key=’Your Secret Access Key’)
How to get list of Buckets in Boto 3 Client:-
Response=s3.list_buckets()
Bucket_List=[bucket[“Name”] for bucket in Response[‘Buckets’]]
How to Create Bucket in Boto 3 Client:-
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 Client:-
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)
How to Create Folder in S3 Bucket Using Client:-
bucket_name = "Hrishikesh_Bucket"
#Specify the name which you want to give to your bucketfolder_name = "Hrishikesh's Work"
#Name of folder which you want to create inside buckets3.put_object(Bucket=bucket_name, Key=(folder_name+'/'))
#Creates folder
How to Upload File to Specific Folder in Bucket:-
file_path='Hrishikesh.png'
#name of the file if file is present in current working directory else full paths3.upload_file(file_path,bucket_name, '%s/%s' % (folder_name, 'Hrishikesh.png'))#Where file path is name of your file, bucket name is name of your bucket and folder_name is the name of folder in which you want to upload your file.