Minio part 2

This commit is contained in:
Sean Morley
2024-06-09 18:10:09 +00:00
parent 08c6708543
commit 1c7939f2b8
4 changed files with 28 additions and 17 deletions

View File

@@ -3,6 +3,7 @@
import minioClient from "$lib/server/minio.js";
import type { RequestEvent } from "@sveltejs/kit";
import { generateId } from "lucia";
import { MINIO_URL } from "$env/static/private";
export async function POST(event: RequestEvent): Promise<Response> {
try {
@@ -10,8 +11,6 @@ export async function POST(event: RequestEvent): Promise<Response> {
const fileExtension = contentType.split("/").pop();
const fileName = `${generateId(25)}.${fileExtension}`;
console.log(fileName);
const fileBuffer = await event.request.arrayBuffer();
const metaData = {
"Content-Type": contentType,
@@ -21,6 +20,28 @@ export async function POST(event: RequestEvent): Promise<Response> {
if (!found) {
await minioClient.makeBucket("profile-pics");
// Set a bucket policy to allow public read access
const bucketPolicy = {
Version: "2012-10-17",
Statement: [
{
Effect: "Allow",
Principal: "*",
Action: ["s3:GetBucketLocation", "s3:ListBucket"],
Resource: `arn:aws:s3:::profile-pics`,
},
{
Effect: "Allow",
Principal: "*",
Action: "s3:GetObject",
Resource: `arn:aws:s3:::profile-pics/*`,
},
],
};
await minioClient.setBucketPolicy(
"profile-pics",
JSON.stringify(bucketPolicy)
);
}
await minioClient.putObject(
@@ -34,9 +55,9 @@ export async function POST(event: RequestEvent): Promise<Response> {
fileName
);
console.log(fileUrl);
const publicUrl = `${MINIO_URL}/profile-pics/${fileName}`;
return new Response(JSON.stringify({ fileUrl }), {
return new Response(JSON.stringify({ publicUrl }), {
status: 200,
headers: {
"Content-Type": "application/json",