Working upload, list has issues

This commit is contained in:
Manohar Gupta 2026-05-10 15:02:47 +05:30
parent 1dd8563450
commit cf5ea7b820

View file

@ -1,4 +1,4 @@
import { S3Client, PutObjectCommand, ListObjectsV2Command, ListBucketsCommand } from "@aws-sdk/client-s3"; import { S3Client, PutObjectCommand, ListObjectsV2Command } from "@aws-sdk/client-s3";
import { getSignedUrl } from "@aws-sdk/s3-request-presigner"; import { getSignedUrl } from "@aws-sdk/s3-request-presigner";
import { NextRequest, NextResponse } from "next/server"; import { NextRequest, NextResponse } from "next/server";
@ -18,31 +18,15 @@ const client = new S3Client({
}, },
}); });
const clientNoBucket = new S3Client({
region: "auto",
endpoint: `https://${R2.accountId}.r2.cloudflarestorage.com`,
credentials: {
accessKeyId: R2.accessKeyId,
secretAccessKey: R2.secretKey,
},
});
const baseUrl = `https://pub-37a76fd657c94d1dbc521a109c087a11.r2.dev/tia`; const baseUrl = `https://pub-37a76fd657c94d1dbc521a109c087a11.r2.dev/tia`;
export async function GET() { export async function GET() {
try { try {
// First try listing buckets const command = new ListObjectsV2Command({ Bucket: R2.bucket });
const bucketsCmd = new ListBucketsCommand({});
const bucketsRes = await clientNoBucket.send(bucketsCmd);
console.log("Buckets:", bucketsRes.Buckets);
// Then list objects in "tia" bucket
const command = new ListObjectsV2Command({ Bucket: "tia" });
const response = await client.send(command); const response = await client.send(command);
return NextResponse.json({ return NextResponse.json({
success: true, success: true,
buckets: bucketsRes.Buckets,
count: response.Contents?.length || 0, count: response.Contents?.length || 0,
items: response.Contents?.map((o) => ({ items: response.Contents?.map((o) => ({
key: o.Key, key: o.Key,