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. 


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()

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

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.

:~/git/myscripts/aws$ python

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

:~/git/myscripts/aws$ python
List of bucketsmybuckettemp11289656111330911

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

:~/git/myscripts/aws$ python