summaryrefslogtreecommitdiff
path: root/app/src/lib/server/s3.ts
blob: 5d0dec92f0dda2f463cfca3d9c3e5a241f807d0c (plain)
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
import { S3Client } from "@aws-sdk/client-s3";
import type { Handle } from "@sveltejs/kit";

// We would obviously read from .env in prod, but it's an annoying indirection for this demo.
function getS3Client(): S3Client {
	const client = new S3Client({
		endpoint: "http://localhost:9000",
		region: "us-east-1", // Required, but ignored for local usage.
		credentials: {
			accessKeyId: "minioadmin",
			secretAccessKey: "minioadmin",
		},
		forcePathStyle: true, // Required for local service since we obv. can't use subdomains.
	});

	return client;
}

export const s3Handle = (async ({ event, resolve }) => {
	const s3Client = getS3Client();
	event.locals.s3Client = s3Client;

	try {
		return await resolve(event);
	} finally {
		s3Client.destroy();
	}
}) satisfies Handle;