@@ -10,6 +10,9 @@ import {
10
10
import { env } from "$env/dynamic/private" ;
11
11
console . log ( env . AWS_ACCESS_KEY_ID as string ) ;
12
12
13
+ /**
14
+ * Configuration object for S3 client.
15
+ */
13
16
const s3Config : S3ClientConfig = {
14
17
region : ( env . AWS_REGION as string ) || "us-east-1" ,
15
18
credentials : {
@@ -22,6 +25,12 @@ const s3Config: S3ClientConfig = {
22
25
23
26
export const s3Client = new S3Client ( s3Config ) ;
24
27
28
+ /**
29
+ * Ensures that a bucket exists in Amazon S3. If the bucket does not exist, it creates the bucket and sets a bucket policy to allow public read access.
30
+ * If the bucket already exists, it logs a message indicating that the bucket already exists.
31
+ * @param bucketName - The name of the bucket to ensure exists.
32
+ * @throws Throws an error if there is an issue with creating the bucket or setting the bucket policy.
33
+ */
25
34
export const ensureBucketExists = async ( bucketName : string ) : Promise < void > => {
26
35
const headBucketCommand = new HeadBucketCommand ( { Bucket : bucketName } ) ;
27
36
@@ -71,6 +80,14 @@ export const ensureBucketExists = async (bucketName: string): Promise<void> => {
71
80
}
72
81
} ;
73
82
83
+ /**
84
+ * Uploads an object to an S3 bucket.
85
+ * @param bucketName - The name of the S3 bucket.
86
+ * @param fileName - The name of the file to be uploaded.
87
+ * @param fileBuffer - The file content as a Buffer.
88
+ * @returns A Promise that resolves to the URL of the uploaded object.
89
+ * @throws If there is an error during the upload process.
90
+ */
74
91
export const uploadObject = async (
75
92
bucketName : string ,
76
93
fileName : string ,
@@ -97,6 +114,12 @@ export const uploadObject = async (
97
114
}
98
115
} ;
99
116
117
+ /**
118
+ * Deletes an object from an S3 bucket.
119
+ * @param bucketName - The name of the S3 bucket.
120
+ * @param fileName - The name of the file to delete.
121
+ * @throws Throws an error if there is an issue deleting the object.
122
+ */
100
123
export const deleteObject = async ( bucketName : string , fileName : string ) => {
101
124
const deleteObjectCommand = new DeleteObjectCommand ( {
102
125
Bucket : bucketName ,
@@ -114,6 +137,12 @@ export const deleteObject = async (bucketName: string, fileName: string) => {
114
137
}
115
138
} ;
116
139
140
+ /**
141
+ * Returns the URL of an object in the specified bucket.
142
+ * @param bucketName - The name of the bucket.
143
+ * @param fileName - The name of the file.
144
+ * @returns The URL of the object.
145
+ */
117
146
export const getObjectUrl = ( bucketName : string , fileName : string ) : string => {
118
147
let objectUrl : string ;
119
148
let endpoint : string = "" ;
@@ -123,6 +152,9 @@ export const getObjectUrl = (bucketName: string, fileName: string): string => {
123
152
endpoint = env . AWS_S3_ENDPOINT as string ;
124
153
}
125
154
155
+ // This code is not as clean as it could be, but it works for whats needed. Help is welcome to clean it up!
156
+ // Currently supports Amazon S3, Google Cloud Storage, DigitalOcean Spaces, and Supabase Storage as well as self-hosted MinIO.
157
+
126
158
if ( endpoint . includes ( "amazonaws.com" ) ) {
127
159
// Amazon S3
128
160
objectUrl = `https://${ bucketName } .s3.${ env . AWS_REGION } .amazonaws.com/${ fileName } ` ;
0 commit comments