AWS S3 creating,listing and deleting buckets using python SDK(boto3)

By | 15th January 2019

One of the way S3 buckets can be managed is using boto3 – python SDK for AWS. 

Documentation: 
https://boto3.amazonaws.com/v1/documentation/api/latest/index.html#

The following examples helps to get started on the S3 with boto3.

We will be using Ubuntu 16.04 (LTS) for installing boto3, awscli – useful for configuration of the credentials

1. Install boto3 on Ubuntu using pip
#pip install boto3

2. You can check if it is working properly by importing it.
stack@stackubaws:~$ python
Python 2.7.12 (default, Nov 19 2016, 06:48:10)
[GCC 5.4.0 20160609] on linux2
Type “help”, “copyright”, “credits” or “license” for more information.
>>> import boto3
>>> exit()
stack@stackubaws:~$

3. When using boto3 you can provide credentials in the python file but that is not a good and secure way.
We can use awscli to configure the password

stack@stackubaws:~$ sudo apt-get install awscli
Never use the password in the file.

4. Configure AWS CLI to use the your access id and access key
stack@stackubaws:~/.aws$ aws configure
AWS Access Key ID [****************]: Input your key id
AWS Secret Access Key [****************]: Input your access key
Default region name [us-east-1]:
Default output format [json]:

5. Check the files are created properly.
stack@stackubaws:~/.aws$ ls -ltr
total 8
-rw——- 1 stack stack 116 Jan 12 18:40 credentials
-rw——- 1 stack stack 43 Jan 12 18:40 config
stack@stackubaws:~/.aws$

6. Now we are ready to perform some operations on the S3 buckets
1. Create bucket
2. List all the buckets
3. Delete all the buckets

All the code has been uploaded to the following location.
https://github.com/rajeevrn/myscripts/tree/master/aws

Example:
:~/git/myscripts/aws$ python s3listbuckets.py

:~/git/myscripts/aws$ python s3createbucket.py
creation of the Bucket successful

:~/git/myscripts/aws$ python s3listbuckets.py
List of bucketsmybuckettemp11289656111330911

:~/git/myscripts/aws$ python s3deleteallbuckets.py
All Buckets are deleted

:~/git/myscripts/aws$ python s3listbuckets.py

Leave a Reply

Your email address will not be published. Required fields are marked *