-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
212 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
import type { NextApiRequest, NextApiResponse } from "next" | ||
|
||
import AWS from "aws-sdk" | ||
|
||
export default async function handler(req: NextApiRequest, res: NextApiResponse) { | ||
// data is in base64 | ||
const { good, data: base64Data } = JSON.parse(req.body) | ||
|
||
// convert base64 to buffer | ||
const imgData = Buffer.from(base64Data.replace(/^data:image\/\w+;base64,/, ""), "base64") | ||
|
||
// upload to S3 | ||
const s3 = new AWS.S3({ | ||
accessKeyId: process.env.AWS_ACCESS_KEY_ID, | ||
secretAccessKey: process.env.AWS_SECRET_ACCESS_KEY, | ||
}) | ||
|
||
const filename = new Date().toISOString() | ||
|
||
const type = base64Data.split(";")[0].split("/")[1] | ||
|
||
const params: AWS.S3.PutObjectRequest = { | ||
Bucket: process.env?.AWS_BUCKET_NAME ?? "", | ||
Key: `${good ? "good" : "bad"}-${filename}.${type}`, | ||
Body: imgData, | ||
ACL: "public-read", | ||
ContentEncoding: "base64", // required | ||
ContentType: `image/${type}`, // required. Notice the back ticks | ||
} | ||
|
||
s3.upload(params, (err: any, data: any) => { | ||
if (err) { | ||
console.error(err) | ||
res.status(500).json({ error: err }) | ||
} else { | ||
console.log(data.Location) | ||
res.status(200).json({ name: data.Location }) | ||
} | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters