如何在Node JS中设置AWS s3存储桶版本控制?

时间:2018-12-10 13:05:58

标签: node.js amazon-s3

我正在使用以下代码来设置存储桶版本控制,但这给了我错误。如何在Node JS中设置AWS s3存储桶版本控制?

............................................... ................................................... ................................................... ................................................... ...............................

let params = {
    Bucket:"home",
    ACL:"public-read-write", 
    VersioningConfiguration: {
        MFADelete: "Disabled", 
        Status: "Enabled"
       }
    }

let callBack = (err,data)=>{
    if(err){
        console.log("--------- Home Bucket Creation Fail --------",err);
        callCreateBucket()
        // setTimeout(()=>{
        //     callCreateBucket()
        // },3000)
    }
    else 
        console.log("--------- Home Bucket Created with version setup --------",data);
}

function callCreateBucket(){
    console.log("---------callCreateBucket --------");
    createBucket (params , callBack)
}
function createBucket (param ,cb ){
    console.log("---------createBucket --------");
    setTimeout(()=>{
        s3.createBucket(param,cb); 
    },10000)

}

callCreateBucket()

2 个答案:

答案 0 :(得分:1)

在这种情况下,我将避免放置setTimetout函数。最好使用documentation中的以下代码模板:

// Load the SDK for JavaScript
var AWS = require('aws-sdk');
// Set the region 
AWS.config.update({region: 'REGION'});

// Create the parameters for calling createBucket
var bucketParams = {
   Bucket : process.argv[2]
};            

// Create S3 service object
s3 = new AWS.S3({apiVersion: '2006-03-01'});

// Call S3 to create the bucket
s3.createBucket(bucketParams, function(err, data) {
   if (err) {
      console.log("Error", err);
   } else {
      console.log("Success", data.Location);
   }
});

答案 1 :(得分:0)

首先,您需要使用createBucket创建存储桶,然后使用putBucketVersioning为存储桶启用版本控制。