generated from corrad-software/corrad-af-2024

Uploading now works with AWS. Documentation can be found in dms-api.md at the root folder.
65 lines
2.0 KiB
JavaScript
65 lines
2.0 KiB
JavaScript
import { S3Client, PutObjectCommand } from "@aws-sdk/client-s3";
|
|
import { getSignedUrl } from "@aws-sdk/s3-request-presigner";
|
|
import { readMultipartFormData } from 'h3';
|
|
|
|
export default defineEventHandler(async (event) => {
|
|
// Create S3 client with config
|
|
const client = new S3Client({
|
|
region: process.env.NUXT_AWS_REGION,
|
|
credentials: {
|
|
accessKeyId: process.env.NUXT_AWS_ACCESS_KEY_ID,
|
|
secretAccessKey: process.env.NUXT_AWS_SECRET_ACCESS_KEY
|
|
}
|
|
});
|
|
|
|
try {
|
|
const parts = await readMultipartFormData(event, { maxSize: 1024 * 1024 }); // 1MB limit since we're only handling metadata
|
|
|
|
if (!parts) {
|
|
return {
|
|
status: 400,
|
|
message: "Bad request. No parts found."
|
|
}
|
|
}
|
|
|
|
// Extract form data
|
|
const fileNamePart = parts.find(p => p.name === "fileName");
|
|
const fileTypePart = parts.find(p => p.name === "fileType");
|
|
|
|
if (!fileNamePart || !fileTypePart) {
|
|
return {
|
|
status: 400,
|
|
message: "Missing required fields { fileName, fileType }"
|
|
}
|
|
}
|
|
|
|
const fileName = fileNamePart.data.toString();
|
|
const fileType = fileTypePart.data.toString();
|
|
|
|
// Generate a unique key using timestamp and filename
|
|
const uploadKey = `uploads/${Date.now()}-${fileName}`;
|
|
|
|
const command = new PutObjectCommand({
|
|
Bucket: process.env.NUXT_AWS_BUCKET,
|
|
Key: uploadKey,
|
|
ContentType: fileType,
|
|
});
|
|
|
|
// Generate signed URL
|
|
const signedUrl = await getSignedUrl(client, command, { expiresIn: 60 });
|
|
|
|
return {
|
|
status: 200,
|
|
message: `Signed URL generated for file: ${fileName}`,
|
|
signedUrl
|
|
};
|
|
|
|
} catch (error) {
|
|
console.error('Error processing request:', error);
|
|
return {
|
|
status: 500,
|
|
message: "Failed to generate signed URL",
|
|
error: error.message
|
|
}
|
|
}
|
|
}) |