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
