|
1 | 1 | import { RegionType, CapiCredentials } from './../interface';
|
2 | 2 | import COS, {
|
3 | 3 | CORSRule,
|
| 4 | + CosSdkError, |
4 | 5 | LifecycleRule,
|
5 | 6 | PutBucketAclParams,
|
6 | 7 | PutBucketCorsParams,
|
@@ -36,53 +37,38 @@ import fs from 'fs';
|
36 | 37 | import { traverseDirSync } from '../../utils';
|
37 | 38 | import { ApiTypeError, ApiError } from '../../utils/error';
|
38 | 39 |
|
39 |
| -export interface CosError { |
40 |
| - error?: |
41 |
| - | { |
42 |
| - Code?: string; |
43 |
| - Message?: string; |
44 |
| - Stack?: string; |
45 |
| - RequestId?: string; |
46 |
| - } |
47 |
| - | string; |
48 |
| - code?: string; |
49 |
| - message?: string; |
50 |
| - stack?: string; |
51 |
| - requestId?: string; |
| 40 | +export interface CosInsideError { |
| 41 | + Code: string; |
| 42 | + Message: string; |
| 43 | + RequestId?: string; |
| 44 | + Resource?: string; |
| 45 | + TraceId?: string; |
52 | 46 | }
|
53 | 47 |
|
54 | 48 | /** 将 Cos error 转为统一的形式 */
|
55 |
| -export function convertCosError(err: CosError) { |
| 49 | +export function convertCosError(err: CosSdkError) { |
| 50 | + let { code } = err; |
| 51 | + const reqId = err?.headers && err?.headers['x-cos-request-id']; |
| 52 | + const traceId = err?.headers && err?.headers['x-cos-trace-id']; |
| 53 | + const msgSuffix = reqId ? ` (reqId: ${reqId}${traceId ? `, traceId: ${traceId}` : ''})` : ''; |
56 | 54 | if (typeof err.error === 'string') {
|
57 | 55 | return {
|
58 |
| - code: err.code!, |
59 |
| - message: err.message! ?? err.error, |
60 |
| - stack: err?.stack, |
61 |
| - reqId: err?.requestId, |
| 56 | + code, |
| 57 | + message: `${err.message ?? err.error}`, |
| 58 | + reqId, |
62 | 59 | };
|
63 | 60 | }
|
| 61 | + const error = err.error as CosInsideError; |
| 62 | + code = error?.Code || err.code; |
| 63 | + const message = `${error?.Message || err.message}${msgSuffix}`; |
64 | 64 | return {
|
65 |
| - code: err?.error?.Code ?? err.code!, |
66 |
| - message: err?.error?.Message |
67 |
| - ? `${err?.error?.Message} (reqId: ${err.error.RequestId})` |
68 |
| - : `${err.message!} (reqId: ${err.requestId!})`, |
69 |
| - stack: err?.stack ?? err?.error?.Stack!, |
70 |
| - reqId: err?.error?.RequestId ?? err.requestId!, |
| 65 | + code, |
| 66 | + message: `${message}`, |
| 67 | + reqId, |
71 | 68 | };
|
72 | 69 | }
|
73 | 70 |
|
74 |
| -function constructCosError( |
75 |
| - type: string, |
76 |
| - err: { |
77 |
| - error: { |
78 |
| - Code: string; |
79 |
| - Message: string; |
80 |
| - Stack: string; |
81 |
| - RequestId: string; |
82 |
| - }; |
83 |
| - stack: string; |
84 |
| - }, |
85 |
| -) { |
| 71 | +function constructCosError(type: string, err: CosSdkError) { |
86 | 72 | const e = convertCosError(err);
|
87 | 73 | return new ApiError({ type, ...e });
|
88 | 74 | }
|
@@ -179,17 +165,16 @@ export default class Cos {
|
179 | 165 | const accessControlPolicy: Exclude<typeof setAclParams.AccessControlPolicy, undefined> = {
|
180 | 166 | Owner: {
|
181 | 167 | ID: acp?.owner?.id!,
|
182 |
| - DisplayName: acp?.owner?.displayName!, |
183 | 168 | },
|
184 |
| - Grants: { |
185 |
| - Permission: acp?.grants?.permission!, |
186 |
| - // FIXME: dont have URI |
187 |
| - Grantee: { |
188 |
| - ID: acp?.grants?.grantee?.id!, |
189 |
| - DisplayName: acp.grants?.grantee?.displayName!, |
190 |
| - // URI: acp?.grants?.grantee?.uri!, |
| 169 | + Grants: [ |
| 170 | + { |
| 171 | + Permission: acp?.grants?.permission!, |
| 172 | + // FIXME: dont have URI |
| 173 | + Grantee: { |
| 174 | + ID: acp?.grants?.grantee?.id!, |
| 175 | + }, |
191 | 176 | },
|
192 |
| - }, |
| 177 | + ], |
193 | 178 | };
|
194 | 179 | setAclParams.AccessControlPolicy = accessControlPolicy;
|
195 | 180 | }
|
@@ -444,23 +429,28 @@ export default class Cos {
|
444 | 429 | }
|
445 | 430 |
|
446 | 431 | async getObjectUrl(inputs: CosGetObjectUrlInputs = {}) {
|
447 |
| - try { |
448 |
| - const res = await this.cosClient.getObjectUrl({ |
449 |
| - Bucket: inputs.bucket!, |
450 |
| - Region: this.region, |
451 |
| - Key: inputs.object!, |
452 |
| - // default request method is GET |
453 |
| - Method: inputs.method ?? 'GET', |
454 |
| - // default expire time is 15min |
455 |
| - Expires: inputs.expires ?? 900, |
456 |
| - // default is sign url |
457 |
| - Sign: inputs.sign === false ? false : true, |
458 |
| - }); |
459 |
| - // FIXME: Fuck you Cos SDK, res is not an object; |
460 |
| - return res as unknown as string; |
461 |
| - } catch (err) { |
462 |
| - throw constructCosError(`API_COS_getObjectUrl`, err); |
463 |
| - } |
| 432 | + return new Promise((resolve, reject) => { |
| 433 | + this.cosClient.getObjectUrl( |
| 434 | + { |
| 435 | + Bucket: inputs.bucket!, |
| 436 | + Region: this.region, |
| 437 | + Key: inputs.object!, |
| 438 | + // default request method is GET |
| 439 | + Method: inputs.method ?? 'GET', |
| 440 | + // default expire time is 15min |
| 441 | + Expires: inputs.expires ?? 900, |
| 442 | + // default is sign url |
| 443 | + Sign: inputs.sign === false ? false : true, |
| 444 | + }, |
| 445 | + (err, data) => { |
| 446 | + if (err) { |
| 447 | + reject(constructCosError(`API_COS_getObjectUrl`, err)); |
| 448 | + return; |
| 449 | + } |
| 450 | + resolve(data.Url); |
| 451 | + }, |
| 452 | + ); |
| 453 | + }); |
464 | 454 | }
|
465 | 455 |
|
466 | 456 | async getBucketObjects(bucket: string) {
|
|
0 commit comments