psych
CDCloudflare Developers
•Created by psych on 12/9/2024 in #general-help
Beginner: Cannot connect to R2 via Worker
Omg that's such a silly mistake. Sorry. And thank you for looking into it!
5 replies
CDCloudflare Developers
•Created by psych on 12/9/2024 in #general-help
Beginner: Cannot connect to R2 via Worker
@Walshy Thank you for responding!
index.js
/**
* Welcome to Cloudflare Workers! This is your first worker.
*
* - Run
npm run dev in your terminal to start a development server
* - Open a browser tab at http://localhost:8787/ to see your worker in action
* - Run
npm run deploy to publish your worker
*
* Learn more at https://developers.cloudflare.com/workers/
*/
export default {
async fetch(request, env, ctx) {
// return new Response('Hello World!');
const url = new URL(request.url);
const key = url.pathname.slice(1);
switch (request.method) {
case "PUT":
await env.MY_BUCKET.put(key, request.body);
return new Response(
Put ${key} successfully!);
default:
return new Response(
${request.method} is not allowed., {
status: 405,
headers: {
Allow: "PUT",
},
});
}
},
};
wrangler.toml (only change is the name of the bucket)
# Bind an R2 Bucket. Use R2 to store arbitrarily large blobs of data, such as files.
# Docs: https://developers.cloudflare.com/workers/wrangler/configuration/#r2-buckets
# [[r2_buckets]]
# binding = "MY_BUCKET"
# bucket_name = "faltu-bucket"
5 replies