all repos — stealth-developers @ 6ec0bb9f6aa7c031bd18ccafc763e523b20320ac

apps/bot/src/lib/s3.ts (view raw)

 1
 2
 3
 4
 5
 6
 7
 8
 9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
import config from "@stealth-developers/config";
import { S3Client } from "bun";

export const s3 = new S3Client({
	accessKeyId: config.s3.accessKeyId,
	secretAccessKey: config.s3.secretAccessKey,
	bucket: config.s3.bucket,
	endpoint: config.s3.endpoint,
	region: config.s3.region,
});

export function getPublicUrl(key: string) {
	if (config.s3.publicUrl) {
		return `${config.s3.publicUrl}/${key}`;
	}

	return s3.file(key).presign({ expiresIn: 3600 });
}

export type PresignOptions = {
	expiresIn?: number;
	method?: "GET" | "PUT" | "POST" | "DELETE";
	headers?: Record<string, string>;
};

export function getPresignedUrl(key: string, opts: PresignOptions = {}) {
	const { expiresIn = 3600, method, headers } = opts;
	const presignOpts: any = { expiresIn };
	if (method) presignOpts.method = method;
	if (headers) presignOpts.headers = headers;
	return s3.file(key).presign(presignOpts);
}