Skip to content

Commit 3581b91

Browse files
committed
fix(cos): optimize createBucket method
1 parent 6b37ad5 commit 3581b91

File tree

2 files changed

+21
-31
lines changed

2 files changed

+21
-31
lines changed

__tests__/cos.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,8 +77,8 @@ describe('Cos', () => {
7777
const res = await cos.deploy({ ...inputs, bucket: '1234567890' });
7878
expect(res).toBe(undefined);
7979
} catch (err) {
80-
console.log(JSON.stringify(err));
8180
expect(err.type).toBe('API_COS_putBucket');
81+
expect(err.displayMsg).toBe('Bucket should format as "test-1250000000".');
8282
}
8383
});
8484

src/modules/cos/index.ts

Lines changed: 20 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,8 @@ export default class Cos {
9191
credentials: CapiCredentials;
9292
region: RegionType;
9393
cosClient: COS;
94+
retryTimes: number;
95+
maxRetryTimes: number;
9496

9597
constructor(credentials: CapiCredentials = {}, region: RegionType = 'ap-guangzhou') {
9698
this.region = region;
@@ -103,6 +105,10 @@ export default class Cos {
103105
this.credentials.XCosSecurityToken = credentials.Token;
104106
}
105107
this.cosClient = new COS(this.credentials);
108+
109+
// 支持 CreateBucket 重试一次
110+
this.retryTimes = 0;
111+
this.maxRetryTimes = 1;
106112
}
107113

108114
async isBucketExist(bucket: string) {
@@ -118,20 +124,22 @@ export default class Cos {
118124
}
119125

120126
async createBucket(inputs: CosCreateBucketInputs = {}) {
121-
// 在创建之前,检查是否存在
122-
const exist = await this.isBucketExist(inputs.bucket!);
123-
if (exist) {
124-
return true;
127+
// TODO: HeadBucket 请求如果 404 COS 会缓存,暂时不能使用,只能直接调用 CreateBucket
128+
// const exist = await this.isBucketExist(inputs.bucket!);
129+
// if (exist) {
130+
// return true;
131+
// }
132+
if (this.retryTimes === 0) {
133+
console.log(`Creating bucket ${inputs.bucket}`);
125134
}
126-
// 不存在就尝试创建
127-
console.log(`Creating bucket ${inputs.bucket}`);
128135
const createParams = {
129136
Bucket: inputs.bucket!,
130137
Region: this.region,
131138
};
132139

133140
try {
134141
await this.cosClient.putBucket(createParams);
142+
this.retryTimes = 0;
135143
} catch (err) {
136144
const e = convertCosError(err);
137145
if (e.code === 'BucketAlreadyExists' || e.code === 'BucketAlreadyOwnedByYou') {
@@ -141,30 +149,12 @@ export default class Cos {
141149
console.log(`Bucket ${inputs.bucket} already exist.`);
142150
}
143151
} else {
144-
// TODO: cos在云函数中可能出现ECONNRESET错误,没办法具体定位,初步猜测是客户端问题,是函数启动网络还没准备好,这个还不确定,所以在这里做兼容
145-
if (e?.message?.includes('ECONNRESET')) {
146-
// 检查bucket是否存在
147-
try {
148-
const isHave = await this.cosClient.headBucket(createParams);
149-
if (isHave.statusCode === 200) {
150-
if (!inputs.force) {
151-
throw new ApiError({
152-
type: `API_COS_headBucket`,
153-
message: `Bucket ${inputs.bucket} already exist`,
154-
});
155-
} else {
156-
console.log(`Bucket ${inputs.bucket} already exist`);
157-
}
158-
} else {
159-
throw new ApiError({
160-
type: `API_COS_headBucket`,
161-
message: `Could not find bucket ${inputs.bucket}`,
162-
});
163-
}
164-
} catch (errAgain) {
165-
throw constructCosError(`API_COS_headBucket`, errAgain);
166-
}
152+
// 失败重试 1 次,如果再次出错,正常处理
153+
if (this.retryTimes < this.maxRetryTimes) {
154+
this.retryTimes++;
155+
await this.createBucket(inputs);
167156
} else {
157+
this.retryTimes = 0;
168158
throw constructCosError(`API_COS_putBucket`, err);
169159
}
170160
}
@@ -531,7 +521,7 @@ export default class Cos {
531521
await this.flushBucketFiles(bucket);
532522
}
533523

534-
console.log(`Uploding files to bucket ${bucket}`);
524+
console.log(`Uploading files to bucket ${bucket}`);
535525

536526
/** 上传文件夹 */
537527
if (inputs.dir && fs.existsSync(inputs.dir)) {

0 commit comments

Comments
 (0)