Boto3 Python

 

Boto3 is an AWS SDK for Python. It allows users to create, and manage AWS services such as EC2 and S3. It provides an object oriented API services and low level services to the AWS services. In this tutorial, you’ll. Create session in Boto3 Python Download files from S3 using Boto3 Python Download all from S3 Bucket using Boto3 Python. Boto3 documentation ¶ You use the AWS SDK for Python (Boto3) to create, configure, and manage AWS services, such as Amazon Elastic Compute Cloud (Amazon EC2) and Amazon Simple Storage Service (Amazon S3). The SDK provides an object-oriented API as well as low-level access to AWS services.

This guide details the steps needed to install or update the AWS SDK for Python. The SDK is composed of two key Python packages: Botocore (the library providing the low-level functionality shared between the Python SDK and the AWS CLI) and Boto3 (the package implementing the Python SDK itself). Boto3 documentation¶ You use the AWS SDK for Python (Boto3) to create, configure, and manage AWS services, such as Amazon Elastic Compute Cloud (Amazon EC2) and Amazon Simple Storage Service (Amazon S3). The SDK provides an object-oriented API as well as low-level access to AWS services. 🍎 🍺 🐍 Mac Brew installed Python notes. Brew wilfully breaks the -target option with no seeming intention to fix it which causes issues since this uses that option. There are a few easy workarounds for this: Install Python from python.org and specify it with the pythonBin option. Create a virtualenv and activate it while using.

In this lab, we are going to use Amazon Translate to translate some text from English to French.

First, look at the Boto3 documentation for Amazon Translate.

In the section marked client, it tells us how to use the translate service in our program.

In the Boto3 documentation for AWS Translate, we can see that it supports different methods. You can explore each of these if you wish. We are going to be using the translate_text() method in our program.

In the translate text method, the documentation starts with translate_text(**kwargs) and then further down has a section with the title Request Syntax. The request syntax is as follows:

With these pieces of information we can build our python function. In a python function the () at the end, prior to the : is used to provide parameters which are inputs to your function.

In Boto3 the methods use (**kwargs). In python, this indicates that the function will accept an arbitrary number of keyword arguments. We will come back to the use of kwargs later.

Install Boto3 Python 3

Reading the documentation you will see that translate_text() has three [REQUIRED] inputs and one optional input.

  1. Text [REQUIRED]
  2. TerminologyNames - this is optional because it is not shown as required
  3. SourceLanguageCode [REQUIRED]
  4. TargetLanguageCode [REQUIRED]

All of these are the type String, with the exception of TerminologyNames, which is a list containing comma separated values of the type String. For now, to simplify our function further we are going to remove the TerminologyNames, which is not required and provide hard coded values to our variables.

The SourceLanguageCode and TargetLanguageCode are listed in the documentation. Twitch streamer.

Boto3 python

We will be using English and French, with the codes en and fr. Perform the following steps:

  • Create a new file called lab_2_intro_to_boto3.py and add the following code:

When something new is introduced the code is marked up with comments, subsequently to keep the program looking clean the comment will be removed. In python, anything following the ‘#’ will be a comment and ignored by python.

  • First, we need to tell python to use the boto3 package we installed using pip in the python packages section of the workshop. This is done using the import boto3 statement.

  • Second, we need to tell python which specific service we want to use within the boto3 package. This is done with client = boto3.client('translate').

Now that we have built our function and assigned values to our variables, we need to tell python to run the function and to output the response so we can see it. Do that now and our function is ready to run.

Import Boto3 Python

  • Modify python lab_2_intro_to_boto3.py as follows:
  • To run the program, enter the following command in the terminal:

    python lab_2_intro_to_boto3.py

You should get an output that looks similar to this:

Boto3 Python 2.7

If you refer to the documentation, it will show the following:

Boto3 Python Aws

This is telling us that AWS is returning data as a dictionary with a specific syntax.

Compare what has been returned from your program with the structure in the documentation. You will see that it follows the structure outlined in the documentation plus some metadata from the AWS service. This format is not easy for a human to read but it is a format used to exchange information between programs and modules. In a later lab we will find out how to extract data from this structure to present it in a format that is easier for humans to read.

Boto3 Python Tutorial

If you look at the TranslatedText value, you should see 'J'apprends à coder dans AWS'. This is the french text returned by Amazon Translate. You have just written your first python program that uses AWS. You have already learned some key concepts about variables and functions.