core: s3: use lib-storage

This commit is contained in:
undefined committed 2022-10-09 13:17:39 +08:00
1 parent 52422c33b3
commit 0593d4d86a
2 files changed
+21 -7

No files matched your search

+19 -6
View File
@@ -5,6 +5,7 @@ import {
CopyObjectCommand, DeleteObjectCommand, DeleteObjectsCommand,
GetObjectCommand, HeadObjectCommand, PutObjectCommand, S3Client,
} from '@aws-sdk/client-s3';
import { Upload } from '@aws-sdk/lib-storage';
import { createPresignedPost } from '@aws-sdk/s3-presigned-post';
import { getSignedUrl } from '@aws-sdk/s3-request-presigner';
import {
@@ -114,12 +115,24 @@ class RemoteStorageService {
async put(target: string, file: string | Buffer | Readable, meta: Record<string, string> = {}) {
if (target.includes('..') || target.includes('//')) throw new Error('Invalid path');
if (typeof file === 'string') file = createReadStream(file);
await this.client.send(new PutObjectCommand({
Bucket: this.opts.bucket,
Body: file,
Key: target,
Metadata: meta,
}));
if (file instanceof Buffer && file.byteLength <= 5 * 1024 * 1024) {
await this.client.send(new PutObjectCommand({
Bucket: this.opts.bucket,
Body: file,
Key: target,
Metadata: meta,
}));
} else {
const upload = new Upload({
client: this.client,
params: { Bucket: this.opts.bucket, Key: target, Body: file },
tags: [],
queueSize: 4,
partSize: 1024 * 1024 * 5,
leavePartsOnError: false,
});
await upload.done();
}
}
async get(target: string, path?: string) {