Initial commit
This commit is contained in:
@@ -0,0 +1,82 @@
|
||||
import {
|
||||
S3Client,
|
||||
PutObjectCommand,
|
||||
DeleteObjectCommand,
|
||||
} from "@aws-sdk/client-s3";
|
||||
import { getSignedUrl } from "@aws-sdk/s3-request-presigner";
|
||||
|
||||
const RUSTFS_ENDPOINT = process.env.RUSTFS_ENDPOINT || "http://localhost:9000";
|
||||
const RUSTFS_ACCESS_KEY = process.env.RUSTFS_ACCESS_KEY || "minioadmin";
|
||||
const RUSTFS_SECRET_KEY = process.env.RUSTFS_SECRET_KEY || "minioadmin";
|
||||
const RUSTFS_BUCKET = process.env.RUSTFS_BUCKET || "surf";
|
||||
const RUSTFS_PUBLIC = (process.env.RUSTFS_PUBLIC || "true") === "true";
|
||||
|
||||
let client: S3Client | null = null;
|
||||
|
||||
function getRustFsClient(): S3Client {
|
||||
if (!client) {
|
||||
client = new S3Client({
|
||||
endpoint: RUSTFS_ENDPOINT,
|
||||
region: "us-east-1",
|
||||
credentials: {
|
||||
accessKeyId: RUSTFS_ACCESS_KEY,
|
||||
secretAccessKey: RUSTFS_SECRET_KEY,
|
||||
},
|
||||
forcePathStyle: true,
|
||||
});
|
||||
}
|
||||
return client;
|
||||
}
|
||||
|
||||
function getPublicUrl(key: string): string {
|
||||
if (RUSTFS_PUBLIC) {
|
||||
return `${RUSTFS_ENDPOINT}/${RUSTFS_BUCKET}/${key}`;
|
||||
}
|
||||
return "";
|
||||
}
|
||||
|
||||
export function getObjectUrl(key: string): string {
|
||||
const publicUrl = getPublicUrl(key);
|
||||
if (publicUrl) return publicUrl;
|
||||
return `/api/videos/file/${encodeURIComponent(key)}`;
|
||||
}
|
||||
|
||||
export async function uploadObject(
|
||||
key: string,
|
||||
body: Buffer,
|
||||
contentType: string,
|
||||
): Promise<void> {
|
||||
const s3 = getRustFsClient();
|
||||
await s3.send(
|
||||
new PutObjectCommand({
|
||||
Bucket: RUSTFS_BUCKET,
|
||||
Key: key,
|
||||
Body: body,
|
||||
ContentType: contentType,
|
||||
}),
|
||||
);
|
||||
}
|
||||
|
||||
export async function deleteObject(key: string): Promise<void> {
|
||||
const s3 = getRustFsClient();
|
||||
await s3.send(
|
||||
new DeleteObjectCommand({
|
||||
Bucket: RUSTFS_BUCKET,
|
||||
Key: key,
|
||||
}),
|
||||
);
|
||||
}
|
||||
|
||||
export async function getPresignedUploadUrl(
|
||||
key: string,
|
||||
contentType: string,
|
||||
expiresInSeconds = 3600,
|
||||
): Promise<string> {
|
||||
const s3 = getRustFsClient();
|
||||
const command = new PutObjectCommand({
|
||||
Bucket: RUSTFS_BUCKET,
|
||||
Key: key,
|
||||
ContentType: contentType,
|
||||
});
|
||||
return getSignedUrl(s3, command, { expiresIn: expiresInSeconds });
|
||||
}
|
||||
Reference in New Issue
Block a user