繁体   English   中英

如何从 Node.js 中的 AWS getObject 调用返回变量

[英]How to Return variable from AWS getObject call in Node.js

我正在开发一个 Node.js 项目,该项目依赖于来自 AWS 的getObject的响应。 目前,我可以访问我需要的数据并将其存储在变量 ( header ) 中,但不知道如何使其在主函数中可用。

TileStreamerS3.prototype.Init = function(filepath, index, s3config){
   var retval = false; 
   AWS.config.update({accessKeyId: s3config.access_key, secretAccessKey: s3config.secret_key});
   var blc = new BlockLibraryConfigs(); 
   var awsConfig = blc.awsConfig; 
   AWS.config.update({region: awsConfig.region}); 

   var aws = new AWS.S3(); 
   var params = {
       Bucket: s3config.bucket, 
       Key: s3config.tile_directory + filepath, 
       Range: 'bytes=0-27'
   };



 aws.getObject(params, function(err, data){
       if(err == null){
            var header = bufferpack.unpack('<7I', data.Body);
            return header; 
       }
   }); 
}; 

如何返回header变量? 我看到了一个使用 JQuery 的可能解决方案,但我在 Windows 上并且在让 JQuery 与 Node.js 一起工作时遇到问题。 如果有更好的方法,我不想走这条路。

更新:我知道有多个关于从异步函数返回值的问题。 我认为让我失望的是aws.getObject()是一个将另一个函数作为参数的函数。 我想从作为aws.getObject()函数参数的函数返回header值。 此外,我无权更改getObject()函数。

要了解有关异步编程的第一件事是,你不能真正从回调将结果返回给你的主要功能或其他地方。 这是有关异步编码的教程,可帮助您入门


至于你目前的情况,我会使用Async Waterfall来处理。 Async将通过在调用下一个函数之前等待它们完成来强制其他异步函数同步运行(将前一个函数的输出作为下一个函数的参数)。

下面是一个例子:

TileStreamerS3.prototype.Init = function(filepath, index, s3config, callback) {
// pass a callback function as a parameter

    var retval = false;
    AWS.config.update({
        accessKeyId: s3config.access_key,
        secretAccessKey: s3config.secret_key
    });
    var blc = new BlockLibraryConfigs();
    var awsConfig = blc.awsConfig;
    AWS.config.update({ region: awsConfig.region });

    var aws = new AWS.S3();
    var params = {
        Bucket: s3config.bucket,
        Key: s3config.tile_directory + filepath,
        Range: 'bytes=0-27'
    };

    async.waterfall([function(callback) {

        aws.getObject(params, function(err, data) {
            if (!err) {
                var header = bufferpack.unpack('<7I', data.Body);

                callback(null, header); // using this we are passing the control to the
                // next function in the array
            }
        });

    }, function(header, callback) { // this is the function which will be called 
        // synchronously from aws.getObject's callback

        // use the header value passed from the previous function
        // do the next bit of asynchronous computation

        callback(null, output_if_any); // call the next function or the final callback

    }], callback); // attach the callback function to the async.waterfall

    // due to the async nature of the code, anything written after the waterfall will
    // be executed before the above code! So if you try to set any variable within the 
    // async function's callback and console.log it here, you will get undefined.

};

这里发生的是,使用async.waterfall我们可以创建一个函数列表(每个函数可能包含异步函数),以便一个接一个地同步调用。 当所有的函数都执行完毕后,最后的callback函数被调用,输出results

现在,当您调用函数TileStreamerS3.Init ,您可以这样做:

TileStreamerS3Object.Init(filepath, index, s3config, function(err, results) {

    if (err) {
        // handle the error
    } else {
        // do whatever you want with the results
        // and continue the processing
    }

});

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM