Can I write a data URI to an R2 bucket?
Is it possible to write to an R2 bucket with a base64 data URI i.e.
? That fails (it creates a text file with literally that content), but perhaps there's a way to encode the file contents first such that R2 accepts it?
4 Replies
Convert the base64 data to a Uint8Array
You could probably use
(await fetch('data:image/png;base64,i5Gkw...')).body
, if it's any different performance-wise to that snippet is something you'd have to test.Ah that's cool, never occurred to me to fetch a data URI. I'll try both and come back if any issues. Thanks!
@kian's method was the only one that worked here. For reference in case anyone else finds this useful.
Method 1: This produces
InvalidCharacterError: atob() called with invalid...
:
Method 2: (suggested by @kian) - this successfully saves the file to R2.
Method 3: (variant on method 1) - this SO answer suggested a different way to derive an array buffer that avoids atob()
. This does save the file to R2, but with mangled source, such that it can't be opened.
On this generally, presumably passing a file as base64 to my back-end and saving it to R2 in this way isn't a terrible idea for any reason I'm missing?