Skip to content

Commit

Permalink
fix: allow oss put empty file (#34)
Browse files Browse the repository at this point in the history
  • Loading branch information
fengmk2 authored Aug 30, 2023
1 parent 6b7ddac commit 0c09afb
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 1 deletion.
36 changes: 36 additions & 0 deletions src/client/Oss.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,9 @@ export default class Oss extends API {
}

private async* putObjectIterator(request: PutObjectRequest): AsyncGenerator<PutObjectInput> {
let hasChunk = false;
for await (const chunk of request.body) {
hasChunk = true;
const req = new PutObjectInput();
req.setStoreName(request.storeName);
req.setBucket(request.bucket);
Expand Down Expand Up @@ -80,6 +82,40 @@ export default class Oss extends API {
req.setBody(chunk);
yield req;
}

if (!hasChunk) {
// put empty file
const req = new PutObjectInput();
req.setStoreName(request.storeName);
req.setBucket(request.bucket);
req.setKey(request.key);
req.setContentLength(request.contentLength);
if (request.acl) {
req.setAcl(request.acl);
}
if (request.bucketKeyEnabled) {
req.setBucketKeyEnabled(request.bucketKeyEnabled);
}
if (request.cacheControl) {
req.setCacheControl(request.cacheControl);
}
if (request.contentDisposition) {
req.setContentDisposition(request.contentDisposition);
}
if (request.contentEncoding) {
req.setContentEncoding(request.contentEncoding);
}
if (request.expires) {
req.setExpires(request.expires);
}
if (request.serverSideEncryption) {
req.setServerSideEncryption(request.serverSideEncryption);
}
if (request.signedUrl) {
req.setSignedUrl(request.signedUrl);
}
yield req;
}
}

async put(request: PutObjectRequest): Promise<PutObjectOutput.AsObject> {
Expand Down
27 changes: 26 additions & 1 deletion test/unit/client/Oss.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import assert from 'node:assert';
import { strict as assert } from 'node:assert';
import { Readable } from 'node:stream';
import { createReadStream } from 'node:fs';
import path from 'node:path';
import crypto from 'node:crypto';
import { Client } from '../../../src';

Expand Down Expand Up @@ -27,6 +29,29 @@ describe.skip('client/Oss.test.ts', () => {
assert(data === 'hello world');
});

it('test put empty file', async () => {
const fileStream = createReadStream(path.join(__dirname, 'fixtures/empty-file.txt'));
const hello = await client.oss.put({
storeName: 'oss_demo',
bucket: 'antsys-tnpmbuild',
key: 'test-empty-file.txt',
body: fileStream,
contentLength: 0,
});
assert(hello);
const res = await client.oss.get({
storeName: 'oss_demo',
bucket: 'antsys-tnpmbuild',
key: 'test-empty-file.txt',
});
const buf: Uint8Array[] = [];
for await (const chunk of res.object) {
buf.push(chunk);
}
const data = Buffer.concat(buf).toString();
assert.equal(data, '');
});

it('test put large object', async () => {
const buf = new Array(1024);
buf.fill(Buffer.alloc(1024).fill('a'));
Expand Down
Empty file.

0 comments on commit 0c09afb

Please sign in to comment.