首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用node.js '@azure/storage-blob‘包从Azure-Storage中播放视频

使用node.js '@azure/storage-blob‘包从Azure-Storage中播放视频
EN

Stack Overflow用户
提问于 2021-10-18 11:29:47
回答 1查看 243关注 0票数 0

下面的代码允许通过使用'azure-storage'包从Azure存储中播放视频。

我希望实现同样的目标,但使用'@azure/storage-blob'包。

代码语言:javascript
复制
.....
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(`.....`);
});

提前谢谢..。

EN

回答 1

Stack Overflow用户

发布于 2021-10-18 18:59:37

好吧,我找到了解决办法..。

代码语言:javascript
复制
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);
    }

});
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/69615268

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档