From 1f7aff07567281fa2d3b80bc5801a39903c66d24 Mon Sep 17 00:00:00 2001 From: fengmk2 Date: Mon, 20 May 2024 15:48:11 +0800 Subject: [PATCH] feat: format Object key remove `/` prefix string --- src/client/Oss.ts | 25 ++++++++++++++++++------- test/unit/client/Oss.test.ts | 3 ++- 2 files changed, 20 insertions(+), 8 deletions(-) diff --git a/src/client/Oss.ts b/src/client/Oss.ts index a73193a..a8ec7f4 100644 --- a/src/client/Oss.ts +++ b/src/client/Oss.ts @@ -46,14 +46,20 @@ export class Oss extends API { this.options = options; } + // https://github.com/node-modules/oss-client/blob/master/src/OSSObject.ts#L984 + #objectKey(key: string) { + return key.replace(/^\/+/, ''); + } + private async* putObjectIterator(request: PutObjectRequest): AsyncGenerator { + const key = this.#objectKey(request.key); let hasChunk = false; for await (const chunk of request.body) { hasChunk = true; const req = new PutObjectInput(); req.setStoreName(request.storeName); req.setBucket(request.bucket); - req.setKey(request.key); + req.setKey(key); req.setContentLength(request.contentLength); if (request.acl) { req.setAcl(request.acl); @@ -88,7 +94,7 @@ export class Oss extends API { const req = new PutObjectInput(); req.setStoreName(request.storeName); req.setBucket(request.bucket); - req.setKey(request.key); + req.setKey(key); req.setContentLength(request.contentLength); if (request.acl) { req.setAcl(request.acl); @@ -152,10 +158,11 @@ export class Oss extends API { } async get(request: GetObjectRequest): Promise { + const key = this.#objectKey(request.key); const req = new GetObjectInput(); req.setStoreName(request.storeName); req.setBucket(request.bucket); - req.setKey(request.key); + req.setKey(key); if (request.expectedBucketOwner) { req.setExpectedBucketOwner(request.expectedBucketOwner); } @@ -232,10 +239,11 @@ export class Oss extends API { } async copy(request: CopyObjectRequest): Promise { + const key = this.#objectKey(request.key); const req = new CopyObjectInput(); req.setStoreName(request.storeName); req.setBucket(request.bucket); - req.setKey(request.key); + req.setKey(key); const copySource = new CopySource(); req.setCopySource(copySource); copySource.setCopySourceBucket(request.copySource.copySourceBucket); @@ -261,10 +269,11 @@ export class Oss extends API { } async head(request: HeadObjectRequest): Promise { + const key = this.#objectKey(request.key); const req = new HeadObjectInput(); req.setStoreName(request.storeName); req.setBucket(request.bucket); - req.setKey(request.key); + req.setKey(key); if (request.checksumMode) { req.setChecksumMode(request.checksumMode); } @@ -316,10 +325,11 @@ export class Oss extends API { } async delete(request: DeleteObjectRequest): Promise { + const key = this.#objectKey(request.key); const req = new DeleteObjectInput(); req.setStoreName(request.storeName); req.setBucket(request.bucket); - req.setKey(request.key); + req.setKey(key); if (request.requestPayer) { req.setRequestPayer(request.requestPayer); } @@ -376,10 +386,11 @@ export class Oss extends API { } async signUrl(request: SignUrlRequest): Promise { + const key = this.#objectKey(request.key); const req = new SignURLInput(); req.setStoreName(request.storeName); req.setBucket(request.bucket); - req.setKey(request.key); + req.setKey(key); req.setMethod(request.method); req.setExpiredInSec(request.expiredInSec); diff --git a/test/unit/client/Oss.test.ts b/test/unit/client/Oss.test.ts index 82e54b9..79d9d87 100644 --- a/test/unit/client/Oss.test.ts +++ b/test/unit/client/Oss.test.ts @@ -19,7 +19,8 @@ describe.skip('client/Oss.test.ts', () => { const res = await client.oss.get({ storeName: 'oss_demo', bucket: 'antsys-tnpmbuild', - key: 'test.txt', + // support prefix with `/` + key: '/test.txt', }); const buf: Uint8Array[] = []; for await (const chunk of res.object) {