angularjs - 如何使用 imgURL 一次从 Amazon S3 检索多个图像?

标签 angularjs image amazon-s3 aws-sdk-js

我想根据图像 URL 从 Amazon S3 一次性检索图像列表。

目前我可以使用以下代码获取单个图像:-

     AWS.config.update({
                    accessKeyId: accessKeyId,
                    secretAccessKey: secretAccessKey
                });

                AWS.config.region = region;

                var bucketInstance = new AWS.S3();
                var params = {
                    Bucket: bucketName,
                    Key: awsImgUrl
                }
                bucketInstance.getObject(params, function (err, file) {
                    if (file) {
                        var dataSrc = "data:" + file.ContentType + ";base64," + EncodeData(file.Body);
                        callbackSuccess(dataSrc);
                    } else {
                        callbackSuccess("Error");
                    }
                });

EncodeData = function (data) {
        var str = data.reduce(function (a, b) { return a + String.fromCharCode(b) }, '');
        return btoa(str).replace(/.{76}(?=.)/g, '$&\n');
    }

在我的场景中,我有多个 S3 图像 url,例如 awsImgUrl1、awsImgUrl2..awsImgUrln。

如何一次性获取而不是一个一个地获取?

最佳答案

使用 S3 进行每次 api 调用时,您无法获取多于一张图像。但是,您可以并行进行多个调用。 使用 Promise 这很简单。

var bucketInstance = new AWS.S3();
var imageKeys = [ awsImgUrl1, awsImgUrl2, awsImgUrl3];

var promisesOfS3Objects = imageKeys.map(function(key) {
  return bucketInstance.getObject({
    Bucket: bucketName,
    Key: key
  }).promise()
    .then(function (file) {
      return "data:" + file.ContentType + ";base64," + EncodeData(file.Body);
    })
  })
Promise.all(promisesOfS3Objects)
.then(callbackSuccess) // callbackSuccess is called with an array of string
.catch(function() { callbackSuccess("Error") })

关于angularjs - 如何使用 imgURL 一次从 Amazon S3 检索多个图像?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52675661/

相关文章:

java - Android HttpURLConnection PUT 到 Amazon AWS S3 403 错误

python - 让客户端通过 Chalice 将图像上传到 S3 存储桶的最佳实践是什么?

javascript - Angularjs Post 不向 Spring JWT 发送 header

html - 无法附加 AngularJS 数组

java - 在 Java/Scala 中显示图像的简单方法是什么?

swift - 如何使用mlmodel对UIImage进行预测?

amazon-web-services - 在不使用自定义域的情况下在 AWS S3 上设置 HTTPS

angularjs - 使用 angularjs 和 ui-router,如何禁用一个 View 更改的导航器历史记录

javascript - 使用 Angular 将文本呈现为 html

iphone - 将增强现实照片放在手机上