Although my code works fine, the console throws the following warning:
API resolved without sending a response for /api/image-upload
The endpoint itself uploads an image to Digital Ocean's object storage service "Spaces", and sends back some metadata (like the location):
import formidable from "formidable-serverless";
import aws from "aws-sdk";
import fs from "fs";
export const config = {
api: {
bodyParser: false,
},
};
export default async (req, res) => {
const s3 = new aws.S3({
endpoint: "...",
accessKey: "...",
secretKey: "...",
});
const form = new formidable.IncomingForm();
form.parse(req, async (err, fields, files) => {
if (err) return res.status(500);
const path = files["file[]"].path;
const file = fs.readFileSync(path);
const name = files["file[]"].name;
s3.upload({
Bucket: "my-bucket",
ACL: "public-read",
Key: `${...}/${name}`,
Body: file,
}).send((err, data) => {
if (err) return res.status(500);
fs.unlinkSync(path);
res.json({
file: {
url: data.Location,
},
});
});
});
};