Created
May 3, 2018 09:02
-
-
Save gongzili456/4f185a3207dbaa3e03743d4d93e32847 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const COS = require('cos-nodejs-sdk-v5') | |
const params = { | |
AppId: '', | |
SecretId: '', | |
SecretKey: '', | |
} | |
const cos = new COS(params) | |
const bucketParams = { | |
Bucket: '', | |
Region: 'ap-beijing-1', | |
} | |
function listBuckets() { | |
return new Promise((resolve, reject) => { | |
cos.getBucket(bucketParams, function(err, data) { | |
if (err) { | |
console.log(err) | |
return reject(err) | |
} | |
return resolve(data) | |
}) | |
}) | |
} | |
function del(data) { | |
return new Promise((resolve, reject) => { | |
const delParams = Object.assign({}, bucketParams, { | |
Objects: data.Contents.map(c => ({ Key: c.Key })), | |
}) | |
cos.deleteMultipleObject(delParams, function(err, d) { | |
if (err) { | |
console.log(err) | |
return reject(err) | |
} | |
console.log('deleted: ', d) | |
return resolve(data.Contents.length) | |
}) | |
}) | |
} | |
function task() { | |
return new Promise((resolve, reject) => { | |
return listBuckets() | |
.then(del) | |
.then(length => { | |
return length ? task() : Promise.resolve() | |
}) | |
}) | |
} | |
task().then() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment