Skip to main content

AWS Manage S3 Buckets using Python Boto3

 In this post, I will show you 

how to create S3 bucket, 

how to put objects into the bucket, 

how to upload multiple objects in s3, 

how to download multiple objects, 

how to control access policy, and 

how to host a static website in S3. 


1. How to create S3 Bucket using Python3 Boto3?

Object : resource method: create_bucket 

important method  parameters: 
 ACL : private or public 
 Bucket - name of the bucket name this should be unique for each bucket 
 CreateBucketConfiguration - have the LocationConstrating that is region on which you want to host your s3 bucket.
import boto3
s3_resource = boto3.resource('s3')
bucket = s3_resource.create_bucket(ACL='private',
			Bucket='vybhava2023demo.com',
			CreateBucketConfiguration={
			'LocationConstrating': 'us-west-2'
			}) 
			
print("Successfully create bucket:", bucket)

2. How to put the objects into the S3 Bucket using Boto3

When you want to add file from local system we can use Python file methods and have a object reference here f is the reference to the file opened for read operation.
f=open('greet.txt').read()
s3_client =boto3.client('s3')
respose = s3_client.put_object(
	ACL='private',
	Body=f,
	Bucket='vybhava2023demo.com',
	Key='greet.txt'
	)

3. Delete object from S3 Bucket

Delete object from S3 bucket 
Object : client 
method : delete_object()
s3_client = boto3.client('s3')
response = s3_client.delete_object(
	Bucket='vybhava2023demo.com',
	Key='greet.txt'	
)

4. List all content objects in a Bucket

List all the contents of a Bucket this may be file objects those are put into the Bucket earlier.
Object: client
method: list_objects
s3_client = boto3.client('s3')
response = s3_client.list_objects(
	Bucket='vybhava2023demo.com'
	)

for content in response['Contents']:
	print(content['Key'])

5. List all S3 Buckets

Get the s3_client object 
method: list_buckets : 
On the aws-cli run the command: aws s3 ls
# File: list-s3.py 
# Description: This script will list all s3 buckets using client interface

s3_client = boto3.client('s3')
list_buckets=s3_client.list_buckets()
print(list_buckets)

# run 2
for b in list_buckets['Buckets']:
	print(b['Name'])
Hope you enjoyed this post!! Please write back your errors and exceptions when you run the Boto3 programs for AWS services and resources.

Comments

Popular posts from this blog

Python Interview Questions -Coding Snipets

 This post is dedicated for all DevOps Engineer, Software Engineers who are preparing for Coding Interviews,  Overview of Coding Interviews Most Companies looking for People with minimum Coding knowledge. In a coding interview, you will be given a small problem to solve within 10 - 20 minutes online screen or in-person on their system. In the question, you might be having some part of the code framed and you might be asked to write a snippet of code in between. You need to understand the code comments and proceed to build the expected snippet of code. Bigger companies look for the General purpose questions, where small companies look for specific questions. The General questions would be like this: Determine if the given word is a palindrome or not. (Example madam) Determine given number is prime or not. How to prepare for a coding interview? Now we have the flexibility to choose the programming language on which you are comfortable.  In general DevOps Infra guys will be ...

Python Sets and Dictionaries

Python Sets Python Maps examples A mapping object maps hashable values to arbitrary objects. Mappings are mutable objects. There is currently only one standard mapping type, the dictionary. Dictionaries consist of pairs (called items) of keys and their corresponding values. Dictionaries can be created by placing a comma-separated list of key: value pairs within curly braces Keys are unique within a dictionary while values may not be. The values of a dictionary can be of any type, but the keys must be of an immutable data type such as strings, numbers, or tuples. Python Set Operations and methods Python Set methods continueed >>> SET={'new','old','list','new'} >>> SET set(['new', 'old', 'list']) Lets play with set operations, it is more like school math here... >>> s={1,2,3,4} >>> k={3,4,5,6} >>> len(s) 4 >>> len(k) 4 >>> 1 in s True >>...

Program Controls and loops in Python

Python Programming controls The flow of program will be controlled with conditional statements where this flow  if condition if-else condition if-elif condition while loop while - else loop for loop for with if- else loop if condition Here I would like to work on program control statements, wherein relate some operating system functionalities so that, this would give some basic prototype for the sys admin scripts. #!/usr/bin/python # This illustrate if condition # Filename : ifex.py import os d=os.listdir('/home/pavanbsd/pybin') print d f='ifex.py' if f in d: print "File exists" else: print "Not found..." if-elif-else ladder No worries, coming soon... When you need the task that need to be done repeatedly then your choice is using loops. Python provides two simple loop constructs easy to use! More powerful then other scripting compare to, it has 'else' block. while loop Understand the power of while loop,...