| 1 | import { |
| 2 | S3Client, |
| 3 | PutObjectCommand, |
| 4 | GetObjectCommand, |
| 5 | DeleteObjectCommand, |
| 6 | HeadBucketCommand, |
| 7 | HeadObjectCommand, |
| 8 | CreateBucketCommand, |
| 9 | } from "@aws-sdk/client-s3"; |
| 10 | import { env } |
| 11 | |
| 12 | const s3 = new |
| 13 | endpoint: env.MINIO_ENDPOINT |
| 14 | region: "us-east-1", |
| 15 | credentials: { |
| 16 | accessKeyId: env.MINIO_ACCESS_KEY |
| 17 | secretAccessKey: env.MINIO_SECRET_KEY |
| 18 | }, |
| 19 | forcePathStyle: true, |
| 20 | }); |
| 21 | |
| 22 | async function tryEnsureBucket() |
| 23 | try { |
| 24 | await s3.send |
| 25 | } catch (err: |
| 26 | const status = |
| 27 | if (status === |
| 28 | await s3.send |
| 29 | } else { |
| 30 | throw err; |
| 31 | } |
| 32 | } |
| 33 | } |
| 34 | |
| 35 | let bucketReady: Promise |
| 36 | |
| 37 | // If the previous attempt failed, reset so the next call gets a fresh try |
| 38 | async function requireBucket() |
| 39 | try { |
| 40 | await bucketReady; |
| 41 | } catch { |
| 42 | bucketReady = tryEnsureBucket() |
| 43 | await bucketReady; |
| 44 | } |
| 45 | } |
| 46 | |
| 47 | export async function uploadObject |
| 48 | await requireBucket(); |
| 49 | await s3.send |
| 50 | } |
| 51 | |
| 52 | export async function getObject |
| 53 | await requireBucket(); |
| 54 | const res = |
| 55 | if (!res.Body |
| 56 | return { stream: |
| 57 | } |
| 58 | |
| 59 | export async function headObject |
| 60 | await requireBucket(); |
| 61 | const res = |
| 62 | return { etag: |
| 63 | } |
| 64 | |
| 65 | export async function deleteObject |
| 66 | await requireBucket(); |
| 67 | try { |
| 68 | await s3.send |
| 69 | } catch { |
| 70 | // swallow NoSuchKey |
| 71 | } |
| 72 | } |