workers dont see my R2
hey, so workers dont see my R2 for some reason.
here's my simple setup:
and here's my wrangler.toml:
jast 100% exists, and keep it mind I'm using workers through the admin account, so there should be no issues, but it just doesnt see the binding for some reaso
here's my simple setup:
export default {
async fetch(request, env, ctx) {
try {
// Check if the R2 bucket binding exists
if (!env.MY_BUCKET) {
throw new Error("R2 bucket binding 'MY_BUCKET' is not defined");
}
const prefix = 'your-prefix/' // Replace with your desired prefix
const options = {
prefix: prefix,
limit: 1000, // Adjust as needed
}
const objects = await env.MY_BUCKET.list(options);
const totalFiles = objects.objects.length;
const first10Files = objects.objects.slice(0, 10).map(object => ({
name: object.key,
lastModified: new Date(object.uploaded).toISOString(),
}));
const latestAdditionTime = first10Files.length > 0
? first10Files.reduce((latest, file) =>
file.lastModified > latest ? file.lastModified : latest,
first10Files[0].lastModified)
: null;
return new Response(JSON.stringify({
totalFiles,
latestAdditionTime,
first10Files,
}, null, 2), {
headers: { 'Content-Type': 'application/json' },
});
} catch (error) {
console.error("Error in worker:", error);
return new Response(JSON.stringify({ error: error.message }), {
status: 500,
headers: { 'Content-Type': 'application/json' },
});
}
}
}export default {
async fetch(request, env, ctx) {
try {
// Check if the R2 bucket binding exists
if (!env.MY_BUCKET) {
throw new Error("R2 bucket binding 'MY_BUCKET' is not defined");
}
const prefix = 'your-prefix/' // Replace with your desired prefix
const options = {
prefix: prefix,
limit: 1000, // Adjust as needed
}
const objects = await env.MY_BUCKET.list(options);
const totalFiles = objects.objects.length;
const first10Files = objects.objects.slice(0, 10).map(object => ({
name: object.key,
lastModified: new Date(object.uploaded).toISOString(),
}));
const latestAdditionTime = first10Files.length > 0
? first10Files.reduce((latest, file) =>
file.lastModified > latest ? file.lastModified : latest,
first10Files[0].lastModified)
: null;
return new Response(JSON.stringify({
totalFiles,
latestAdditionTime,
first10Files,
}, null, 2), {
headers: { 'Content-Type': 'application/json' },
});
} catch (error) {
console.error("Error in worker:", error);
return new Response(JSON.stringify({ error: error.message }), {
status: 500,
headers: { 'Content-Type': 'application/json' },
});
}
}
}and here's my wrangler.toml:
name = "r2-test"
main = "worker.js"
compatibility_date = "2023-08-23"
[[r2_buckets]]
binding = 'MY_BUCKET' # <~ valid JavaScript variable name
bucket_name = 'jast'name = "r2-test"
main = "worker.js"
compatibility_date = "2023-08-23"
[[r2_buckets]]
binding = 'MY_BUCKET' # <~ valid JavaScript variable name
bucket_name = 'jast'jast 100% exists, and keep it mind I'm using workers through the admin account, so there should be no issues, but it just doesnt see the binding for some reaso