javascript - 使用 Azure 函数从 Azure Blob 存储下载文件会返回不同的文件大小
问题描述
我们正在尝试使用 Azure 函数为托管在 Azure blob 存储中的文件提供服务。获取它们时,我们收到以下错误:
Uncaught (in promise) Error: Length in header does not match actual data length: X != Y
我对这类事情不是很精通,但我们的开发人员正在绞尽脑汁,我也很空虚。
我们认为是罪魁祸首的 Azure 函数中的代码片段是:
const streamToBuffer = async (readableStream) => {
return new Promise((resolve, reject) => {
const chunks = [];
readableStream.on('data', (data) => {
chunks.push(data instanceof Buffer ? data : Buffer.from(data));
});
readableStream.on('end', () => {
resolve(Buffer.concat(chunks));
});
readableStream.on('error', reject);
});
};
编辑:
const blobClient = containerClient.getBlobClient(path);
if (await blobClient.exists()) {
const downloadBlockBlobResponse = await blobClient.download();
const content = (
await streamToBuffer(downloadBlockBlobResponse.readableStreamBody)
).toString();
编辑2:
跟踪中的错误:
Uncaught (in promise) Error: Can not parse environment file at god3.js:16
Uncaught (in promise) Error: Length in header does not match actual data length: X != Y
Error while trying to use the following icon from the Manifest: $url (Download error or resource isn't a valid image)
我们仅在通过 Azure 函数提供数据时遇到此问题。我们已经尝试将逻辑托管在不同的平台上,并且在拉取文件时它可以工作。
函数的附加部分:
const containerClient = blobServiceClient.getContainerClient('site');
const blobClient = containerClient.getBlobClient(path);
if (await blobClient.exists()) {
const downloadBlockBlobResponse = await blobClient.download();
const content = (
await streamToBuffer(downloadBlockBlobResponse.readableStreamBody)
).toString();
context.res = {
headers: {
'Content-Type': downloadBlockBlobResponse.contentType,
},
body: content,
};
} else {
context.res = {
status: 404,
};
}
} else {
context.res = {
status: 401,
headers: { 'WWW-Authenticate': 'Basic realm=Access to the staging site' },
body: { success: false },
};
}
};
任何指针将不胜感激。
谢谢
解决方案
我们发现了问题。
我们正在对二进制文件进行字符串化,BabylonJS 对此并不欣赏。
解决方案是在函数中添加文件类型检查。
const content = await streamToBuffer(
downloadBlockBlobResponse.readableStreamBody,
);
const fileType = path.split('.').slice(-1)[0];
context.res = {
headers: {
'Content-Type': downloadBlockBlobResponse.contentType,
},
body: textPlainType.has(fileType) ? content : content.toString(),
};
推荐阅读
- javascript - X 轴标签为类别未正确显示:Highcharts
- apache-camel - 如何仅将参数值添加到骆驼的端点?
- cloud - 为什么它会在 Odoo.sh 中被删除?
- javascript - Exporting html to Excel / CSV using tableExport, the exported file loses its formatting
- flutter - flutter TabBarView doesn't change by TabController by tapping on tabs/tabs doesn't change when swiping but tabbarview does
- python - pass a parameter from http request as a column name does not work in python azure function
- linux - 命令 `docker` 不能与管道一起使用。不要使用`|`、`>`、`>>`
- c# - 在c#中使用单一方法会发生Stackoverflow异常吗?
- javascript - metronics mDatatable 加载大量数据的问题 40000 appx ..找不到任何解决方案
- python - 使用opencv python获取文本大小