返回 AiToEarn
s3.adapter.ts
根目录 / project / aitoearn-backend / libs / assets / src / adapters / s3.adapter.ts
1 import type { Readable } from 'node:stream'
2 import type { CopyObjectOptions, StorageGetObjectResult, StorageHeadResult } from '../storage-provider'
3 import { S3Service } from '@yikart/aws-s3'
4 import { StorageProvider } from '../storage-provider'
5
6 export class S3Adapter extends StorageProvider {
7 constructor(
8 private readonly s3Service: S3Service,
9 endpoint: string,
10 cdnEndpoint?: string,
11 ) {
12 super(endpoint, cdnEndpoint)
13 }
14
15 async putObject(objectPath: string, file: Buffer | Readable, contentType?: string): Promise<{ path: string }> {
16 return this.s3Service.putObject(objectPath, file, contentType)
17 }
18
19 async headObject(objectPath: string): Promise<StorageHeadResult> {
20 const result = await this.s3Service.headObject(objectPath)
21 return {
22 contentLength: result.ContentLength,
23 contentType: result.ContentType,
24 }
25 }
26
27 async putObjectFromUrl(url: string, objectPath: string): Promise<{ path: string, exists?: boolean }> {
28 return this.s3Service.putObjectFromUrl(url, objectPath)
29 }
30
31 async deleteObject(objectPath: string): Promise<void> {
32 await this.s3Service.deleteObject(objectPath)
33 }
34
35 async getUploadSignUrl(objectPath: string, contentType?: string, contentLength?: number, _callbackVars?: Record<string, string>): Promise<string> {
36 return this.s3Service.getUploadSignUrl(objectPath, contentType, contentLength)
37 }
38
39 async copyObject(objectPath: string, options: CopyObjectOptions): Promise<void> {
40 await this.s3Service.copyObject(objectPath, options)
41 }
42
43 async getObject(objectPath: string): Promise<StorageGetObjectResult> {
44 const response = await this.s3Service.getObject(objectPath)
45 const buffer = response.Body ? Buffer.from(await response.Body.transformToByteArray()) : undefined
46 return { buffer }
47 }
48
49 async getReadSignUrl(objectPath: string, expiresIn?: number): Promise<string> {
50 return this.s3Service.getReadSignUrl(objectPath, expiresIn)
51 }
52 }
53
53 lines TYPESCRIPT