Skip to content

Commit 394aded

Browse files
committed
fix(cos): check bucket exist before creating
1 parent 8facc16 commit 394aded

File tree

1 file changed

+18
-0
lines changed

1 file changed

+18
-0
lines changed

src/modules/cos/index.ts

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,25 @@ export default class Cos {
8888
this.cosClient = new COS(this.credentials);
8989
}
9090

91+
async isBucketExist(bucket: string) {
92+
try {
93+
const isHave = await this.cosClient.headBucket({
94+
Bucket: bucket,
95+
Region: this.region,
96+
});
97+
return isHave.statusCode === 200;
98+
} catch (e) {
99+
return false;
100+
}
101+
}
102+
91103
async createBucket(inputs: CosCreateBucketInputs = {}) {
104+
// 在创建之前,检查是否存在
105+
const exist = await this.isBucketExist(inputs.bucket!);
106+
if (exist) {
107+
return true;
108+
}
109+
// 不存在就尝试创建
92110
console.log(`Creating bucket ${inputs.bucket}`);
93111
const createParams = {
94112
Bucket: inputs.bucket!,

0 commit comments

Comments
 (0)