Skip to content

Commit

Permalink
feat: format Object key (#41)
Browse files Browse the repository at this point in the history
remove `/` prefix string
  • Loading branch information
fengmk2 authored May 20, 2024
1 parent dab2e15 commit 2468fcc
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 9 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/nodejs.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
25 changes: 18 additions & 7 deletions src/client/Oss.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<PutObjectInput> {
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);
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -152,10 +158,11 @@ export class Oss extends API {
}

async get(request: GetObjectRequest): Promise<GetObjectResponse> {
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);
}
Expand Down Expand Up @@ -232,10 +239,11 @@ export class Oss extends API {
}

async copy(request: CopyObjectRequest): Promise<CopyObjectOutput.AsObject> {
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);
Expand All @@ -261,10 +269,11 @@ export class Oss extends API {
}

async head(request: HeadObjectRequest): Promise<HeadObjectOutput.AsObject> {
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);
}
Expand Down Expand Up @@ -316,10 +325,11 @@ export class Oss extends API {
}

async delete(request: DeleteObjectRequest): Promise<DeleteObjectOutput.AsObject> {
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);
}
Expand Down Expand Up @@ -376,10 +386,11 @@ export class Oss extends API {
}

async signUrl(request: SignUrlRequest): Promise<SignURLOutput.AsObject> {
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);

Expand Down
3 changes: 2 additions & 1 deletion test/unit/client/Oss.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down

0 comments on commit 2468fcc

Please sign in to comment.