Overview
Azure Storage provides a massively scalable object store for data objects, a file system for the cloud, a messaging store for reliable messaging, and a NoSQL store. This tutorial walks you through creating a storage account, uploading a blob, and accessing it from code.
Prerequisites
- Azure subscription (free trial works)
- Azure CLI installed (
az) - PowerShell 5.1+ or Bash
- Basic knowledge of cloud concepts
Create a Storage Account
Run the following commands in your terminal to create a resource group and a storage account.
az group create --name tutorial-rg --location eastus
az storage account create \
--name tutstorage$(openssl rand -hex 3) \
--resource-group tutorial-rg \
--location eastus \
--sku Standard_LRS
Upload a Blob
First, create a container and upload a sample file.
ACCOUNT=$(az storage account show -g tutorial-rg -n tutstorage* --query "name" -o tsv)
CONTAINER=mycontainer
az storage container create --account-name $ACCOUNT --name $CONTAINER
# Create a sample text file
echo "Hello Azure Storage!" > sample.txt
az storage blob upload \
--account-name $ACCOUNT \
--container-name $CONTAINER \
--name hello.txt \
--file sample.txt
Access Your Data
Retrieve the blob URL and read its contents using Azure SDK for JavaScript (Node.js).
npm install @azure/storage-blob
// index.js
const { BlobServiceClient } = require("@azure/storage-blob");
async function main() {
const account = process.env.AZURE_STORAGE_ACCOUNT;
const key = process.env.AZURE_STORAGE_KEY;
const sharedKeyCredential = new StorageSharedKeyCredential(account, key);
const blobServiceClient = new BlobServiceClient(
`https://${account}.blob.core.windows.net`,
sharedKeyCredential
);
const containerClient = blobServiceClient.getContainerClient("mycontainer");
const blobClient = containerClient.getBlobClient("hello.txt");
const downloadBlockBlobResponse = await blobClient.download();
const downloaded = await streamToString(downloadBlockBlobResponse.readableStreamBody);
console.log("Blob content:", downloaded);
}
main().catch(console.error);
Next Steps
- Explore Blob storage features.
- Implement lifecycle management policies.
- Secure your data with Azure AD authentication.
- Scale with Azure Data Lake Storage.