mirror of
https://github.com/Hestia-Homes/assessment-model.git
synced 2026-06-30 12:55:02 +00:00
add uri
This commit is contained in:
parent
08705f708a
commit
da8500c01b
2 changed files with 54 additions and 10 deletions
45
src/app/api/upload/retrofit-energy-assessments-dev/route.ts
Normal file
45
src/app/api/upload/retrofit-energy-assessments-dev/route.ts
Normal file
|
|
@ -0,0 +1,45 @@
|
|||
import { NextRequest, NextResponse } from "next/server";
|
||||
import { z } from "zod";
|
||||
import { createS3Client, presignGetUrl } from "@/app/utils/s3";
|
||||
|
||||
const Schema = z.object({
|
||||
path: z.string(),
|
||||
expiresInSeconds: z.number().int().positive().default(300),
|
||||
responseContentType: z.string().optional(),
|
||||
responseContentDisposition: z.string().optional(), // e.g., 'attachment; filename="file.csv"'
|
||||
});
|
||||
|
||||
export async function POST(request: NextRequest) {
|
||||
try {
|
||||
const body = await request.json();
|
||||
const { path, expiresInSeconds, responseContentType, responseContentDisposition } =
|
||||
Schema.parse(body);
|
||||
|
||||
// Retrofit s3 bucket connection
|
||||
let bucket = process.env.RETROFIT_ENERGY_ASSESSMENTS_BUCKET
|
||||
if (!bucket) {
|
||||
return NextResponse.json({ msg: "RETROFIT_ENERGY_ASSESSMENTS_BUCKET is not set" }, { status: 400 });
|
||||
}
|
||||
|
||||
const s3 = createS3Client({
|
||||
region: process.env.PRESIGN_AWS_REGION,
|
||||
accessKeyId: process.env.RETROFIT_ENERGY_ASSESSMENTS_AWS_ACCESS_KEY,
|
||||
secretAccessKey: process.env.RETROFIT_ENERGY_ASSESSMENTS_AWS_ACCESS_KEY,
|
||||
});
|
||||
|
||||
const url = await presignGetUrl(s3, {
|
||||
bucket,
|
||||
key: path,
|
||||
expiresInSeconds,
|
||||
responseContentType,
|
||||
responseContentDisposition,
|
||||
});
|
||||
|
||||
return NextResponse.json({ url });
|
||||
} catch (err) {
|
||||
console.error(err);
|
||||
return err instanceof z.ZodError
|
||||
? NextResponse.json({ msg: "Invalid input", issues: err.issues }, { status: 400 })
|
||||
: NextResponse.json({ msg: "Internal server error" }, { status: 500 });
|
||||
}
|
||||
}
|
||||
|
|
@ -25,19 +25,20 @@ const titles: Record<ReportType, string> = {
|
|||
};
|
||||
|
||||
async function generatePresignedUrls({
|
||||
key,
|
||||
bucket,
|
||||
path,
|
||||
contentType,
|
||||
expiresInSeconds,
|
||||
}: {
|
||||
key: string;
|
||||
bucket: string;
|
||||
path: string;
|
||||
contentType: string;
|
||||
expiresInSeconds: number;
|
||||
}) {
|
||||
const body = JSON.stringify({ key, bucket, contentType, expiresInSeconds });
|
||||
console.log("bucket is ", bucket);
|
||||
const presignedResponse = await fetch("/api/upload/s3_bucket_presigned_url", {
|
||||
const body = JSON.stringify({
|
||||
path: path,
|
||||
responseContentType: contentType,
|
||||
expiresInSeconds: expiresInSeconds
|
||||
});
|
||||
const presignedResponse = await fetch("/api/upload/retrofit-energy-assessments-dev", {
|
||||
method: "POST",
|
||||
body,
|
||||
});
|
||||
|
|
@ -62,9 +63,7 @@ export const UploadModal = ({
|
|||
console.log("Get Presigned url in a specific bucket location")
|
||||
|
||||
const { url } = await generatePresignedUrls({
|
||||
key: "foo/test/trololol", // path in bucket
|
||||
bucket: process.env.RETROFIT_ENERGY_ASSESSMENTS_BUCKET || "back up", //s3 bucket location
|
||||
// bucket: "retrofit-energy-assessments-dev", //s3 bucket location
|
||||
path: "foo/test/trololol", // path in bucket
|
||||
contentType: "application/pdf",
|
||||
expiresInSeconds: 5 * 60,
|
||||
});
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue