From 1fa83110a3ebeb0b8bcdb10ee0201f852af64edc Mon Sep 17 00:00:00 2001 From: alma Date: Sun, 4 May 2025 15:52:32 +0200 Subject: [PATCH] pages s3 --- app/api/debug/s3/route.ts | 78 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 78 insertions(+) create mode 100644 app/api/debug/s3/route.ts diff --git a/app/api/debug/s3/route.ts b/app/api/debug/s3/route.ts new file mode 100644 index 00000000..6c1a08ce --- /dev/null +++ b/app/api/debug/s3/route.ts @@ -0,0 +1,78 @@ +import { NextResponse } from 'next/server'; +import { getServerSession } from 'next-auth'; +import { authOptions } from '@/app/api/auth/[...nextauth]/route'; +import { ListBucketsCommand, ListObjectsV2Command } from '@aws-sdk/client-s3'; +import { s3Client, S3_CONFIG } from '@/lib/s3'; + +export async function GET(request: Request) { + try { + // Get session + const session = await getServerSession(authOptions); + if (!session?.user?.id) { + return NextResponse.json({ error: 'Unauthorized' }, { status: 401 }); + } + + const userId = session.user.id; + const { searchParams } = new URL(request.url); + const folder = searchParams.get('folder') || 'notes'; + + // Debug information about the configuration + const config = { + endpoint: S3_CONFIG.endpoint, + region: S3_CONFIG.region, + bucket: S3_CONFIG.bucket, + hasAccessKey: !!S3_CONFIG.accessKey, + hasSecretKey: !!S3_CONFIG.secretKey + }; + + // Try to list buckets to verify credentials + let bucketList = []; + try { + const bucketsResponse = await s3Client.send(new ListBucketsCommand({})); + bucketList = bucketsResponse.Buckets?.map(b => b.Name) || []; + } catch (error) { + return NextResponse.json({ + error: 'Failed to list buckets', + message: error instanceof Error ? error.message : String(error), + config + }, { status: 500 }); + } + + // Now try to list objects in the folder + const prefix = `user-${userId}/${folder}/`; + try { + const command = new ListObjectsV2Command({ + Bucket: S3_CONFIG.bucket, + Prefix: prefix, + Delimiter: '/' + }); + + const response = await s3Client.send(command); + + return NextResponse.json({ + config, + buckets: bucketList, + prefix, + objects: response.Contents?.map(item => ({ + key: item.Key, + size: item.Size, + lastModified: item.LastModified + })) || [], + prefixes: response.CommonPrefixes?.map(p => p.Prefix) || [] + }); + } catch (error) { + return NextResponse.json({ + error: 'Failed to list objects', + message: error instanceof Error ? error.message : String(error), + config, + buckets: bucketList, + prefix + }, { status: 500 }); + } + } catch (error) { + return NextResponse.json({ + error: 'Internal server error', + message: error instanceof Error ? error.message : String(error) + }, { status: 500 }); + } +} \ No newline at end of file