Kyzer
Kyzer
CDCloudflare Developers
Created by Kyzer on 5/3/2024 in #pages-help
Issue with Next.js (next-on-pages), API route and R2
Hey, any help is appreciated, thanks. I have a route that returns the list of objects in R2, it works the first time, but if you try visit again or refresh too quickly I get an error "The script will never generate a response.". lib/r2.ts
import { S3Client } from "@aws-sdk/client-s3";

export const r2 = new S3Client({
region: "auto",
endpoint: `https://${process.env.R2_ACCOUNT_ID}.r2.cloudflarestorage.com`,
credentials: {
accessKeyId: process.env.R2_ACCESS_KEY_ID || "",
secretAccessKey: process.env.R2_SECRET_ACCESS_KEY || "",
},
});
import { S3Client } from "@aws-sdk/client-s3";

export const r2 = new S3Client({
region: "auto",
endpoint: `https://${process.env.R2_ACCOUNT_ID}.r2.cloudflarestorage.com`,
credentials: {
accessKeyId: process.env.R2_ACCESS_KEY_ID || "",
secretAccessKey: process.env.R2_SECRET_ACCESS_KEY || "",
},
});
app/api/files/route.ts
import { r2 } from "@/lib/r2";
import { ListObjectsV2Command } from "@aws-sdk/client-s3";

export const runtime = "edge";

export async function GET() {
const command = new ListObjectsV2Command({
Bucket: process.env.R2_BUCKET_NAME,
});
const response = await r2.send(command);

return Response.json(response);
}
import { r2 } from "@/lib/r2";
import { ListObjectsV2Command } from "@aws-sdk/client-s3";

export const runtime = "edge";

export async function GET() {
const command = new ListObjectsV2Command({
Bucket: process.env.R2_BUCKET_NAME,
});
const response = await r2.send(command);

return Response.json(response);
}
1 replies