node.js - Azure 存储下载到 Blob 过早结束 Function App 调用
问题描述
我正在使用 Nodejs 将文件下载到缓冲区以用于在我的代码中进行处理。相关代码是
let bbc = containerClient.getBlockBlobClient(
userId + "/" + documentUuids[i] + ".pdf"
);
let blob;
try {
console.log("downloading blob")
blob = await bbc.downloadToBuffer();
console.log("downloaded blob ")
} catch (e) {
console.log(userId + "/" + documentUuids[i] + ".pdf")
console.log(e);
}
但是,该行没有等待下载然后继续执行其余代码,而是blob = await bbc.downloadToBuffer();
提前结束了函数应用程序并返回没有正文的 200。然后在控制台中我看到消息
Warning: Unexpected call to 'log' on the context object after function execution has completed. Please check for asynchronous calls that are not awaited or calls to 'done' made before function execution completes. Function name: BasketsCreateUpdate. Invocation Id: 59f57785-6390-4b93-a69e-8244dc688d37. Learn more: https://go.microsoft.com/fwlink/?linkid=2097909
最终在我的日志中,我看到了所需的输出,但该函数已经过早地返回了一个空主体。我不知道为什么会这样,我将不胜感激。
解决方案
您的 blob 下载代码没有任何问题,我认为您在 js 函数中处理结果应该有问题。我编写了一个简单的演示,为您获取 .txt 的内容,可以满足您的要求:
module.exports = async function (context, req) {
const { BlobServiceClient, StorageSharedKeyCredential } = require("@azure/storage-blob");
const account = ''
const accountKey = ''
const container = ''
const blobName = ''
async function test(context){
const sharedKeyCredential = new StorageSharedKeyCredential(account, accountKey);
const blobServiceClient = new BlobServiceClient(
`https://${account}.blob.core.windows.net`,
sharedKeyCredential
);
const bbc = blobServiceClient.getContainerClient(container).getBlockBlobClient(blobName);
context.log('=================> download start');
let blob = await bbc.downloadToBuffer();
context.log('=================> download complete');
return blob.toString('utf-8');
}
var result = await test(context);
context.res = {
body: result
};
}
结果:
推荐阅读
- javascript - 重复的 ChoicePrompt 清除 UserState
- android - 反应原生地图折线
- c# - Unity3D:如何调用从另一个类实例化预制件的方法?
- tsql - DATEADD 的 Pyspark 实现
- python - 如何在 xpathin selenium 中使用运算符“in”
- json - 我在远程分页中将数据作为表单数据而不是 JSON 获取
- java - 找不到 Spring Boot 2.1 EntityManagerFactory Bean
- c# - 如何读取 Azure Active Directory 的域
- python - 在单键多值字典的 python 列表中,如何修改每个字典的第 n 个值?
- gdb - yocto:在构建 gdb 时在构建中添加 .gdbinit