gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-27 23:35:15 24 4
gpt4 key购买 nike

也许我想得太多了,但我对我面临的问题感到困惑。我目前正在使用 JavaScript S3 SDK 从我的存储桶中提取一些模板。我使用 promise 返回结果,但是,返回的数据是一堆对象,因为我正在循环访问对象所在的文件路径列表。因为我使用的是 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);
}
});
};
})
}

预先感谢您提供的任何帮助。

最佳答案

在 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();
}

关于javascript - 将返回的 S3 对象放入数组中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34181937/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com