R2 binding questions
Hello, first off just want to say I'm very new to CF and web development in genrl.
What I'm doing:
I'm working on a project and trying to understand the R2 function binding on CF pages, so I made a small hello world page project to try and load a image from R2 on page load.
So I can have a bucket of a lot of images and just have the key for the image on the html and load it in when the page is loaded on a users browser. side note* is this a good setup for speed? can images get cached this way?
Problem/question:
I'm running in to problems with loading the image from R2
I'm using some boiler plate code from cloudflare developer documentation site on pages R2
bindings. my code and error messages pics are at the bottom.
I tried to just remove the "context.env." and just use "MEDIA_DB.get()". but got error saying: "Uncaught (in promise) ReferenceError: MEDIA_DB is not defined"
I have made a binding to the bucket in the page project to MEDIA_DB. *(I understand the env variable can somehow be used for specifying different context of deployment like sandbox or production etc. but I don't know how to use the property or if it can be removed...(any good place to read up on this, not theory of it but application of the code of env property?)) lmk if you need any more info to help me out 🙂
I have made a binding to the bucket in the page project to MEDIA_DB. *(I understand the env variable can somehow be used for specifying different context of deployment like sandbox or production etc. but I don't know how to use the property or if it can be removed...(any good place to read up on this, not theory of it but application of the code of env property?)) lmk if you need any more info to help me out 🙂
3 Replies
My code:
??
That looks like you're confusing server-side and local code
and that editor looks like the Worker Editor
If you're just making a basic static site, just upload the images along with your html. It'll be faster then R2, versioned, etc.
If you want to use Functions, there's a getting started guide here: https://developers.cloudflare.com/pages/functions/get-started/
Cloudflare Docs
Functions - Get started · Cloudflare Pages docs
This guide will instruct you on creating and deploying a Pages Function.