Skip to content

Contact sales

By filling out this form and clicking submit, you acknowledge our privacy policy.
  • Labs icon Lab
  • A Cloud Guru
Azure icon
Labs

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.

Azure icon
Labs

Path Info

Level
Clock icon Intermediate
Duration
Clock icon 45m
Published
Clock icon Feb 14, 2020

Contact sales

By filling out this form and clicking submit, you acknowledge our privacy policy.

Table of Contents

  1. Challenge

    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_length\nx-ms-date:$request_date\nx-ms-type:file\nx-ms-version:$storage_service_version"
    
    1. 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}"
    
    1. 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)"
    
    1. 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"
    
    1. 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"
    
  2. Challenge

    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_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"
    
    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.

The Cloud Content team comprises subject matter experts hyper focused on services offered by the leading cloud vendors (AWS, GCP, and Azure), as well as cloud-related technologies such as Linux and DevOps. The team is thrilled to share their knowledge to help you build modern tech solutions from the ground up, secure and optimize your environments, and so much more!

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.

Start learning by doing today

View Plans