在async.waterfall中使用async函数

时间:2019-05-13 13:03:29

标签: javascript node.js async-await azure-storage-blobs

我正在尝试使用Microsoft Azure Storage sdk从Azure存储中下载一些文件。 我的工作基于github存储库(https://github.com/Azure-Samples/azure-storage-js-v10-quickstart/blob/master/index.js)上的示例

当我运行以下示例代码时,一切正常。

async function execute_download() {
    const containerName = "myContainer";
    const blobName = "myFile.json";

    const credentials = new SharedKeyCredential(STORAGE_ACCOUNT_NAME, ACCOUNT_ACCESS_KEY);
    const pipeline = StorageURL.newPipeline(credentials);
    const serviceURL = new ServiceURL(`https://${STORAGE_ACCOUNT_NAME}.blob.core.windows.net`, pipeline);
    const aborter = Aborter.timeout(30 * 60 * 1000);

    const containerURL = ContainerURL.fromServiceURL(serviceURL, containerName);
    const blockBlobURL = BlockBlobURL.fromContainerURL(containerURL, blobName);

    const downloadResponse = await blockBlobURL.download(aborter, 0);
    const downloadedContent = downloadResponse.readableStreamBody.read().toString();

    const fs = require('fs');
    fs.writeFile(blobName, downloadedContent, function(err) {
        if(err) {
            return console.log(err);
        }
        console.log("The file was saved!");
    });
}

execute_download().then(() => console.log("Done")).catch((e) => console.log(e));

但是现在我正在尝试将其集成到一个更复杂的项目中。这是代码:

function downloadFile(callback) {
    async.waterfall([
        function(callback) {
            // do some stuff
            callback(null);
        },

        // Download the new file
        async function (callback) {
            const credentials = new SharedKeyCredential(STORAGE_ACCOUNT_NAME, ACCOUNT_ACCESS_KEY);
            const pipeline = StorageURL.newPipeline(credentials);
            const serviceURL = new ServiceURL(`https://${STORAGE_ACCOUNT_NAME}.blob.core.windows.net`, pipeline);
            const aborter = Aborter.timeout(30 * 60 * 1000);
            const containerURL = ContainerURL.fromServiceURL(serviceURL, containerName);
            const url = BlockBlobURL.fromContainerURL(containerURL, fileName);
            const downloadResponse =  await url.download(aborter, 0);
            const downloadedContent = downloadResponse.readableStreamBody.read().toString();
            console.log(`Downloaded file:\n "${downloadedContent}"`);

            fs.writeFile(fileName, downloadedContent, function (err) {
                if (err) {
                    callback(err);
                }
                console.log("The file was !");
                callback(null);
            });
        },

        function(callback) {
            // do some stuff
            callback(null);
        }
    ], function (err) {
        callback(err);
    });
}

TypeError: Cannot read property 'toString' of null的行出现错误downloadResponse.readableStreamBody.read().toString()。 我检查了内容downloadResponse,但与第一个示例中的内容不同。它始于:

我还尝试用以下方法代替水冷壁的所有第二功能:

// in waterfall:
function (callback) {
    download(fileName).then(() => callback(null)).catch((e) => callback(e));
}

// somewhere else:
async function download(fileName) {
    const credentials = new SharedKeyCredential(STORAGE_ACCOUNT_NAME, ACCOUNT_ACCESS_KEY);
    const pipeline = StorageURL.newPipeline(credentials);
    const serviceURL = new ServiceURL(`https://${STORAGE_ACCOUNT_NAME}.blob.core.windows.net`, pipeline);

    const containerURL = ContainerURL.fromServiceURL(serviceURL, containerName);
    const blockBlobURL = BlockBlobURL.fromContainerURL(containerURL, fileName);
    //console.log(`Blobs in container:`);
    //await showBlobNames(aborter, containerURL);

    const downloadResponse = await url.download(aborter, 0);
    const downloadedContent = downloadResponse.readableStreamBody.read().toString();
    console.log(`Downloaded file:\n "${downloadedContent}"`);

    fs.writeFile(fileName, downloadedContent, function (err) {
        if (err) {
            callback(err);
        }
        callback(null);
    });
}

但是同样的错误。 也许错误是由于我在await中使用了async.waterfall吗?

您能帮我解决这个问题吗? 谢谢

0 个答案:

没有答案