Skip to content

Commit

Permalink
deploy s3 5
Browse files Browse the repository at this point in the history
  • Loading branch information
aliamerj committed Mar 10, 2024
1 parent ab9982f commit e866f44
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 18 deletions.
28 changes: 12 additions & 16 deletions app/api/project/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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;
}
2 changes: 0 additions & 2 deletions utils/aws/s3_bucket.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down

0 comments on commit e866f44

Please sign in to comment.