4

How to upload data on AWS DynamoDB using Boto3

 2 years ago
source link: https://blog.knoldus.com/how-to-upload-data-on-aws-dynamodb-using-boto3/
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

 How to upload data on AWS DynamoDB using Boto3

Reading Time: 4 minutes

Hello Readers! In this blog we will see how to upload data on AWS DynamoDB using Boto3 in a few easy steps. So, let’s not waste any time.

We have some following prerequisites for starting it:

  1. You must have installed Python3 on your system.
  2. You must have installed Boto3 on your system. Do prefer this blog for the setup of Boto3: https://blog.knoldus.com/introduction-to-boto3-and-its-installation/
  3. Install AWS CLI and Configure it. If you want to check if you have configured it or not, you can check it like this.

aws configure

Make sure to complete all the above steps before starting.

Let’s get started!

Our first step is to create a DynamoDB. We will create it by using AWS Console. So, follow the following steps for creating a DynamoDB in AWS:

DynamoDB

Click on Create Table.

DynamoDB

Fill here the table details. Give here the name of the table as per your wish. You have to give a primary key which we all know is a unique key. It should be different for every person at the table. So, here I will specify emp_id as a primary key.

create table

After filling the fields. Click on Create. And you can see here AWS is creating a table for us in DynamoDB.

table

After sometime you can see here the status of the table is active.

table

So, first let’s add data manually in the DynamoDB table.

For this open your table and move to Create item.

create item

Select here the type of item you want to add. Here I am adding string item.

item

Fill here the attribute name and value you want to add. And Click on Create Item.

item

It is created now as we can see here.

item created

Let’s upload data by using boto3:

We will do it through a boto3 program. In this program I am creating boto3 objects. I am specifying the name of the DynamoDB table (3rd line) in which we want to insert data.

I will use the put_item method here in my program. It is basically used to create a new item and replaces an old item with a new item in the DynamoDB table.

import boto3

db = boto3.resource('dynamodb')
table = db.Table("employees")

table.put_item(
    Item={
      'emp_id':"2",
      'name':"Johny",
      'age':"25"
    }
)

So, let’s run the code and see the result.

python3 upload.py

DynamoDB

It’s done. Now lets see:

How to get data from the DynamoDB table using boto3:

For this we have to use the get_item method. And in response we will get all the values matching with emp_id : 2.

import boto3

db = boto3.resource('dynamodb')
table = db.Table("employees")

response = table.get_item(
    Key={
      'emp_id':"2"
    }
)
print(response["Item"])

let’s run the code and see the result. We are getting here all the data of employee whose emp_id is 2.

jDrlUQEW4_hNzWlf82HXjkOVv3ezt48UOKmuPlCHHKwB0bgYtzVsFi9sMtsfYNrYlsf966JlCUAcv8VbQJ9qo10G2f-1Bhlniv4SSmrL-aBhHnCebbR0wwwku0AErKqa9WMESvvZ

Congrats! 👏 We are successfully done now!

Conclusion

In this blog we have seen how we can upload data on AWS DynamoDB using Boto3. Thank you for sticking to the end. If you like this blog, please do show your appreciation by giving thumbs ups and share this blog and give me suggestions on how I can improve my future posts to suit your needs. Follow me to get updates on different technologies.

Scala Future

HAPPY LEARNING!


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK