Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 2 additions & 9 deletions src/app/api/s3list/route.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import { logger } from '@/libs/Logger';
import { NextResponse } from 'next/server';
import { S3Client, ListObjectsV2Command, GetObjectCommand } from '@aws-sdk/client-s3';
import { getSignedUrl } from '@aws-sdk/s3-request-presigner';
import { S3Client, ListObjectsV2Command } from '@aws-sdk/client-s3';

const s3Client = new S3Client({ region: 'us-east-1' , credentials:{
accessKeyId: process.env.AWS_ACCESS_KEY_ID_S3 || '',
Expand Down Expand Up @@ -43,21 +42,15 @@ export async function GET(request: Request) {

const files = await Promise.all(
sortedContents.map(async (item) => {
const getObjectParams = {
Bucket: bucketName,
Key: item.Key,
};
const url = await getSignedUrl(s3Client, new GetObjectCommand(getObjectParams), { expiresIn: 3600 });
return {
key: item.Key,
url,
lastModified: item.LastModified,
};
})
);

return NextResponse.json({ files });
} catch (error:any) {
} catch (error: any) {
logger.error(`Error listing S3 files for session ${sessionId}:`, error);
return NextResponse.json({ error: 'Error listing S3 files' }, { status: 500 });
}
Expand Down
58 changes: 58 additions & 0 deletions src/app/api/s3view/route.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
import { logger } from '@/libs/Logger';
import { S3Client, GetObjectCommand } from '@aws-sdk/client-s3';
import { NextResponse } from 'next/server';

const s3Client = new S3Client({
region: 'us-east-1',
credentials: {
accessKeyId: process.env.AWS_ACCESS_KEY_ID_S3 || '',
secretAccessKey: process.env.AWS_SECRET_ACCESS_KEY_S3 || ''
}
});

export async function GET(request: Request) {
const { searchParams } = new URL(request.url);
const key = searchParams.get('key');
const sessionId = request.headers.get('X-Session-Id');

if (!key || !sessionId) {
return NextResponse.json({ error: 'Missing key or session ID' }, { status: 400 });
}

try {
const command = new GetObjectCommand({
Bucket: process.env.S3_BUCKET_NAME || '',
Key: key
});

const response = await s3Client.send(command);

if (!response.Body) {
throw new Error('No body in response');
}

// Convert the readable stream to text
const streamReader = response.Body.transformToWebStream();
const decoder = new TextDecoderStream();
const textStream = streamReader.pipeThrough(decoder);
const reader = textStream.getReader();

let content = '';
while (true) {
const { done, value } = await reader.read();
if (done) break;
content += value;
}

return new NextResponse(content, {
headers: {
'Content-Type': response.ContentType || 'application/json',
'Content-Disposition': `inline; filename="${key.split('/').pop()}"`,
},
});

} catch (error: any) {
logger.error(`Error downloading file ${key}:`, error);
return NextResponse.json({ error: 'Error downloading file' }, { status: 500 });
}
}
16 changes: 12 additions & 4 deletions src/app/s3/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -33,11 +33,19 @@ function App() {
fetchFiles();
}, []);

const handleFileClick = async (url: string) => {
const handleFileClick = async (file: any) => {
try {
setLoading(true);
console.log('Fetching JSON:', url);
const response = await fetch(url);
const response = await fetch(`/api/s3view?key=${encodeURIComponent(file.key)}`, {
headers: {
'X-Session-Id': sessionId || ''
}
});

if (!response.ok) {
throw new Error('Failed to fetch file');
}

const ndJsonData = await response.text();
const jsonData = ndJsonData
.split('\n')
Expand Down Expand Up @@ -78,7 +86,7 @@ function App() {
>
<td
className="px-6 py-4 text-sm font-medium text-gray-900"
onClick={() => handleFileClick(file.url)}
onClick={() => handleFileClick(file)}
>
{file.key}
</td>
Expand Down