首页 > 解决方案 > 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 

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

调用 downloadToBuffer 的输出

标签: node.jsazure-blob-storageazure-function-app

解决方案


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

结果:

在此处输入图像描述 在此处输入图像描述


推荐阅读