From e866f446c35dcb69162e67d5853f8ca74144965c Mon Sep 17 00:00:00 2001 From: aliamerj Date: Sun, 10 Mar 2024 15:52:23 +0300 Subject: [PATCH] deploy s3 5 --- app/api/project/route.ts | 28 ++++++++++++---------------- utils/aws/s3_bucket.ts | 2 -- 2 files changed, 12 insertions(+), 18 deletions(-) diff --git a/app/api/project/route.ts b/app/api/project/route.ts index edb752c..de60ab0 100644 --- a/app/api/project/route.ts +++ b/app/api/project/route.ts @@ -20,7 +20,6 @@ import { } from "@/db/schemes/projectSchema"; import { and, eq } from "drizzle-orm"; import { users } from "@/db/schemes/userSchema"; -import { PutObjectCommand, S3Client } from "@aws-sdk/client-s3"; export async function POST(request: NextRequest) { const session = await getServerSession(authOptions); @@ -259,23 +258,20 @@ function serializeProjectData(project: FormData) { async function setImageInBucket(userId: string, logoKey: string, logo: File) { const img = Buffer.from(await logo.arrayBuffer()) const resizedImageBuffer = await sharp(img).resize(400,500).toBuffer() -const command = new PutObjectCommand({ - Bucket: process.env.S3_NAME!, - Key:logoKey, - Body:resizedImageBuffer, - ContentType: logo.type, - ContentLength: logo.size, - Metadata: { - userId: userId, - }, - }) - - await s3.send(command) - - const signedUrl = await getSignedUrl(s3,command, + const signedUrl = await getSignedUrl( + s3, + uploadProjectLogo(logoKey, logo.type, logo.size, userId), { expiresIn: 60, }, - ); + ); + + fetch(signedUrl, { + method: "PUT", + body: resizedImageBuffer, + headers: { + "content-type": logo.type, + }, + }); return signedUrl; } diff --git a/utils/aws/s3_bucket.ts b/utils/aws/s3_bucket.ts index 8fb2a5e..8378d36 100644 --- a/utils/aws/s3_bucket.ts +++ b/utils/aws/s3_bucket.ts @@ -17,13 +17,11 @@ export const uploadProjectLogo = ( fileType: string, size: number, userId: string, - image : Buffer, ) => new PutObjectCommand({ Bucket: process.env.S3_NAME!, ContentType: fileType, ContentLength: size, - Body:image, Key: logoName, Metadata: { userId: userId,