将返回的S3对象放入数组中

时间:2015-12-09 14:57:14

标签: javascript amazon-s3 rxjs

也许我在思考这个问题,但我对我面临的问题感到困惑。我目前正在使用JavaScript S3 SDK从我的存储桶中删除一些模板。我使用promise来返回结果,但是,返回的数据是一堆对象,因为我循环遍历对象所在的文件路径列表。因为我使用的是promise,它只返回八十个返回的对象中的一个。所以我的解决方案是将返回的对象放在一个数组中,我可以将其作为我的承诺的返回数据传递。这是RX适用的东西吗?如果是的话,会推荐哪些方法?

var getTemplateParams = function (isoObj) {
return new Promise(function (resolve, reject) {
    var params;
    var paths = isoObj.filePathArr.Contents;
    for (var i = 0; i < paths.length; i++) {
        params = {
            Bucket: isoObj.filePathArr.Name,
            Key: paths[i].Key,
            ResponseContentEncoding: 'utf-8',
            ResponseContentType: 'ReadableStream'
        }
        s3.getObject(params, function (err, data) {
            if (err) {
                reject(err);
            } else {
                data['isoId'] = isoObj.isoId;
                data['templateId'] = isoObj.templateId;
                data['siteName'] = isoObj.siteName;
                data['domain'] = isoObj.domain;
                data['mainEmail'] = isoObj.mainEmail;
                data['mainPhone'] = isoObj.mainPhone;
                data['Name'] = isoObj.filePathArr.Name;

                // The data that is returned needs to get put into an array.
                resolve(data);
            }
        });
    };
})
}

提前感谢您提供的任何帮助。

2 个答案:

答案 0 :(得分:1)

在RxJS中,它看起来像这样:

var getTemplateParams = function (isoObj) {

  //Turns this callback function into a function that returns a observable
  var getObjectObservable = Rx.Observable.fromNodeCallback(s3.getObject);

  //Wrap the paths into an Observable
  return Rx.Observable.fromArray(isoObj.filePathArr.Contents)

           //Convert the paths in to param objects
           .map(function(path) {
             return {
               Bucket: isoObject.filePathArr.Name
               Key: path.Key,
               ResponseContentEncoding: 'utf-8',
               ResponseContentType: 'ReadableStream'
             };
           })
           //Have each request sent out to the server
           .flatMap(getObjectObservable, 
                    //Process each result
                    function(path, data) {

                //Don't fully understand why you are copying all the data 
                //from isoObject into the data object.

                data['isoId'] = isoObj.isoId;
                data['templateId'] = isoObj.templateId;
                data['siteName'] = isoObj.siteName;
                data['domain'] = isoObj.domain;
                data['mainEmail'] = isoObj.mainEmail;
                data['mainPhone'] = isoObj.mainPhone;
                data['Name'] = isoObj.filePathArr.Name;
                return data;
           })

           //Flatten all the results into an Array.
           .toArray();
}

答案 1 :(得分:0)

我想我会这样做。 使每个s3请求成为一个承诺,然后使用Promise.all来解决所有的promises,它将promises的结果作为数组返回 刚刚找到我之前使用过的代码。我使用蓝鸟,但大多数承诺框架应该能够做到这一点

return Promise.all(photos.map(function(photo) {
  var params = 'bucketinfo'
  return s3.getSignedUrlAsync('getObject', params)
    .then(function(url) {
      return url;
    });
}));