Just one reason to prefer Python, the AWS S3 SDK explained

And as promised, here is my little surprise. For those of you who follow me, but also for you that visit this website for the first time: here will follow an important consideration.

Whether you are an experienced developer and want to enhance your career, or a newbie, passionate about programming and want to make a living and find job opportunities in the field of software development, take into consideration what I have to say to you.

The Python programming language

It may seem nothing, this little gift I intend to share with you, but trust me, it is the result of many years of experience and work in the tech field, so please take a moment to listen to me.

Python is the most promising programming language in this sector and has applications and libraries that will fulfill any needs, even for those of you that take business very seriously and want reliable applications such as those written in Java: the ecosystem is now mature.

So I think it is worthwhile to take the time to learn it and you can begin by this tutorial, which will give you an example application to start with: have fun playing with it.

A prerequisite for this tutorial is the installation and configuration of the AWS CLI so you can configure your credentials and verify what you have created with our Python code, this previous tutorial can give you all the needed details.

AWS CLI commands for S3 are not mentioned here for brevity, check them out at the AWS CLI Command Reference for S3. Otherwise you can look at your web console to see the result of our Python scripts.

Another important requirement is a Python 3 installation, this tutorial is based on 3.5.1 on a Windows machine with pip, the package manager.

There are other versions that may be compatible, and different installation methods depending on your actual OS, read the download page for further informations.

Once you’re done simply install the SDK by means of pip, then choose a path inside your hard drive for our project, like so:

C:\>pip install boto3
C:\>mkdir aws_s3_python
C:\>cd aws_s3_python

Let’s begin by creating our ‘create_buckets.py‘ file which will simply ask for a Bucket name and create it, make sure you set ‘eu-central-1‘, when configuring your CLI:

import boto3
 
 
s3 = boto3.client('s3')
 
bucket_name = input("Please enter a name for your Bucket: ")
 
response = s3.create_bucket(
    ACL = 'private',
    Bucket = bucket_name,
    CreateBucketConfiguration = {
        'LocationConstraint': 'eu-central-1'
    }
)

We now want the capability of showing all of our newly created Buckets, for that purpose we create a ‘show_buckets.py‘ file:

import boto3
 
 
s3 = boto3.client('s3')
 
all_buckets = s3.list_buckets()
 
for bucket in all_buckets['Buckets']:
    print (bucket['Name'])

Let’s then create the opportunity to put files inside our Bucket, we will create a new file with the name of ‘put_files.py‘:

import boto3
 
 
s3 = boto3.client('s3')
 
bucket_name = input("Please enter the name of your Bucket: ")
file_name = input("Please enter the name of your file: ")
 
file_handler = open(file_name, "rb") 
 
response = s3.put_object(
    ACL = 'private',
    Body = file_handler,
    Bucket = bucket_name,
    Key = file_name
)

Let’s delete our files. The script will be called, you guessed it, ‘delete_files.py‘:

import boto3
 
 
s3 = boto3.client('s3')
 
bucket_name = input("Please enter the name of your Bucket: ")
file_name = input("Please enter the name of your file: ")
 
response = s3.delete_object(
    Bucket = bucket_name,
    Key = file_name
)

The last thing to do is to create something that will delete our Buckets, even if they’re not empty. In that case the script will delete all the content first, let’s name it ‘delete_buckets.py‘:

import boto3
 
 
s3 = boto3.client('s3')
 
bucket_name = input("Please enter the name of your Bucket: ")

objects_list = s3.list_objects(
    Bucket = bucket_name
)
 
if 'Contents' in objects_list:
 
    object_keys = []
     
    for object in objects_list['Contents']:
        object_keys.append({'Key': object['Key']})
     
    response = s3.delete_objects(
        Bucket = bucket_name,
        Delete = {
            'Objects': object_keys
        }
    )
 
response = s3.delete_bucket(
    Bucket = bucket_name
)

And that’s all, please give me feedback if you have any difficulties in following the tutorial. I will do all I can to make it better.

The code is self explanatory and you can always take a look at the repository on GitHub for this tutorial. I bet you know I’m looking forward to see you for the next tutorial, till then, goodbye and take care.

Did you like this post? Please comment here below and share it on your preferred social networks, thank you!

Published by

Mirko Benedetti

Hi there, my name is Mirko Benedetti and I'm a Software Developer. Excellence is what I consider to be our ultimate goal, and passion for technology constantly drives me to it. I began programming self-taught at a very young age; since then I learned a lot and every day I love learning new things. Here is my Linkedin Curriculum, feel free to connect.

4 thoughts on “Just one reason to prefer Python, the AWS S3 SDK explained”

  1. I don’t think Python will have a bright future
    I must disagree with you…

  2. It’s true, people talk a lot..
    You must give Python a try, then you will tell me

Leave a Reply

Give me your opinion, I will be grateful.