mirror of
https://github.com/shumaiOne/shumai.git
synced 2026-12-02 05:54:44 +00:00
Merge pull request #246 from shumaiOne/fix/s3-service-property-syntax-and-stream-support
fix(s3): enforce property syntax in S3Service interface and fix ReadableStream handling in S3StorageService
This commit is contained in:
@@ -154,6 +154,41 @@ describe('S3Service implementations', () => {
|
|||||||
expect(url2).toBe('http://download-url-2')
|
expect(url2).toBe('http://download-url-2')
|
||||||
expect(s3PresignSpy).toHaveBeenCalledTimes(2)
|
expect(s3PresignSpy).toHaveBeenCalledTimes(2)
|
||||||
})
|
})
|
||||||
|
|
||||||
|
it('should handle ReadableStream in putObject by wrapping in Response', async () => {
|
||||||
|
const s3 = new S3StorageService('http://localhost:9000', 'key', 'secret', 'test-bucket')
|
||||||
|
s3WriteSpy.mockClear()
|
||||||
|
|
||||||
|
const stream = new ReadableStream({
|
||||||
|
start(controller) {
|
||||||
|
controller.enqueue(new TextEncoder().encode('stream content'))
|
||||||
|
controller.close()
|
||||||
|
},
|
||||||
|
})
|
||||||
|
|
||||||
|
await s3.putObject('test-bucket', 'file.txt', stream, 14, 'text/plain')
|
||||||
|
|
||||||
|
expect(s3WriteSpy).toHaveBeenCalledWith(
|
||||||
|
'file.txt',
|
||||||
|
expect.any(Response),
|
||||||
|
expect.objectContaining({ bucket: 'test-bucket', type: 'text/plain' }),
|
||||||
|
)
|
||||||
|
})
|
||||||
|
|
||||||
|
it('should handle ArrayBuffer in putObject', async () => {
|
||||||
|
const s3 = new S3StorageService('http://localhost:9000', 'key', 'secret', 'test-bucket')
|
||||||
|
s3WriteSpy.mockClear()
|
||||||
|
|
||||||
|
const arrayBuffer = new TextEncoder().encode('array buffer content').buffer
|
||||||
|
|
||||||
|
await s3.putObject('test-bucket', 'file.txt', arrayBuffer, 20, 'text/plain')
|
||||||
|
|
||||||
|
expect(s3WriteSpy).toHaveBeenCalledWith(
|
||||||
|
'file.txt',
|
||||||
|
arrayBuffer,
|
||||||
|
expect.objectContaining({ bucket: 'test-bucket', type: 'text/plain' }),
|
||||||
|
)
|
||||||
|
})
|
||||||
})
|
})
|
||||||
|
|
||||||
describe('LocalStorageService', () => {
|
describe('LocalStorageService', () => {
|
||||||
|
|||||||
+20
-16
@@ -33,29 +33,29 @@ export interface S3Object {
|
|||||||
}
|
}
|
||||||
|
|
||||||
export interface S3Service {
|
export interface S3Service {
|
||||||
getObjectSize(bucket: string, key: string): Promise<number>
|
getObjectSize: (bucket: string, key: string) => Promise<number>
|
||||||
putObject(
|
putObject: (
|
||||||
bucket: string,
|
bucket: string,
|
||||||
key: string,
|
key: string,
|
||||||
body: Buffer | Uint8Array | ArrayBuffer | string | ReadableStream,
|
body: Buffer | Uint8Array | ArrayBuffer | string | ReadableStream,
|
||||||
size: number,
|
size: number,
|
||||||
contentType?: string,
|
contentType?: string,
|
||||||
): Promise<void>
|
) => Promise<void>
|
||||||
getObject(bucket: string, key: string): Promise<S3Object>
|
getObject: (bucket: string, key: string) => Promise<S3Object>
|
||||||
copyObject(
|
copyObject: (
|
||||||
sourceBucket: string,
|
sourceBucket: string,
|
||||||
sourceKey: string,
|
sourceKey: string,
|
||||||
destBucket: string,
|
destBucket: string,
|
||||||
destKey: string,
|
destKey: string,
|
||||||
): Promise<void>
|
) => Promise<void>
|
||||||
downloadToFile(bucket: string, key: string, filePath: string): Promise<void>
|
downloadToFile: (bucket: string, key: string, filePath: string) => Promise<void>
|
||||||
deleteObject(bucket: string, key: string): Promise<number>
|
deleteObject: (bucket: string, key: string) => Promise<number>
|
||||||
deletePrefix(bucket: string, prefix: string): Promise<number>
|
deletePrefix: (bucket: string, prefix: string) => Promise<number>
|
||||||
headObject(bucket: string, key: string): Promise<ObjectInfo>
|
headObject: (bucket: string, key: string) => Promise<ObjectInfo>
|
||||||
listObjects(bucket: string, prefix: string): Promise<string[]>
|
listObjects: (bucket: string, prefix: string) => Promise<string[]>
|
||||||
uploadFile(filePath: string, contentType: string): Promise<string>
|
uploadFile: (filePath: string, contentType: string) => Promise<string>
|
||||||
uploadFileToKey(filePath: string, key: string, contentType: string): Promise<void>
|
uploadFileToKey: (filePath: string, key: string, contentType: string) => Promise<void>
|
||||||
presign(bucket: string, key: string, method: string, download?: boolean): Promise<string>
|
presign: (bucket: string, key: string, method: string, download?: boolean) => Promise<string>
|
||||||
}
|
}
|
||||||
|
|
||||||
export class S3StorageService implements S3Service {
|
export class S3StorageService implements S3Service {
|
||||||
@@ -87,11 +87,15 @@ export class S3StorageService implements S3Service {
|
|||||||
async putObject(
|
async putObject(
|
||||||
bucket: string,
|
bucket: string,
|
||||||
key: string,
|
key: string,
|
||||||
body: Buffer | Uint8Array | string,
|
body: Buffer | Uint8Array | ArrayBuffer | string | ReadableStream,
|
||||||
_size: number,
|
_size: number,
|
||||||
contentType?: string,
|
contentType?: string,
|
||||||
): Promise<void> {
|
): Promise<void> {
|
||||||
await this.client.write(key, body, {
|
const payload =
|
||||||
|
body && typeof body === 'object' && 'getReader' in body
|
||||||
|
? new Response(body as ReadableStream)
|
||||||
|
: body
|
||||||
|
await this.client.write(key, payload, {
|
||||||
bucket,
|
bucket,
|
||||||
type: contentType,
|
type: contentType,
|
||||||
})
|
})
|
||||||
|
|||||||
Reference in New Issue
Block a user