Boto3 SDK use cases

Posted By : Anshuman Abhishek | 11-Jan-2021

Amazon Web Services (AWS) SDK for Python is known as Boto. With Boto, Python developers can create, configure, and manage AWS services, such as EC2 and S3. It is easy to use, has an object-oriented API, and provides low-level access to AWS services.

 

How To install Boto3

 

python2.7- pip install boto

python3.x- pip3 install boto3

 

Install AWS in Python

 

pip install AWS

 

You can use this if you have an AWS account, else you can opt for localstack.

 

Also Read: Execute Lambda Functions on S3 Event Triggers

 

Let’s configure the AWS account

 

aws configure

AWS Access Key ID [None]: yourAccessKeyID

AWS Secret Access Key [None]: yourAccessKey

Default region name [None]: yourRegionName ex.us-west-2

Default output format [None]: json

 

Creating a bucket in S3

 

aws s3 mb s3://mybucketoutput- 
make_bucket: my-bucket

Now let’s go to the main code

 

Also Read: Deploying NodeJS Application On AWS Lambda

 

Listing all the buckets in s3

 

import boto3s3 = boto3.resource('s3')for bucket in s3.buckets.all():
    print(bucket.name)output-
my-bucket
my-bucket1
my-bucket2

 

Listing all Keys(In common language folder and files)


import boto3s3 = boto3.client('s3')
bucket = 'my-bucket'
prefix = 'dir1/sub-dir1/'
for obj in s3.list_objects_v2(Bucket=bucket, Prefix=prefix)['Contents']:
    print(obj['Key'])output- 
dir1/sub-dir1/s3-file.txt

 

Reading a file from S3

 

bucket = 'my-bucket'
prefix = 'dir1/sub-dir1/s3-file.txt'
s3 = boto3.resource('s3')
obj = s3.Object(bucket, prefix)
body = obj.get()['Body'].read()
print(body)Output-
b'This is a sample text file.\n'

 

Also Read: Integrate AWS Amplify Cognito in Android

 

Downloading a file from S3


s3 = boto3.client('s3')
bucket = 'my-bucket'
s3_file = 'dir1/sub-dir1/s3-file.txt'
to_be_downloaded_file = 'to_be_downloaded_file.txt'
s3.download_file(bucket, s3_file, to_be_downloaded_file)

 

In the above method, we have the Bucket, you want to read data from as the first parameter. The second is the key name (filename with folder) with extension, and the last one is the file name when downloaded to the local system.

 

You might be thinking that why did I use the client and resource at some places.

 

Here's why.

 

Client

  • Low-level AWS service access
  • Generated from AWS service description
  • Exposes botocore client to the developer
  • Usually maps 1:1 with the AWS service API
  • Clients support every AWS application
  • Snake-cased method names (e.g. ListBuckets API => list_buckets method)

Resource

  • Higher-level, object-oriented API
  • Generated from resource description
  • Uses identifiers and attributes
  • Has actions (operations on resources)
  • Exposes subresources and collections of AWS resources
  • Fails to provide 100% API coverage of AWS services

 

 

Avail Our AWS Development Services To Strengthen Your Enterprise Processes

 

We are a 360-degree cloud app development company that provides end-to-end AWS application development services for varied business needs. Our development team is experienced in using and implementing a variety of Amazon Web Services such as AWS Lambda, CloudFront, ElasticSearch, Amazon S3, Amazon EC2, Kinesis, Amazon ECS, and more. For project-related queries, reach us out at [email protected]m

About Author

Author Image
Anshuman Abhishek

He has experience in Linux, Kubernetes, and different open source application. He has also worked on different clouds like IBM and AWS.

Request for Proposal

Name is required

Comment is required

Sending message..