【发布时间】: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