我想使用AWS SDK for javascript列出我的所有s3存储桶的默认加密设置(无、AES-256、AWS-KMS)。(2.305)
首先,我使用listBuckets获取所有存储桶,然后使用getBucketEncryption
函数迭代所有存储桶。
我的问题是getBucketEncryption
是异步的,因此我使用promises来等待所有请求的处理。因此,我将所有的承诺添加到一个列表中,并使用Promise.all()
等待它们。最后,当我得到所有结果时,我不知道结果属于哪个存储桶,因为getBucketEncryption
不会返回存储桶名称和数据,并且承诺可以以任何顺序解析。
不知何故,我需要在s3.getBucketEncryption
调用周围包装一个承诺,在那里我可以传递存储桶名称,以便我稍后可以对其进行评估……但是怎么做呢?
下面是代码:
console.log('Loading function');
const aws = require('aws-sdk');
const s3 = new aws.S3({ apiVersion: '2006-03-01', region: 'eu-west-1' });
function reflect(promise){
return promise.then(
function(v){ return {cryptSetting:v, status: "COMPLIANT" }},
function(e){ return {error:e, status: "NON_COMPLIANT"}});
}
s3.listBuckets({},function(err,data){
if (err){
console.log(err, err.stack); // an error occurred
}
else{
var bucketList = JSON.parse(JSON.stringify(data.Buckets));
var list = new Array();
for(let i in bucketList){
list.push(s3.getBucketEncryption({Bucket: bucketList[i].Name})
.promise());
}
Promise.all(list.map(reflect)).then(function(values) {
for(let i in values){
// at this point I do not have the bucket name any more
// because it's not included in the values array
console.log("Bucketname missing here " + values[i].cryptSetting
+ ' ' + values[i].status);
}
});
}
});
输出如下所示:
Loading function
Bucketname missing here [object Object] COMPLIANT
Bucketname missing here [object Object] COMPLIANT
Bucketname missing here [object Object] COMPLIANT
Bucketname missing here [object Object] COMPLIANT
Bucketname missing here [object Object] COMPLIANT
Bucketname missing here [object Object] COMPLIANT
Bucketname missing here [object Object] COMPLIANT
Bucketname missing here undefined NON_COMPLIANT
Bucketname missing here undefined NON_COMPLIANT
Bucketname missing here [object Object] COMPLIANT
Bucketname missing here undefined NON_COMPLIANT
发布于 2018-09-06 08:45:55
好消息……真的很简单。
保证values
数组与bucketList
数组一致,而不管promises的解决顺序如何。
因此,bucketList[i]
将与values[i]
相对应。
s3.listBuckets({}, function(err, data) {
if (err) {
console.log(err, err.stack); // an error occurred
} else {
var bucketList = JSON.parse(JSON.stringify(data.Buckets));
var promises = bucketList.map(function(b) {
return s3.getBucketEncryption({ 'Bucket':b.Name }).promise();
});
Promise.all(promises.map(reflect)).then(function(values) {
for(let i in values) {
// `values` is guaranteed to be congruous with `bucketList`.
// ie. `bucketList[i]` corresponds with `values[i]`.
console.log([bucketList[i].name, values[i].cryptSetting, values[i].status].join(' '));
}
});
}
});
另一种方法是:
reflect
上移到bucketList.map()
闭包中(在bucketList.map(...)
闭包中) reflect()
提供的每个具有.bucket
属性的values
。s3.listBuckets({}, function(err, data) {
if (err) {
console.log(err, err.stack); // an error occurred
} else {
var bucketList = JSON.parse(JSON.stringify(data.Buckets));
var promises = bucketList.map(function(b) {
var promise = s3.getBucketEncryption({ 'Bucket':b.Name }).promise();
return reflect(promise).then(function(value) {
return Object.assign(value, { 'bucket':b });
});
});
Promise.all(promises).then(function(values) {
for(let i in values) {
// each value now has a '.bucket' property.
console.log([values[i].bucket.name, values[i].cryptSetting, values[i].status].join(' '));
}
});
}
});
https://stackoverflow.com/questions/52187752
复制相似问题