Minio part 2
This commit is contained in:
@@ -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",
|
||||
|
||||
Reference in New Issue
Block a user