diff --git a/src/bos/object.ts b/src/bos/object.ts index 273bfb1..7b50a2b 100644 --- a/src/bos/object.ts +++ b/src/bos/object.ts @@ -1,6 +1,7 @@ import fs from 'node:fs'; import {Readable} from 'node:stream'; import {Http} from '../shared/index.js'; +import {normalizeUrl} from '../utils/string.js'; export interface PutObjectOptions { headers?: Record; @@ -14,7 +15,7 @@ export class BosObjectClient { constructor(http: Http, objectKey: string) { this.http = http; - this.objectKey = objectKey; + this.objectKey = normalizeUrl(objectKey, false); } async get() { diff --git a/src/utils/string.ts b/src/utils/string.ts new file mode 100644 index 0000000..7f8d3b2 --- /dev/null +++ b/src/utils/string.ts @@ -0,0 +1,13 @@ +const URL_ESCAPE: Record = { + '!': '%21', + '\'': '%27', + '(': '%28', + ')': '%29', + '*': '%2A', +}; + +export const normalizeUrl = (value: string, encodeSlash = true) => { + const parts = value.split('/'); + const normalized = parts.map(v => v.replace(/[!'()*]/g, c => URL_ESCAPE[c])); + return normalized.join(encodeSlash ? '%2F' : '/'); +};