- Lab
- A Cloud Guru
Interacting with Azure Files Using REST
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.
Path Info
Table of Contents
-
Challenge
Create the file using the REST API.
- Log in to the provided VM using the Public IP address and credentials.
- In order to authenticate, you will need to create a signed authorization header.
- Set some temporary environment variables to aid in future commands.
- For the
storage_account
andaccess_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"
- Create a temporary environment variable containing the list of headers that need to be signed:
headers="x-ms-content-length:$file_length\nx-ms-date:$request_date\nx-ms-type:file\nx-ms-version:$storage_service_version"
- Create a variable that contains the full string that will be signed:
string_to_sign="${request_method}\n\n\n\n\n\n\n\n\n\n\n\n${headers}\n${resource}"
- 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)"
- 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"
- 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"
-
Challenge
Upload the file's contents.
- 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_date\nx-ms-range:bytes=0-$((file_length - 1))\nx-ms-version:$storage_service_version\nx-ms-write:update" string_to_sign="${request_method}\n\n\n$file_length\n\n$content_type\n\n\n\n\n\n\n${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"
- 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"
- If you wish, you can locate the file in the Azure portal and click Download to verify that the contents appear.
What's a lab?
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.
Provided environment for hands-on practice
We will provide the credentials and environment necessary for you to practice right within your browser.
Guided walkthrough
Follow along with the author’s guided walkthrough and build something new in your provided environment!
Did you know?
On average, you retain 75% more of your learning if you get time for practice.