【问题标题】:Getting "{"message":"Invalid value \"undefined\" for header \"Content-Type\""}" error when tried to download a container file尝试下载容器文件时出现“{“message”:“标题 \"Content-Type\""}" 的无效值 \"undefined\" 错误
【发布时间】:2020-06-04 18:21:47
【问题描述】:

我使用下面的代码从 azure 容器中获取文件。

const azure = require('azure-storage');
const url = `https://${storageAccountName}.blob.core.windows.net}`;
const blobSvc = azure.createBlobService(storageAccountName, accessKey, url);

const read = blobSvc.createReadStream(containerName, fileName);

res.setHeader('Content-Disposition', `attachment; filename=${filename}`);
res.setHeader('Content-Type', read.contentType);
readable.stream.pipe(res);

但是,当我下载文本文件时,我在文件中得到{"message":"Invalid value \"undefined\" for header \"Content-Type\""}

const azure = require('azure-storage');
const url = `https://${storageAccountName}.blob.core.windows.net}`;
const blobSvc = azure.createBlobService(storageAccountName, accessKey, url);

const properties = blobSvc.getBlobProperties(
containerName,
fileName,
function(err, properties, status) {
    if (err) {
        res.send(502, "Error fetching file: %s", err.message);
    } else if (!status.isSuccessful) {
        res.send(404, "The file %s does not exist", fileName);
    } else {
        return properties;
    }
});

)

返回属性;

在这个函数中,第一次调用返回null,第二次调用时有值弹出,但是没用,因为在这种情况下等待不起作用,线程以未定义的值结束。有什么我遗漏的吗?

【问题讨论】:

  • console.log(read.contentType) 的输出是什么?
  • 问题是 blobSvc.createReadStream 返回一个可读流并且没有 contentType 属性。
  • 有没有办法检索内容类型??
  • 提供了答案。 HTH。

标签: node.js typescript azure azure-storage azure-stream-analytics


【解决方案1】:

您收到此错误的原因是因为blobSvc.createReadStream 向您返回了一个流并且没有contentType 属性。

如果您需要在响应标头中发送内容类型,您首先需要获取 blob 的属性并从那里使用 contentType 属性。

这是一个示例代码(未经测试)(参考:https://willi.am/blog/2014/07/03/azure-blob-storage-and-node-downloading-blobs/):

blobSvc.getBlobProperties(
    containerName,
    fileName,
    function(err, properties, status) {
        if (err) {
            res.send(502, "Error fetching file: %s", err.message);
        } else if (!status.isSuccessful) {
            res.send(404, "The file %s does not exist", fileName);
        } else {
            res.setHeader('Content-Disposition', `attachment; filename=${filename}`);
            res.setHeader(Content-Type', properties.contentType);
            blobService.createReadStream(containerName, fileName).pipe(res);
        }
    });
)

【讨论】:

  • 我使用了以下代码。blobSvc.getBlobProperties( containerName, fileName, function(err, properties, status) { if (properties) {return properties }); ) //似乎第一次我无法获取属性,但是在第二次调用期间,它被加载了。不幸的是,节点线程没有等待这个结果。我也添加了等待和异步。没有什么帮助..我错过了什么吗?
  • 请编辑您的问题并添加最新代码作为更新。另外,请在您调用上述代码的地方包含完整的代码。
  • const azure = require('azure-storage');常量 url = https://${storageAccountName}.blob.core.windows.net}; const blobSvc = azure.createBlobService(storageAccountName, accessKey, url); const properties = await blobSvc.getBlobProperties( containerName, fileName, function(err, properties, status) { if (err) { res.send(502, "Error fetching file: %s", err.message); } else if ( !status.isSuccessful) { res.send(404, "文件 %s 不存在", fileName); } else { return properties } });返回属性;
  • 请不要将代码放入 cmets。编辑您的问题并在其中包含代码。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-07-12
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-10-22
  • 2020-07-01
相关资源
最近更新 更多