From 2468fcc239702a343238bef8c7c3ec16886960eb Mon Sep 17 00:00:00 2001 From: fengmk2 Date: Mon, 20 May 2024 16:16:07 +0800 Subject: [PATCH] feat: format Object key (#41) remove `/` prefix string --- .github/workflows/nodejs.yml | 2 +- src/client/Oss.ts | 25 ++++++++++++++++++------- test/unit/client/Oss.test.ts | 3 ++- 3 files changed, 21 insertions(+), 9 deletions(-) diff --git a/.github/workflows/nodejs.yml b/.github/workflows/nodejs.yml index e54aacf..6ec122f 100644 --- a/.github/workflows/nodejs.yml +++ b/.github/workflows/nodejs.yml @@ -40,7 +40,7 @@ jobs: strategy: fail-fast: false matrix: - node-version: [14, 16, 18, 20] + node-version: [14, 16, 18, 20, 22] os: [ubuntu-latest] steps: 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) {