如何通过Javascript SDK获取amazon S3存储桶的默认加密设置

问题描述 投票:0回答:1

我想通过使用AWS SDK for javascript列出我所有s3存储桶的默认加密设置(无,AES-256,AWS-KMS)。(2.305)

首先,我使用listBuckets获取所有存储桶,然后使用getBucketEncryption函数迭代所有存储桶。

我的问题是getBucketEncryption是异步的,因此我使用promises等待处理所有请求。所以我将所有的promisses添加到列表中并使用Promise.all()等待它们。最后,当我得到所有结果时,我不知道结果属于哪个桶,因为getBucketEncryption不会返回带有数据的存储桶名称,并且promisses可以按任何顺序解析。

不知何故,我需要围绕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
javascript amazon-web-services amazon-s3 promise aws-sdk
1个回答
0
投票

好消息.....这真的很简单。

values数组保证与bucketList数组一致,无论承诺的确定顺序如何。

所以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(...)封闭)values提供的每个reflect().bucket财产。
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(' '));
            }
        });
    }
});
© www.soinside.com 2019 - 2024. All rights reserved.