简体   繁体   English

如何使用 Javascript 从 AWS S3 检索多个对象,然后在检索到它们时执行一个函数?

[英]How to retrieve multiple objects from AWS S3 using Javascript and then perform a function when they have been retrieved?

The code below will retrieve at least one object (possibly two) from AWS S3.下面的代码将从 AWS S3 中检索至少一个对象(可能是两个)。

I am using the AWS JS SDK and retrieve the objects from within a loop as there is noway to retrieve multiple objects with one request at this moment in time.我正在使用 AWS JS SDK 并从一个循环中检索对象,因为目前无法通过一个请求检索多个对象。

After the objects have been retrieved I want to do some image composition (the objects are images).检索到对象后,我想进行一些图像合成(对象是图像)。

My problem is that the rest of my code executes before I have successfully retrieved the objects.我的问题是我的其余代码在我成功检索对象之前执行。 I know this because objects remains unchanged when logged to the console.我知道这是因为objects在登录到控制台时保持不变。

How do I ensure I receive the objects from S3 before I attempt to carry out my other function to manipulate the images?在尝试执行其他功能来处理图像之前,如何确保从 S3 接收到对象?

var app = require('../application');

exports.generate = function (req, res) {

    objects = {
        logo: req.body.logo,
    }

    if (!req.body.background.startsWith('#')) {
        objects.background = req.body.background;
    }

    for (type in objects) {
        var params = {
            Bucket: "my-bucket", 
            Key: objects[type]
        };
        app.s3.getObject(params, function(err, data) {
            if (err) {
                console.log(err, err.stack);
            }
            else {
                objects[type] = data;
            }
        });
    }

    if (objects.background) {
        gm(objects.logo).append(objects.background).write('temp.jpg', function() {
            console.log('Logo and background have been appended');
        });
    }

    console.log(objects);
    console.log('Finished');
}   

The console logs the following控制台记录以下内容

{ logo: 'Original 106fm Logo #268390.jpg', background: 'test.jpg' }
Finished

When the images are retrieved the log should be showing the data body for each image.检索图像时,日志应显示每个图像的数据主体。

Key is to use Promise Objects关键是使用Promise对象

app.get('/api/template', (req, res) => {

  let promises = [getS3Object(`templates/x.tpl`),
              getS3Object(`templates/y.tpl`),
              getS3Object(`templates/z.tpl`),
              getS3Object('templates/b.tpl')];

    return Promise.all(promises)
     .then((pres) => {
       const iterable = [0, 1, 2, 3];

       for (let value of iterable) {
        if (!pres[value].statusCode) {
         res.send(pres[value]);
       }
      }
     })
     .catch((res) => {
      console.log(`Error Getting Templates: ${res}`);
   });
});    


const getS3Object = key => {
return new Promise((resolve, reject) => {
  s3.getObject({
      Key: key
  }, (err, data) => {
      if (err){
        resolve(err)
      } else {
        resolve(data.Body)
      }
    })
  })
}

This happens because your console.log calls are executing prior to download files.发生这种情况是因为您的console.log调用在下载文件之前执行。 What you can do is process files after receiving them.您可以做的是在收到文件后对其进行处理。

app.s3.getObject(params, function(err, data) {
    if (err) {
      console.log(err, err.stack);
    }
    else {
      objects[type] = data;
      //Here you have the downloaded files. Do your processing here
      console.log(objects);          
    }
});

Asynchronous behavior is what you should read more about.异步行为是您应该阅读的更多内容。

[Update] you can use following wrapper module to download multiple files [更新] 你可以使用下面的包装器模块来下载多个文件

Download multiple files using aws s3使用aws s3下载多个文件

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

相关问题 将对象从 AWS S3 移动到 MediaStore - Move objects from AWS S3 to MediaStore 如何使用 AWS Lambda 函数从 S3 解码 a.gz 文件? - How can I decode a .gz file from S3 using an AWS Lambda function? golang S3 客户端库是否有获取迭代器 function 来检索 S3 存储桶中的所有对象 - Does golang S3 client library has the get Iterator function to retrieve all the objects in S3 bucket 如何列出所有公开的 AWS S3 对象? - How do I list all AWS S3 objects that are public? 使用 NodeJS 将多个文件上传到 AWS S3 - Uploading multiple files to AWS S3 using NodeJS 从 AWS lambda function 中的 s3 存储桶中读取 .mdb 或 .accdb 文件并使用 python 将其转换为 excel 或 csv - Reading .mdb or .accdb file from s3 bucket in AWS lambda function and converting it into excel or csv using python 如何从 AWS CloudFront 和 S3 请求 gzip javascript 文件 - How do I Request a gzip javascript file from AWS CloudFront and S3 无法使用 AWS AppFlow 将 Google Ads 支持的对象提取到 AWS S3 - Unable to to ingest Google Ads supported objects to AWS S3 using AWS AppFlow 使用aws lambda将批处理数据从aws sqs存储到aws s3 - store batch data from aws sqs to aws s3 using aws lambda 如何使用 boto3 访问 AWS S3 数据 - How to access AWS S3 data using boto3
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM