Skip to content

Instantly share code, notes, and snippets.

@appletreeat56
Last active November 10, 2022 16:57
Show Gist options
  • Save appletreeat56/3b520fdeec8c9dff30c3a4fbe533673b to your computer and use it in GitHub Desktop.
Save appletreeat56/3b520fdeec8c9dff30c3a4fbe533673b to your computer and use it in GitHub Desktop.
import { NextApiRequest, NextApiResponse } from "next";
import S3 from "aws-sdk/clients/s3";
const s3 = new S3({
region: process.env.AWSDEFAULTREGION,
accessKeyId: process.env.AWSACCESSKEYID,
secretAccessKey: process.env.AWSSECRETACCESSKEY,
signatureVersion: "v4",
});
export default async (req: NextApiRequest, res: NextApiResponse) => {
if (req.method !== "POST") {
return res.status(405).json({ message: "Method not allowed" });
}
try {
let { name, type } = req.body;
const fileParams = {
Bucket: process.env.BUCKET_NAME,
Key: name,
Expires: 600,
ContentType: type,
ACL: "public-read"
};
const url = await s3.getSignedUrlPromise("putObject", fileParams);
res.status(200).json({ url });
} catch (err) {
console.log(err);
res.status(400).json({ message: err });
}
};
export const config = {
api: {
bodyParser: {
sizeLimit: "8mb", // Set desired value here
},
},
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment