我们正在尝试使用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(),
};
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句