Skip to content

Commit

Permalink
fix: perf s3 client
Browse files Browse the repository at this point in the history
  • Loading branch information
Blinko committed Nov 18, 2024
1 parent 46690b3 commit cf3b0ec
Showing 1 changed file with 10 additions and 8 deletions.
18 changes: 10 additions & 8 deletions app/api/s3file/[filename]/route.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,12 @@
import { NextRequest, NextResponse } from "next/server";
import path from "path";
import { readFile } from "fs/promises";
import { stat } from "fs/promises";
import mime from "mime-types";
import { UPLOAD_FILE_PATH } from "@/lib/constant";
import { getGlobalConfig } from "@/server/routers/config";
import { GetObjectCommand, S3Client } from "@aws-sdk/client-s3";

export const GET = async (req: Request, { params }: any) => {
let s3ClientInstance: S3Client | null = null;
async function getS3Client() {
const config = await getGlobalConfig();
const s3Client = new S3Client({
if (s3ClientInstance) return { s3ClientInstance, config };
s3ClientInstance = new S3Client({
endpoint: config.s3Endpoint,
region: config.s3Region,
credentials: {
Expand All @@ -18,13 +15,18 @@ export const GET = async (req: Request, { params }: any) => {
},
forcePathStyle: true,
});
return { s3ClientInstance, config };
}


export const GET = async (req: Request, { params }: any) => {
const { s3ClientInstance, config } = await getS3Client();
try {
const command = new GetObjectCommand({
Bucket: config.s3Bucket,
Key: params.filename,
});
const response = await s3Client.send(command);
const response = await s3ClientInstance.send(command);
const headers = new Headers();
headers.set('Content-Type', response.ContentType || 'application/octet-stream');
return new NextResponse(response.Body?.transformToWebStream(), {
Expand Down

0 comments on commit cf3b0ec

Please sign in to comment.