Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save gongzili456/4f185a3207dbaa3e03743d4d93e32847 to your computer and use it in GitHub Desktop.
Save gongzili456/4f185a3207dbaa3e03743d4d93e32847 to your computer and use it in GitHub Desktop.
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