如何解决使用 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(),};
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。