|
| 1 | +import { |
| 2 | + S3Client, |
| 3 | + GetObjectCommand, |
| 4 | + HeadObjectCommand, |
| 5 | + ListObjectsV2Command, |
| 6 | + PutObjectCommand, |
| 7 | + CopyObjectCommand, |
| 8 | +} from '@aws-sdk/client-s3'; |
| 9 | +import { StorageInterface } from './StorageInterface'; |
| 10 | + |
| 11 | +export class S3Storage implements StorageInterface { |
| 12 | + private client: S3Client; |
| 13 | + private bucketName: string; |
| 14 | + |
| 15 | + constructor() { |
| 16 | + if (!process.env.S3_ACCESS_KEY_ID || !process.env.S3_SECRET_ACCESS_KEY) { |
| 17 | + throw new Error('S3 credentials not configured'); |
| 18 | + } |
| 19 | + if (!process.env.S3_BUCKET_NAME) { |
| 20 | + throw new Error('S3 bucket name not configured'); |
| 21 | + } |
| 22 | + this.client = new S3Client({ |
| 23 | + region: process.env.S3_REGION ?? 'auto', |
| 24 | + endpoint: process.env.S3_ENDPOINT, |
| 25 | + credentials: { |
| 26 | + accessKeyId: process.env.S3_ACCESS_KEY_ID, |
| 27 | + secretAccessKey: process.env.S3_SECRET_ACCESS_KEY, |
| 28 | + }, |
| 29 | + }); |
| 30 | + this.bucketName = process.env.S3_BUCKET_NAME; |
| 31 | + } |
| 32 | + |
| 33 | + async copyFile(sourcePath: string, destinationPath: string): Promise<void> { |
| 34 | + const copyCommand = new CopyObjectCommand({ |
| 35 | + Bucket: this.bucketName, |
| 36 | + CopySource: sourcePath, |
| 37 | + Key: destinationPath, |
| 38 | + }); |
| 39 | + await this.client.send(copyCommand); |
| 40 | + } |
| 41 | + |
| 42 | + async downloadFile(path: string): Promise<Buffer> { |
| 43 | + const getCommand = new GetObjectCommand({ |
| 44 | + Bucket: this.bucketName, |
| 45 | + Key: path, |
| 46 | + }); |
| 47 | + const response = await this.client.send(getCommand); |
| 48 | + const body = await response.Body?.transformToByteArray(); |
| 49 | + if (!body) { |
| 50 | + throw new Error('No body found in response'); |
| 51 | + } |
| 52 | + return Buffer.from(body); |
| 53 | + } |
| 54 | + |
| 55 | + async fileExists(path: string): Promise<boolean> { |
| 56 | + try { |
| 57 | + const files = await this.listDirectories(path); |
| 58 | + if (files.length > 0) { |
| 59 | + return true; |
| 60 | + } |
| 61 | + } catch {} |
| 62 | + try { |
| 63 | + const headCommand = new HeadObjectCommand({ |
| 64 | + Bucket: this.bucketName, |
| 65 | + Key: path.split('/').shift(), |
| 66 | + }); |
| 67 | + await this.client.send(headCommand); |
| 68 | + return true; |
| 69 | + } catch { |
| 70 | + return false; |
| 71 | + } |
| 72 | + } |
| 73 | + |
| 74 | + async listFiles(directory: string): Promise< |
| 75 | + { |
| 76 | + name: string; |
| 77 | + updated_at: string; |
| 78 | + created_at: string; |
| 79 | + metadata: { size: number; mimetype: string }; |
| 80 | + }[] |
| 81 | + > { |
| 82 | + const prefix = `${directory}/`; |
| 83 | + const listCommand = new ListObjectsV2Command({ |
| 84 | + Bucket: this.bucketName, |
| 85 | + Prefix: prefix, |
| 86 | + }); |
| 87 | + const response = await this.client.send(listCommand); |
| 88 | + return ( |
| 89 | + response.Contents?.map((file) => ({ |
| 90 | + name: file.Key!.replace(prefix, ''), |
| 91 | + updated_at: file.LastModified?.toISOString() ?? '', |
| 92 | + created_at: file.LastModified?.toISOString() ?? '', |
| 93 | + metadata: { |
| 94 | + size: file.Size ?? 0, |
| 95 | + mimetype: this.getMimeType(file.Key?.split('.').pop() ?? 'unknown'), |
| 96 | + }, |
| 97 | + })) ?? [] |
| 98 | + ); |
| 99 | + } |
| 100 | + |
| 101 | + async listDirectories(directory: string): Promise<string[]> { |
| 102 | + const listCommand = new ListObjectsV2Command({ |
| 103 | + Bucket: this.bucketName, |
| 104 | + Prefix: directory, |
| 105 | + Delimiter: '/', |
| 106 | + }); |
| 107 | + const response = await this.client.send(listCommand); |
| 108 | + return ( |
| 109 | + response.CommonPrefixes?.map((prefix) => |
| 110 | + prefix.Prefix!.replace(directory, '').replace(/\/$/, '') |
| 111 | + ) ?? [] |
| 112 | + ); |
| 113 | + } |
| 114 | + |
| 115 | + async uploadFile(path: string, file: Buffer): Promise<string> { |
| 116 | + const uploadCommand = new PutObjectCommand({ |
| 117 | + Bucket: this.bucketName, |
| 118 | + Key: path, |
| 119 | + Body: file, |
| 120 | + }); |
| 121 | + await this.client.send(uploadCommand); |
| 122 | + return path; |
| 123 | + } |
| 124 | + |
| 125 | + private getMimeType(ext: string): string { |
| 126 | + const mimeTypes: { [key: string]: string } = { |
| 127 | + js: 'application/javascript', |
| 128 | + json: 'application/json', |
| 129 | + png: 'image/png', |
| 130 | + jpg: 'image/jpeg', |
| 131 | + jpeg: 'image/jpeg', |
| 132 | + gif: 'image/gif', |
| 133 | + zip: 'application/zip', |
| 134 | + }; |
| 135 | + return mimeTypes[ext.toLowerCase()] || 'application/octet-stream'; |
| 136 | + } |
| 137 | +} |
0 commit comments