【问题标题】:How to stream a Windows Azure Blob to the client using Node.js?如何使用 Node.js 将 Windows Azure Blob 流式传输到客户端?
【发布时间】:2018-12-29 01:26:18
【问题描述】:

我想直接向客户端发送一个 Windows Azure Blob(图像);我正在尝试这个:

    blobService.getBlobToStream('images', req.url, res, function(err, blob) {
    if (!err) {
        res.writeHead(200, { 'Content-Type': blob.contentType });
    } else {
        res.writeHead(200, { 'Content-Type': 'text/plain' });
        res.end(err);
    }
});

我正在尝试将 Blob 流直接通过管道传输到响应流;这应该工作吗?在 Firefox 中,我收到一条消息:“图像无法显示,因为它包含错误”。看Firebug,图片大小为0。

【问题讨论】:

  • 可能是个愚蠢的问题,但为什么要通过自己的托管服务流式传输 blob?为什么不直接将 blob URL 返回给客户端或将它们重定向到 BLOB URL 并让它们自己通过执行 HTTP GET 流式传输它。这将为您节省自己缓冲/重新流式传输的托管成本。对于大量的 blob 数据,这可以为您节省很多钱。
  • 说得好,迈克,但我希望汤姆正在尝试。
  • 是的,我们的想法是在将 blob(图像)流回客户端之前对其进行转换(即重新调整大小);谢谢理查德!

标签: node.js azure


【解决方案1】:

这对我来说似乎很有效(如果这有帮助吗?):

var azure = require('azure');
var http = require('http');
http.createServer(function (req, res) {
    var blobService = azure.createBlobService("xxx", "yyy", "blob.core.windows.net").withFilter(new azure.ExponentialRetryPolicyFilter());
    blobService.getBlobToStream('container', 'image.png', res, function(error){
        if(!error){
            res.writeHead(200, {'Content-Type': 'image/png'});
            res.end();
        }
        else
        {
            console.log('error');
            console.log(error);
            res.end();
        }
    });
}).listen(8080, "127.0.0.1");

更新

刚刚想通了。 req.url 将有一个前导斜杠 (/)。我猜这与您的图像文件名不匹配。

【讨论】:

    【解决方案2】:

    使用 azure-storage@2.10.2 在完成后写入响应时出现错误。我还注意到getBlobToStream 会自动处理内容类型。

    const storage = require('azure-storage');
    const blobService = storage.createBlobService(azureStorage.account, azureStorage.key); // if you want aren't using the same azure env vars
    
    blobService.getBlobToStream(config.switchConfigStorage.container, 'image.png', res, function(error, blob){
      if(!error){ // blob retrieved
        console.log(blob); // good for debugging and possibly more processing
        res.end(); // no need to writeHead
      }else{
        console.log(error);
        res.end();
      }
    });
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2016-03-03
      • 1970-01-01
      • 2020-12-10
      • 2015-11-29
      • 2021-12-05
      • 2014-12-06
      • 1970-01-01
      相关资源
      最近更新 更多