如何从Node.js中的Amazon S3存储桶同步下载文件 [英] How to download a file from Amazon S3 bucket in node.js synchronously

查看:347
本文介绍了如何从Node.js中的Amazon S3存储桶同步下载文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我必须使用node.js从S3存储桶下载多个文件.为此,我必须编写for loop&调用s3.getObject(param)方法进行下载.下载文件后,我必须合并它们的内容.

I have to download multiple files from S3 bucket using node.js. For that I have to write a for loop & call the s3.getObject(param) method to download. After the files are downloaded I have to merge their contents.

我这样写:

var fileContentList = new ArrayList();

for(i=0; i<fileNameList.length i++){
    s3.getObject({ Bucket: "my-bucket", Key: fileNameList.get(i) }, function (error, data) {
    if (error != null) {
      alert("Failed to retrieve an object: " + error);
    } else {
      alert("Loaded " + data.ContentLength + " bytes");
      fileContentList.add(data.Body.toString());
    }
  }
);
}

//Do merging with the fileContentList.

但是由于s3.getObject是异步调用,因此当前线程在&上移动.在进行合并时,fileContentList不会添加任何内容.

But as s3.getObject is an asynchronous call the current thread moves on & nothing gets added to the fileContentList while I am doing the merging.

我该如何解决该问题?有什么主意吗?
他们在aws-sdk中使用任何同步方法来下载文件吗?

How can I solve the problem? Any idea?
Is their any synchronous method in aws-sdk to download file?

推荐答案

我已经解决了这个问题.尽管我还没有尝试过亚历山大·莱娜和安培的答案.塞巴斯蒂安,我相信他们提到的每个答案在这种情况下也将起作用.非常感谢他们的快速回复:

I have solved using this. Though I have not tried the answers of Alexander,Lena & Sébastian I believe each of the answers mentioned by them would also work in this case. Many many thanks to them for their quick reply:

Async.eachSeries(casCustomersList, function (customerName, next){
        if(casCustomersList.length>0 && customerName != customerId) {

            var paramToAws = {
                Bucket: bucketName,
                Key: folderPath +'applicationContext-security-' + customerName + '.xml'      //file name
            };
            AWSFileAccessManager.downloadFile(paramToAws, function (error, result) {
                if (error) {
                    next(error);
                } else {
                    customerApplicationContext.add(result.Body.toString());
                    next();
                }

            });
        } else{
            next();
        }

    }, function(err) {
       //Write the rest of your logic here to process synchronously as it is the callback function

    }

这篇关于如何从Node.js中的Amazon S3存储桶同步下载文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆