【问题标题】:Stream a video from Azure-Storage by using node.js '@azure/storage-blob' package使用 node.js '@azure/storage-blob' 包从 Azure-Storage 流式传输视频
【发布时间】:2021-12-05 11:04:35
【问题描述】:

下面的代码允许使用 node.js 'azure-storage' 包从 Azure-Storage 流式传输视频。

我想实现相同的目标,但要使用 '@azure/storage-blob' 包。

.....
app.get("/video", (req, res) => {

    const videoPath = req.query.path;
    
    const blobService = createBlobService();

    const containerName = "videos";

    blobService.getBlobProperties(containerName, videoPath, (err, properties) => { 
        if (err) {
            console.error(`Error occurred getting properties for video ${containerName}/${videoPath}.`);
            console.error(err && err.stack || err);
            res.sendStatus(500);
            return;
        }

        // Writes HTTP headers to the response.

        res.writeHead(200, {
            "Content-Length": properties.contentLength,
            "Content-Type": "video/mp4",
        });

        // Streams the video from Azure storage to the response.

        blobService.getBlobToStream(containerName, videoPath, res, err => {
            if (err) {
                console.error(`Error occurred getting video ${containerName}/${videoPath} to stream.`);
                console.error(err && err.stack || err);
                res.sendStatus(500);
                return;
            }
        });
    });
});

app.listen(PORT, () => {
    console.log(`.....`);
});

提前致谢...

【问题讨论】:

    标签: node.js azure express video-streaming azure-storage


    【解决方案1】:

    好的,我找到了解决办法……

    app.get("/video", async (req, res) => {
    
        try {
            // Create the BlobServiceClient object which will be used to create a container client
            const blobServiceClient = BlobServiceClient.fromConnectionString(
                AZURE_STORAGE_CONNECTION_STRING
            );
    
            const containerName = 'videos';
            const blobName = req.query.path;
    
            // Get a reference to a container
            const containerClient = blobServiceClient.getContainerClient(containerName);
    
            // Get a block blob client
            const blockBlobClient = containerClient.getBlockBlobClient(blobName);
    
            console.log('\nDownloaded blob content...');
    
            const downloadBlockBlobResponse = await blockBlobClient.download(0);
    
            downloadBlockBlobResponse.readableStreamBody.pipe(res);
        } catch (err) {
            console.error(err);
            res.sendStatus(500);
        }
    
    });
    

    【讨论】:

      猜你喜欢
      • 2020-03-12
      • 1970-01-01
      • 2013-11-08
      • 2017-04-30
      • 1970-01-01
      • 2017-11-24
      • 2012-04-14
      • 2019-04-28
      • 2021-10-08
      相关资源
      最近更新 更多