R2

How can I get the content after saving something using
r2.put(key, value)
r2.put(key, value)
27 Replies
Theli
Theli•13mo ago
by calling get(key)?
kugyan
kugyanOP•13mo 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•13mo ago
nope
kugyan
kugyanOP•13mo ago
? I just tested
Theli
Theli•13mo 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
kugyanOP•13mo ago
-
Theli
Theli•13mo ago
body would be stream, so you won't see it after serialization
kugyan
kugyanOP•13mo ago
what the body is just this:
{"UUID":null,cache:[]}
{"UUID":null,cache:[]}
theres nothing related to that in the output
Theli
Theli•13mo ago
just read the stream, what can I say It doesn't get converted by JSON.stringify
kugyan
kugyanOP•13mo ago
what stream
Theli
Theli•13mo ago
call json() on the object you are getting and await it
kugyan
kugyanOP•13mo 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•13mo ago
oh, sorry, I'm stupid, I thought it's json file call text() instead Did it work?
kugyan
kugyanOP•13mo ago
Yes, thanks, json was also invalid, that was probably why the error above lol.
Theli
Theli•13mo ago
yep, no quotes around cache, i missed it
kugyan
kugyanOP•13mo ago
Thanks!
Theli
Theli•13mo ago
I literally quoted docs to you, you know:)
kugyan
kugyanOP•13mo 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
kugyanOP•13mo ago
There wasn't any .json() part :(
Theli
Theli•13mo 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•13mo ago
that's the actual part
kugyan
kugyanOP•13mo ago
The page bellow 😭
kugyan
kugyanOP•13mo ago
No description
kugyan
kugyanOP•13mo ago
I'm dumb, sorry lmfao
Theli
Theli•13mo ago
and all readableStreams probably have this method, I would know if I used JS:)
kugyan
kugyanOP•13mo ago
Thanks again
Theli
Theli•13mo ago
cheers
Want results from more Discord servers?
Add your server