1

I am using Nodejs to download a file to a buffer to use for processing in my code. The relevant code is

        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);
        }

However, instead of waiting for the download and then proceeding with the rest of the code, the line blob = await bbc.downloadToBuffer(); prematurely ends the function app and returns a 200 with no body. In the console I then see the message

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 

and eventually in my logs, I see the required output, but the function has already prematurely returned an empty body. I have no idea why this is happening, and I would appreciate any help.

Output for call to downloadToBuffer

IrtzaSuhail
  • 97
  • 1
  • 9
  • Dont see a problem with you code. I might be happening somewhere. Can you post the entire function and how its used. – kg99 Dec 09 '20 at 12:06
  • How's going? Has your issue got solved ? – Stanley Gong Dec 11 '20 at 01:18
  • Hi! Yes we fixed it. We were chaining multiple .then()s elsewhere in the code that were causing the problem. Refactoring them with awaits resolved our issue – IrtzaSuhail Dec 11 '20 at 08:59
  • Glad to know that your issue has been solved.If my post is helpful,please click on the check mark beside the answer to toggle it from greyed out to filled in to accept it as an answer, so that it will help others and close this query : ) – Stanley Gong Dec 14 '20 at 01:35

1 Answers1

1

There is nothing wrong with your blob downloading code, I assume there shoule be something wrong that you handle the result inside your js function. I writed a simple demo that get the content of a .txt for you whcih could meet your requirement: enter image description here

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

Result:

enter image description here enter image description here

Stanley Gong
  • 11,522
  • 1
  • 8
  • 16