liveBook · Manning

Azure Storage

As explained in Bootstrapping Microservices, Second Edition: With Docker, Kubernetes, GitHub Actions, and Terraform

An Azure Storage account is a Microsoft Azure resource that provides access to cloud storage services and their stored data.

Azure Storage is Microsoft Azure’s cloud file-storage service. Files can be private or public and are accessible through its API. For a video application, it provides a place to keep video files outside the application’s containers. section 4.4.1 section 4.8

To use it for private videos:

  • Create an Azure storage account, create a videos container, and upload a test video through the Azure portal. section 4.4.1
  • Give a video-storage microservice the storage account name and access key through environment variables. Use @azure/storage-blob to authenticate and retrieve the video. section 4.4.1
  • Keep that microservice between the video-streaming microservice and Azure Storage, rather than connecting streaming directly to the storage provider. section 4.4
Listing 4.2 A microservice to retrieve videos from Azure Storage
const express = require("express");
const { BlobServiceClient, StorageSharedKeyCredential }
   = require("@azure/storage-blob");                        #1
 
const PORT = process.env.PORT;
const STORAGE_ACCOUNT_NAME = 
   process.env.STORAGE_ACCOUNT_NAME;                        #2
const STORAGE_ACCESS_KEY = 
   process.env.STORAGE_ACCESS_KEY;                          #3
 
function createBlobService() {                                #4
  const sharedKeyCredential = new StorageSharedKeyCredential(
    STORAGE_ACCOUNT_NAME, STORAGE_ACCESS_KEY
  );
  const blobService = new BlobServiceClient(
    `https://${STORAGE_ACCOUNT_NAME}.blob.core.windows.net`,
    sharedKeyCredential
  );
  return blobService;
}
 
const app = express();
 
app.get("/video", async (req, res) => {                      #5
 
  const videoPath = req.query.path;                          #6
  
  const containerName = "videos";                            #7
  const blobService = createBlobService();                   #8
  const containerClient = 
     blobService.getContainerClient(containerName);        #9
  const blobClient = 
     containerClient.getBlobClient(videoPath);             #10
 
  const properties = 
     await blobClient.getProperties();                     #11
 
  res.writeHead(200, {                                       #12
    "Content-Length": properties.contentLength,
    "Content-Type": "video/mp4",
  });
 
  const response = await blobClient.download();              #13
  response.readableStreamBody.pipe(res);                     #14
});
 
app.listen(PORT);
#1 Loads the Azure Storage SDK so our code can interact with the Azure Storage API
#2 Gets the name of the storage account from an environment variable
#3 Gets the name of the access key from an environment variable
#4 Helper function that connects to the Azure Storage API
#5 HTTP GET route for retrieving a video from Azure Storage. Note that the route handler uses the async keyword so that we can use JavaScript’s async/await syntax within. This is why we installed Express v5 beta in chapter 2.
#6 Specifies the path to the video in storage as an HTTP query parameter
#7 Hardcoded container name. Later, we can vary this for some purpose (e.g., by user ID so we can keep videos for each user separately).
#8 Connects to the Azure Storage API
#9 Connects the client for the Azure Storage container
#10 Connects the client for the “blob” (aka the file) that we’d like to retrieve
#11 Retrieves the video’s properties from Azure Storage
#12 Writes content length and MIME type to the HTTP response headers
#13 Starts the download of the specified file
#14 Pipes the video stream from Azure Storage to the HTTP response

The retrieval service creates a BlobServiceClient with a shared-key credential and reads its account settings from environment variables. section 4.4.1

Private storage prevents unrestricted downloads from Azure Storage; customers receive videos through the application instead. Keeping storage access in its own microservice also leaves room to replace Azure with another provider without changing the overall microservice structure. In production, external storage helps keep a Kubernetes cluster stateless and rebuildable without losing its files. section 4.4.1 section 4.8

How Bootstrapping Microservices, Second Edition: With Docker, Kubernetes, GitHub Actions, and Terraform covers it

How deep it goes
Works through account setup, video upload, and SDK-based retrieval; does not explore Azure Storage internals.
Examples
Uses an uploaded test video, a videos container, and an Express retrieval service configured with account credentials.
What it assumes
Assumes an Azure account and uses Node.js, npm, and environment variables in the retrieval example.
What you can do after
Apply it via @azure/storage-blob to retrieve privately stored videos through a microservice.
How it connects
Connects cloud file storage to microservices, separation of concerns, REST APIs, and stateless Kubernetes deployments.

Also explained in

Compare how 2 books explain Azure Storage →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Bootstrapping Microservices, Second Edition: With Docker, Kubernetes, GitHub Actions, and Terraform. The section links point to the parts of the book it draws on.