node.js - 使用 node.js '@azure/storage-blob' 包从 Azure-Storage 流式传输视频
问题描述
下面的代码允许使用 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(`.....`);
});
提前致谢 ...
解决方案
好的,我找到了解决方案...
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);
}
});
推荐阅读
- c# - 出现错误 CS1503:“参数 1:无法从 'System.Diagnostics,PerformanceCounter' 转换为 'int'
- nginx - 如何托管只能由 tor 访问而不是通过我的本地网络访问的 Web 服务器(nginx)?
- database - EasyPHP 开发服务器无法启动
- security - 如果 OAuth2 访问令牌可以被盗,为什么假设刷新令牌也可以被盗是不安全的?
- matlab - 有没有办法在优化问题中从 matlab 中的 ND 变量中消除空列或行以解决参数维度问题?
- android - 检查可拖动的 ImageView 是否覆盖了任何可见的 RecyclerView 元素
- python - 是否有一个 Numpy/Scipy 函数用于在以指定的基础编写的给定区间内查找多项式的根?
- javascript - 无法从 Javascript 访问 JSON 数据
- python-requests - 使用 python 请求在网站上登录帐户
- android-studio - 如何在“其他”条件下取消 toast 消息,即使它仍然适用