Skip to main content

List S3 Files

  • Node endpoint to list all s3 files

Code​

const AWS = require("aws-sdk");

AWS.config.update({
accessKeyId: process.env["ACCESS_KEY_ID"],
secretAccessKey: process.env["SECRET_ACCESS_KEY"],
region: "ap-southeast-2",
});

const s3 = new AWS.S3();

export default async function handler(req, res) {
try {
const { key } = req.query;
if (!key) {
return res.status(400).json({ error: "File key is required" });
}

const params = {
Bucket: "coloringpagespro",
Key: key,
};

const data = await s3.getObject(params).promise();
if (!data.Body) {
return res.status(404).json({ error: "File not found" });
}

res.setHeader("Content-Type", "image/jpeg");
res.end(data.Body, "binary");
} catch (error) {
console.error(error);
res.status(500).json({ error: "Internal server error" });
}
}