main: added presigned url + favicon
Signed-off-by: nfel <nfilsaraee@gmail.com>
This commit is contained in:
@@ -1,4 +1,4 @@
|
||||
import { S3Client, PutObjectCommand, DeleteObjectCommand } from '@aws-sdk/client-s3';
|
||||
import { S3Client, PutObjectCommand, DeleteObjectCommand, GetObjectCommand } from '@aws-sdk/client-s3';
|
||||
import { getSignedUrl } from '@aws-sdk/s3-request-presigner';
|
||||
|
||||
// Initialize S3 client
|
||||
@@ -17,13 +17,14 @@ export interface UploadFileParams {
|
||||
file: File;
|
||||
key: string;
|
||||
contentType?: string;
|
||||
makePublic?: boolean; // Set to false for private files
|
||||
}
|
||||
|
||||
/**
|
||||
* Upload a file to S3
|
||||
*/
|
||||
export async function uploadFileToS3(params: UploadFileParams): Promise<string> {
|
||||
const { file, key, contentType } = params;
|
||||
const { file, key, contentType, makePublic = true } = params;
|
||||
|
||||
// Convert File to Buffer
|
||||
const arrayBuffer = await file.arrayBuffer();
|
||||
@@ -33,14 +34,18 @@ export async function uploadFileToS3(params: UploadFileParams): Promise<string>
|
||||
Bucket: BUCKET_NAME,
|
||||
Key: key,
|
||||
Body: buffer,
|
||||
ACL: "public-read",
|
||||
...(makePublic && { ACL: "public-read" }), // Only set ACL if makePublic is true
|
||||
ContentType: contentType || file.type,
|
||||
});
|
||||
|
||||
await s3Client.send(command);
|
||||
|
||||
// Return the public URL
|
||||
return `https://${BUCKET_NAME}.s3.ir-thr-at1.arvanstorage.ir/${key}`;
|
||||
// Return the public URL if public, otherwise return the key
|
||||
if (makePublic) {
|
||||
return `https://${BUCKET_NAME}.s3.ir-thr-at1.arvanstorage.ir/${key}`;
|
||||
} else {
|
||||
return key; // Return just the key for private files
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -69,6 +74,41 @@ export async function getPresignedUploadUrl(key: string, contentType: string): P
|
||||
return url;
|
||||
}
|
||||
|
||||
/**
|
||||
* Generate a presigned URL for downloading/viewing a file
|
||||
* @param key - The S3 object key
|
||||
* @param expiresIn - Expiration time in seconds (default: 1 hour)
|
||||
* @returns Pre-signed URL
|
||||
*/
|
||||
export async function getPresignedUrl(key: string, expiresIn: number = 3600): Promise<string> {
|
||||
const command = new GetObjectCommand({
|
||||
Bucket: BUCKET_NAME,
|
||||
Key: key,
|
||||
});
|
||||
|
||||
const url = await getSignedUrl(s3Client, command, { expiresIn });
|
||||
return url;
|
||||
}
|
||||
|
||||
/**
|
||||
* Generate multiple presigned URLs for a list of keys
|
||||
* @param keys - Array of S3 object keys
|
||||
* @param expiresIn - Expiration time in seconds (default: 1 hour)
|
||||
* @returns Map of key to pre-signed URL
|
||||
*/
|
||||
export async function getMultiplePresignedUrls(
|
||||
keys: string[],
|
||||
expiresIn: number = 3600
|
||||
): Promise<Record<string, string>> {
|
||||
const urlPromises = keys.map(async (key) => {
|
||||
const url = await getPresignedUrl(key, expiresIn);
|
||||
return [key, url];
|
||||
});
|
||||
|
||||
const entries = await Promise.all(urlPromises);
|
||||
return Object.fromEntries(entries);
|
||||
}
|
||||
|
||||
/**
|
||||
* Generate a unique key for file uploads
|
||||
*/
|
||||
|
||||
Reference in New Issue
Block a user