Interacting with Azure Files Using REST

45 minutes
  • 2 Learning Objectives

About this Hands-on Lab

Azure Files are a great way to manage structured data in the cloud. You can even mount them as regular file shares in order to access them from your servers using technologies you may already be familiar with, such as Samba. However, Azure also offers a RESTful interface which you can use to interact with Azure File Shares. In this lab, you will see what it looks like to read and write to an Azure File Share using REST.

Learning Objectives

Successfully complete this lab by achieving the following learning objectives:

Create the file using the REST API.
  1. Log in to the provided VM using the Public IP address and credentials.
  2. In order to authenticate, you will need to create a signed authorization header.
  3. Set some temporary environment variables to aid in future commands.
  4. For the storage_account and access_key, provide the actual storage account name and access key. To obtain these, log in to the Azure portal. The access key can be found by clicking the storage account, then clicking Access Keys.
request_date=$(TZ=GMT date "+%a, %d %h %Y %H:%M:%S %Z")

storage_service_version="2018-11-09"

storage_account=${your storage account name}

access_key=${your storage account access key}

resource="/${storage_account}/config/nginx-site-config.conf"

file_length=$(wc -m < nginx-site-config.conf)

request_method="PUT"
  1. Create a temporary environment variable containing the list of headers that need to be signed:
    headers="x-ms-content-length:$file_lengthnx-ms-date:$request_datenx-ms-type:filenx-ms-version:$storage_service_version"
  2. Create a variable that contains the full string that will be signed:
    string_to_sign="${request_method}nnnnnnnnnnnn${headers}n${resource}"
  3. Create a variable that contains the access key decoded and converted to hex:
    hex_key="$(echo -n $access_key | base64 -d -w0 | xxd -p -c256)"
  4. Generate the signature and Authorization header:
    signature=$(printf "$string_to_sign" | openssl dgst -sha256 -mac HMAC -macopt "hexkey:$hex_key" -binary |  base64 -w0)
    authorization_header="SharedKey $storage_account:$signature"
  5. Create the file in the Azure File Share:
    curl -X PUT -H "x-ms-content-length:$file_length" 
    -H "x-ms-date:$request_date" 
    -H "x-ms-type:file" 
    -H "x-ms-version:$storage_service_version" 
    -H "Content-Length: 0" 
    -H "Authorization:$authorization_header" 
    "https://${storage_account}.file.core.windows.net/config/nginx-site-config.conf"
Upload the file’s contents.
  1. Generate a new authorization header:
request_date=$(TZ=GMT date "+%a, %d %h %Y %H:%M:%S %Z")

request_method="PUT"

content_type="text/plain"

headers="x-ms-date:$request_datenx-ms-range:bytes=0-$((file_length - 1))nx-ms-version:$storage_service_versionnx-ms-write:update"

string_to_sign="${request_method}nnn$file_lengthnn$content_typennnnnnn${headers}n${resource}ncomp:range"

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. Upload the data to the file:
curl -X PUT -H "x-ms-date:$request_date" 
  -H "x-ms-version:$storage_service_version" 
  -H "x-ms-range:bytes=0-$((file_length - 1))" 
  -H "x-ms-write:update" 
  -H "Authorization: $authorization_header" 
  -H "Content-Type:$content_type" 
  -H "Content-Length:$file_length" 
  --data-binary "@nginx-site-config.conf" 
  "https://${storage_account}.file.core.windows.net/config/nginx-site-config.conf?comp=range"
  1. If you wish, you can locate the file in the Azure portal and click Download to verify that the contents appear.

Additional Resources

Your company, Store All The Things!, offers customers the ability to store any object. They want to use an Azure file share to maintain some shared configuration for a collection of Nginx web servers. The developers are requesting that you upload a configuration file to the Azure File Share. Use the REST API to upload the nginx configuration file, then download it to verify its contents.

Here are the details you will need:

  • The storage account containing the File Share begins with the text webconfig.
  • The File Share is called config.
  • The configuration file stored in the File Share should be named nginx-site-config.conf.
  • The configuration file that needs to be uploaded can be found on the lab VM at /home/cloud_user/nginx-site-config.conf.

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.

Sign In
Welcome Back!

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

Get Started
Who’s going to be learning?