liveBook · Manning

Video-storage microservice

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

The book's storage-facing microservice abstraction through which video content is retrieved independently of its storage provider.

A video-storage microservice sits between video-streaming and a file storage provider. Video-streaming requests a video through the service rather than connecting to cloud storage itself. This keeps provider-specific code in one place and lets the storage implementation change with minimal disruption to the rest of the application. section 4.4 section 4.4.6

A minimal Azure-backed version has three parts:

  • Create an Azure Storage account, a private videos container, and a test video. Private storage prevents direct public downloads; the service authenticates to retrieve files. section 4.4.1
  • Implement an Express REST API that reads the storage account name and access key from environment variables and uses @azure/storage-blob to retrieve videos. section 4.4.1
  • Add the service to Docker Compose. Its implementation can be named azure-storage while its container is named video-storage: callers use the storage-facing name, not the provider name. section 4.4.3

Run and check both services

From chapter-4/example-2, run docker compose up --build after setting STORAGE_ACCOUNT_NAME and STORAGE_ACCESS_KEY. Open http://localhost:4001/video to check video-streaming and its storage dependency together. The storage service can also be tested independently. section 4.4.3 section 4.4.4

The storage service retrieves the file; it does not need the video's metadata database. In the database-backed version, video-streaming reads the video's path from MongoDB instead of hardcoding it, then requests the file from video-storage. section 4.5 section 4.5.5

Listing 10.8 Streaming video from Azure Storage
app.get("/video", async (req, res) => {            #1
 
  const videoId = req.query.id;                    #2
 
  const blobService = createBlobService();
  const containerClient =
   blobService.getContainerClient(STORAGE_CONTAINER_NAME);
  const blobClient = containerClient.getBlobClient(videoId);
  const properties = await blobClient.getProperties();
 
  res.writeHead(200, {                             #3
    "Content-Length": properties.contentLength,
    "Content-Type": "video/mp4",
  });
 
  const response = await blobClient.download();
  response.readableStreamBody.pipe(res);           #4
});

The completed Azure implementation shows GET /video locating a blob by video ID, setting response headers, and piping the download stream into the HTTP response. section 10.7.5

The completed application also sends uploads through video-storage: video-upload forwards an HTTP POST containing the file and a new video ID, and the Azure implementation streams the request into blob storage. Upload is a later addition, not part of the initial retrieval-only service. section 10.7.6 section 4.4.6

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

How deep it goes
Builds an Azure-backed retrieval API and Compose setup, then shows streaming and upload routes; initially defers upload.
Examples
Uses a private Azure videos container, an Express retrieval route, a Docker Compose service, and a streamed upload route.
What it assumes
Assumes an Azure account is available while walking through storage-account setup; uses Node.js and Docker Compose for the running application.
What you can do after
Build it from scratch with Express and @azure/storage-blob, then operate it alongside video-streaming with Docker Compose.
How it connects
Connects separation of concerns and single responsibility to HTTP microservice communication, Docker Compose, Azure Storage, and MongoDB metadata.

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.