使用一组键将多个s3对象推入一个数组

时间:2018-04-30 14:26:29

标签: javascript arrays node.js loops amazon-s3

const AWS = require("aws-sdk");
const s3 = new AWS.S3();

function getAttachments(file, bucket) {
    var paramsArray = [];
    var attachmentArray = [];
    for (var a = 0; a < file.length; a++) {
        paramsArray.push({
            Bucket: bucket,
            Key: file[a]
        });
    }
    paramsArray.map(x = > s3.getObject(x, function (err, data) {
        if (err) {
            console.log(err);
        }
        attachmentArray.push({
            filename: x.Key,
            content: data.Body
        });
        console.log(attachmentArray)
    })
)
}

var attachments = ['a.doc', 'b.doc', 'c.doc', 'd.doc', 'e.doc']

getAttachments(attachments, 
'exampleBucket')

我想记录一个对象数组,每个对象都以密钥作为文件名,缓冲区字符串作为内容。但是,在此示例中,我正在记录附件数组中每个元素的大小增加的多个数组。

这些格式正确,例如:[{filename: a.doc, content: 'Buffer String'}],但我只想要一个数组,其中包含以该格式返回的5个对象。

谁能告诉我怎么做?

2 个答案:

答案 0 :(得分:0)

只需将您的代码更改为此代码:

&#13;
&#13;
const AWS = require("aws-sdk");
const s3 = new AWS.S3();
const attachments = ['a.doc', 'b.doc', 'c.doc', 'd.doc', 'e.doc']

getAttachments(attachments, bucket)
  .then(console.log)
  .catch(console.error);

function getAttachments(files, bucket) {
  return files.map(filename => getAttachment(filename, bucket));
}

function getAttachment(filename, bucket){
  return new Promise((resolve,reject) => {
    const params = {
            Bucket: bucket,
            Key: filename
        };
    s3.getObject(params, (error, data) => {
      if(error) return reject(error);
      return resolve({
        filename: filename,
        content: data.Body
      });
    });
  });
}
&#13;
&#13;
&#13;

或者您可以放弃getAttachments功能。 希望它有所帮助。

答案 1 :(得分:0)

这里是我试图从ascendon1 bucket

下载的文件名列表

这是AJAX请求

var data = {fileNames: ['1.txt','2.txt']};
      $.ajax({
                    type: "POST",
                    url: 'downloadFiles',
                    data: JSON.stringify(data),
                    processData: false,
                    dataType: "json",
                    contentType: 'application/json',
                    success:function(data){
                        alert(data);
                    }
                });

这是NodeJS代码

 app.post('/downloadFiles',function(req,res){

        var promises =[];
        var filesContent = [];
        var filesToDownload = req.body.fileNames
        for(var z=0; z<req.body.fileNames.length;z++){
            var fileName = filesToDownload[z];
            promises.push(getAttachment(fileName));
        }

        Promise.all(promises).then(function(data){
            for(var z=0; z<req.body.fileNames.length;z++){
                var contents={
                    filename: filesToDownload[z],
                    content: data[z].Body
                };
                filesContent.push(contents);
            }
            console.log(filesContent);

        }).catch(function(error){
            console.log(error.stack);
        })

    })

    function getAttachment(fileName){
        var params ={
            Bucket: "ascendon1", 
            Key: fileName
        }
        return s3.getObject(params).promise();
    }

这是最终输出,我们得到了我们在一个数组中请求的所有内容,即filesContent

output