Accessing Azure Table Data with REST

45 minutes
  • 2 Learning Objectives

About this Hands-on Lab

Azure tables are an excellent way to store structured data in a query-able NoSQL database. Azure provides a variety of ways to interact with this data, including a REST API, which allows you to use Azure Table storage with simple HTTP requests. In this lab, you will be able to interact with the Azure Table service REST API by using it to retrieve and store data.

Learning Objectives

Successfully complete this lab by achieving the following learning objectives:

Save a copy of the table’s entity data to a file.
  1. Set up some environment variables to aid in generating a signature. For the storage_account and access_key, provide the actual storage account name and access key. One way to obtain these is to log in to the Azure portal. The access key can be found by clicking the storage account, then clicking Access Keys:
storage_account=${your storage account name}

access_key=${your storage account access key}

table_name=inventory

request_date=$(TZ=GMT date "+%a, %d %h %Y %H:%M:%S %Z")

resource="/${storage_account}/${table_name}"

request_method="GET"
  1. Generate a signature and Authorization header:
string_to_sign="${request_method}nnn${request_date}n${resource}"

hex_key="$(echo -n $access_key | base64 -d -w0 | xxd -p -c256)"

signature=$(printf "$string_to_sign" | openssl dgst -sha256 -mac HMAC -macopt "hexkey:$hex_key" -binary |  base64 -w0)

authorization_header="SharedKey $storage_account:$signature"
  1. Make a request to query for all entities currently in the table, redirecting the output to a file:
curl -s -X $request_method 
  -H "x-ms-date:$request_date" 
  -H "Authorization:$authorization_header" 
  "https://${storage_account}.table.core.windows.net/${table_name}" 
  > /home/cloud_user/entities.txt
  1. Check the contents of the file to see the entity data:
cat /home/cloud_user/entities.txt

You should see some XML representing the existing entities.

Insert a new entity into the table.
  1. Set up some environment variables to aid in generating a signature:
request_date=$(TZ=GMT date "+%a, %d %h %Y %H:%M:%S %Z")

request_method="POST"

content_type="application/json"
  1. Generate a signature and Authorization header:
string_to_sign="${request_method}nn${content_type}n${request_date}n${resource}"

hex_key="$(echo -n $access_key | base64 -d -w0 | xxd -p -c256)"

signature=$(printf "$string_to_sign" | openssl dgst -sha256 -mac HMAC -macopt "hexkey:$hex_key" -binary |  base64 -w0)

authorization_header="SharedKey $storage_account:$signature"
  1. Set up your entity data:
entity="{"PartitionKey":"warehouse1","RowKey":"A4452","Description":"Trophy Collection","OwnerName":"Sgt. Pepper","Unit":"554"}"

entity_length=${#entity}
  1. Make a request to insert the entity:
curl -X $request_method 
  -H "x-ms-date:$request_date" 
  -H "x-ms-version:2019-02-02" 
  -H "Content-Type:$content_type" 
  -H "Content-Length:$entity_length" 
  -H "Authorization:$authorization_header" 
  -d "$entity" 
  "https://${storage_account}.table.core.windows.net/${table_name}"

The output should include an <updated /> tag with a timestamp, indicating when the record was inserted.

Additional Resources

Your company, Store All the Things!, provides physical storage for customers. They are in the process of building a new management system in the cloud, and they are using Azure Tables to hold inventory data about the items being stored in physical storage warehouses.

There are still a few bugs with the system as it is being built, and some manual work needs to be done to follow up on some recent problems. You will need to:

  • Obtain a copy of the raw entity data currently in the inventory table.
  • Manually insert a new entity into the table. You can do both of these tasks using the Azure Table service REST API.

Some additional details:

  • The name of the storage account that contains the queue begins with the text storagewh.
  • The Table is called inventory.
  • Save the current raw entity data in a file at /home/cloud_user/entities.txt.
  • Manually add an entity to the Table with the following data: {"PartitionKey":"warehouse1","RowKey":"A4452","Description":"Trophy Collection","OwnerName":"Sgt. Pepper","Unit":"554"}.

If you get stuck, feel free to check out the solution video, or the detailed instructions under each objective. Good luck!

What are Hands-on Labs

Hands-on Labs are real environments created by industry experts to help you learn. These environments help you gain knowledge and experience, practice without compromising your system, test without risk, destroy without fear, and let you learn from your mistakes. Hands-on Labs: practice your skills before delivering in the real world.

Get Started
Who’s going to be learning?

How many seats do you need?

  • $499 USD per seat per year
  • Billed Annually
  • Renews in 12 months

Ready to accelerate learning?

For over 25 licenses, a member of our sales team will walk you through a custom tailored solution for your business.


$2,495.00

Checkout
Sign In
Welcome Back!

Psst…this one if you’ve been moved to ACG!