【问题标题】:Azure Storage Download to Blob prematurely ends Function App callAzure 存储下载到 Blob 过早结束 Function App 调用
【发布时间】:2021-03-20 19:08:39
【问题描述】:

我正在使用 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 

最终在我的日志中,我看到了所需的输出,但该函数已经过早地返回了一个空主体。我不知道为什么会这样,如果有任何帮助,我将不胜感激。

【问题讨论】:

  • 您的代码没有问题。我可能正在某个地方发生。你能发布整个函数以及它是如何使用的吗?
  • 怎么样?你的问题解决了吗?
  • 嗨!是的,我们修好了。我们在导致问题的代码的其他地方链接了多个 .then()。用 await 重构它们解决了我们的问题
  • 很高兴知道您的问题已经解决。如果我的帖子有帮助,请点击答案旁边的复选标记,将其从灰色切换为已填充以接受它作为答案,所以它将帮助其他人并关闭此查询:)

标签: node.js azure-blob-storage azure-function-app


【解决方案1】:

您的 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
    };
}

结果:

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-02-29
    • 2012-12-08
    • 2019-12-19
    • 2019-09-05
    • 2018-09-26
    • 1970-01-01
    • 2020-10-05
    • 2013-01-21
    相关资源
    最近更新 更多