Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: format Object key #41

Merged
merged 2 commits into from
May 20, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading