R2

How can I get the content after saving something using
r2.put(key, value)
r2.put(key, value)
27 Replies
Theli
Theli•12mo ago
by calling get(key)?
kugyan
kugyan•12mo ago
It somehow doesn't show the actual content 🤔 😭 Code:
JSON.stringify(await env.database.get('potato.json'))
JSON.stringify(await env.database.get('potato.json'))
Output:
{"range":{"offset":0,"length":22},"customMetadata":{},"httpMetadata":{"contentType":"application/json"},"uploaded":"2023-11-12T02:34:20.228Z","checksums":{"md5":"800254d17dcf87cf4dc653ad877d5074"},"httpEtag":""800254d17dcf87cf4dc653ad877d5074"","etag":"800254d17dcf87cf4dc653ad877d5074","size":22,"version":"7e743e9eb9347f0fc9699bd1acd4f7bb","key":"potato.json"}
{"range":{"offset":0,"length":22},"customMetadata":{},"httpMetadata":{"contentType":"application/json"},"uploaded":"2023-11-12T02:34:20.228Z","checksums":{"md5":"800254d17dcf87cf4dc653ad877d5074"},"httpEtag":""800254d17dcf87cf4dc653ad877d5074"","etag":"800254d17dcf87cf4dc653ad877d5074","size":22,"version":"7e743e9eb9347f0fc9699bd1acd4f7bb","key":"potato.json"}
Theli
Theli•12mo ago
nope
kugyan
kugyan•12mo ago
? I just tested
Theli
Theli•12mo ago
get(key, options) Retrieves the R2ObjectBody for the given key containing object metadata and the object body as a , if the key exists, and null if the key does not exist. In the event that a precondition specified in options fails, get() returns an with body undefined.
kugyan
kugyan•12mo ago
-
Theli
Theli•12mo ago
body would be stream, so you won't see it after serialization
kugyan
kugyan•12mo ago
what the body is just this:
{"UUID":null,cache:[]}
{"UUID":null,cache:[]}
theres nothing related to that in the output
Theli
Theli•12mo ago
just read the stream, what can I say It doesn't get converted by JSON.stringify
kugyan
kugyan•12mo ago
what stream
Theli
Theli•12mo ago
call json() on the object you are getting and await it
kugyan
kugyan•12mo ago
let data = await env.database.get('potato.json');

console.log(await data.json());
let data = await env.database.get('potato.json');

console.log(await data.json());
Output:
Expected double-quoted property name in JSON at position 13 (line 1 column 14)
at async Workers.login (worker.js:40:15)
at async jsonError (.internal-92433972-b…c-facade-1.js:12:12)
Expected double-quoted property name in JSON at position 13 (line 1 column 14)
at async Workers.login (worker.js:40:15)
at async jsonError (.internal-92433972-b…c-facade-1.js:12:12)
Theli
Theli•12mo ago
oh, sorry, I'm stupid, I thought it's json file call text() instead Did it work?
kugyan
kugyan•12mo ago
Yes, thanks, json was also invalid, that was probably why the error above lol.
Theli
Theli•12mo ago
yep, no quotes around cache, i missed it
kugyan
kugyan•12mo ago
Thanks!
Theli
Theli•12mo ago
I literally quoted docs to you, you know:)
kugyan
kugyan•12mo ago
Use R2 from Workers · Cloudflare R2 docs
C3 (create-cloudflare-cli) is a command-line tool designed to help you set up and deploy Workers & Pages applications to Cloudflare as fast as …
kugyan
kugyan•12mo ago
There wasn't any .json() part :(
Theli
Theli•12mo ago
Workers API reference · Cloudflare R2 docs
The in-Worker R2 API is accessed by binding an R2 bucket to a Worker. The Worker you write can expose external access to buckets via a route or …
Theli
Theli•12mo ago
that's the actual part
kugyan
kugyan•12mo ago
The page bellow 😭
kugyan
kugyan•12mo ago
No description
kugyan
kugyan•12mo ago
I'm dumb, sorry lmfao
Theli
Theli•12mo ago
and all readableStreams probably have this method, I would know if I used JS:)
kugyan
kugyan•12mo ago
Thanks again
Theli
Theli•12mo ago
cheers
Want results from more Discord servers?
Add your server