Azure provides a variety of ways to interact with Blob storage. You can manage and access Blob storage using the Azure portal, a command line, or your own custom code. In this hands-on lab, you will have the opportunity to work with Azure storage using the Azure Blob Service REST API. Using basic HTTP requests, you will download some data stored in a blob, modify it, and re-upload the modified data to the blob.
Learning Objectives
Successfully complete this lab by achieving the following learning objectives:
- Authenticate with the Azure Blob Service REST API and Download the File
- Log in to the provided VM using the Public IP address and credentials.
- Create a signed authorization header in order to authenticate. To do so, we first need to set some temporary environment variables to aid in some future commands. For the
storage_account
andaccess_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.request_date=$(TZ=GMT date "+%a, %d %h %Y %H:%M:%S %Z") storage_service_version="2015-04-05" storage_account="your storage account name" access_key="your storage account access key" resource="/${storage_account}/records/cars" request_method="GET"
- Create a temporary environment variable containing the list of headers that need to be signed:
headers="x-ms-date:$request_datenx-ms-version:$storage_service_version"
- Create a variable that contains the full string that will be signed:
string_to_sign="${request_method}nnnnnnnnnnnn${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"
8.Download the Blob data to a local file:
curl -H "x-ms-date:$request_date" -H "x-ms-version:$storage_service_version" -H "Authorization: $authorization_header" "https://${storage_account}.blob.core.windows.net/records/cars" > cars.csv
- Verify the file contains the data. You should see a comma-delimited list of records representing various cars:
cat cars.csv
- Modify the File and Re-Upload it to Blob Storage
- Edit the data file:
vi cars.csv
- Add the new customer record to the end of the file:
57991237,2020,Tesla,Cybertruck,X3B-33
- 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" file_length=$(wc -m < cars.csv) headers="x-ms-blob-type:BlockBlobnx-ms-date:$request_datenx-ms-version:$storage_service_version" string_to_sign="${request_method}nnn$file_lengthnn$content_typennnnnnn${headers}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"
- Upload the modified data to the Blob.
curl -X PUT -H "x-ms-blob-type:BlockBlob" -H "x-ms-date:$request_date" -H "x-ms-version:$storage_service_version" -H "Authorization: $authorization_header" -H "Content-Length:$file_length" -H "Content-Type:$content_type" --data-binary "@cars.csv" "https://${storage_account}.blob.core.windows.net/records/cars"
- If you wish, you can locate the Blob in the Azure portal and click Edit to verify that your changes appear.
- Edit the data file: