I created an R2 bucket, and a worker, using the Cloudflare UI.
I then bound the R2 bucket into the variable FNALBUM, again using the UI.
I then went into the quick edit interface and tried to serve from the bucket. E.g.:
async function handleRequest(request) {
const { pathname } = new URL(request.url);
const url = new URL(request.url);
const key = url.pathname.slice(1);
const o = await FNALBUM.get(key);
if (!o) {
return new Response("Object Not Found", { status: 404 });
}
return new Response(`${JSON.stringify(o)} size=${o.size} ${o.body}`);
}
I see two problems:
Contrary to documentation and example code, trying to serve a missing object will not actually return a null object, but an object with a value set to null.
$ curl [ā¦]/nonexistant
{"value": null}
When object is present, all thatās returned is metadata about the object, not the actual body:
What am I doing wrong? Iām clearly actually connected to the bucket, because it can tell the difference between object missing, and provide correct metadata when an object is present.
The body object is a ReadableStream so youāll either pass that into a new Response to stream it or use one of the methods like text(), json(), blob(), etc.
Like @albert mentioned, thereās the āinternalā bindings which are effectively deprecated which will be used if the compatibility date is before 2022-04-18 iirc
I donāt think itās possible to define a compatibility date when using Quick Edit in the Dashboard. When no compatibility date is defined a distant past date will be assumed instead. It is generally recommended you use Wrangler to manage your Workers
In the general case youāre right. Makes for better version control and such. But now for a quick test I need to install wrangler, deal with credentials, find my account ID, etcā¦
And ah yes, this is why I try to avoid using the wrangler CLI. It clears R2 Bucket Bindings on publish, likely because itās an old version. So I try to upgrade it, but it canāt upgrade because I need to upgrade Node. Which is package managed on Ubuntu, so now in order to set the compat date I need to upgrade my OS to a new major version.
Sigh.
Thanks for your help. Wrangler is really putting me off of using Cloudflare products, though.
Iāve been in unupgradable wrangler/npm/node dependency hill (wow, Cloudflare doesnāt let me use well known term dependency h.ll) since 1.19.0. But after upgrading ubuntu (which Iād been avoiding since 21.10 was released with a known ZFS-corrupting bug that I had to first confirm was fixed) and then fighting npm, I got it working.
The secret was to use existing npm to install a newer npm globally, then use that npm from /usr/local/bin to install wrangler.
The only dependency system thatās not been a regression is the Go suite. Even the libc4/libc5/glibc migrations were less painful than npm.