DEV Community

Cover image for Interact with AWS S3 using boto3, AWS's SDK for Python . Part 1

Interact with AWS S3 using boto3, AWS's SDK for Python . Part 1

Boto3 is an amazon AWS SKD (Software Development Kit ) for Python. Using boto3, it is possible to create , update and delete resources on AWS S3 and EC3 by writing the appropriate Python's Scripts. Here in this blogpost, I will be focusing Amazon Simple Storage Service S3 and show how to create and S3 Bucket. Boto3 is fully maintained and published by Amazon Web Services.
To learn more about Boto3 , please check the link here.

Bellow are the prerequisites

  • Have an aws account
  • Lunch an EC2 Instance and SSH into it.
  • Install Python3
  • Install pip
  • Install Boto3

Installing python

sudo amazon-linux-extras install python3.8
Enter fullscreen mode Exit fullscreen mode

** Installing pip **
Once you ssh into your EC2 instance , run the command bellow

sudo yum -y install python-pip

Enter fullscreen mode Exit fullscreen mode

There is no need to install aws cli here since it already comes preinstalled when using amazon linux 2 EC2 instance.
Once installed you can check the version by running the code bellow

aws --version 
Enter fullscreen mode Exit fullscreen mode

Installing Boto3

pip install boto3
Enter fullscreen mode Exit fullscreen mode

Once you have boto3 installed you can also check the version by running the command bellow

pip list
Enter fullscreen mode Exit fullscreen mode

Once you have create the appropriate role and attached it to the instance , you should be able to make some call base on the permission that you passed to the instance via the AIM role

Create an IAM role and attach it to your ec2 instance. On that role you will specify the permission needed to complete your tasks which in here is to interact with aws S3 .I granted full access control to that role

Create a new role

  1. Select your instance
  2. Click on the drop down arrow and select security and then Modify the IAM Role and then chose the new IAM role you created

Here I have create an new role and granted it Full Access to S3 and now I should be able to get some response back from my aws resources.

Run the following command and to see the List all the buckets
you have in your account account

aws s3 ls
Enter fullscreen mode Exit fullscreen mode

Image description

Create a new Bucket named volt123zzzz

aws s3 mb s3://volt123zzzz

Enter fullscreen mode Exit fullscreen mode

bucketBoto3
As you can see a new bucket named(volt123zzzz) was created.

Remove bucket named volt123zzzz

aws s3 rb s3://volt123zzzz

Enter fullscreen mode Exit fullscreen mode

Once run. the newly created bucket (volt123zzzz) will be removed

In later posts, I will dive deep in and show more functionalities and of aws and boto2

Lets link up on twitter @asksouley

Top comments (0)