Public
Script
Readme

r2 wrapper

Save blobs without leaving Val Town! Only for Val Town Pro users to limit abuse.

We get 10gb free, so as long as nobody abuses this, I won't put any strict limits on it. If you want to use more than 10gb, let me know, and we can bill you for it.

All data in this bucket is public if you know it's name!

This val, @stevekrouse.r2, is the "SDK" for @stevekrouse.r2Proxy.

Setup

  1. This val requires you to setup public key auth, which is really just three clicks here: https://blog.val.town/public-key-auth-val-town-users-can-be-your-users
  2. [Optional] You can setup a helper function so you don't have to pass your keys every time:
let blob = (key, value) => @stevekrouse.r2(key, value, @me.exportedKeys)

Warning: Do not make your blob function public or unlisted. That would allow anyone to use it.

Usage

await @me.blob("demo", { hello: "world" }); return @me.blob("demo"); // {hello: "world"}

Example usage: https://www.val.town/v/stevekrouse.r2Demo

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
import { runValAPIAuth } from "https://esm.town/v/stevekrouse/runValAPIAuth";
import { fetchJSON } from "https://esm.town/v/stevekrouse/fetchJSON";
import { whoami } from "https://esm.town/v/stevekrouse/whoami";
export async function r2(key, value?, keys?) {
// GET
if (value === undefined) {
let handle = whoami().at(-1).slice(1).split(".")[0]
.replace("@", "");
return fetchJSON(
`https://pub-6feb98c078874338b9a3bce6ab56e815.r2.dev/${handle}/${key}`,
{ agent: null },
);
}
// SET
return runValAPIAuth({
val: "@stevekrouse.r2Proxy",
args: [key, JSON.stringify(value)],
keys,
});
}
Val Town is a social website to write and deploy JavaScript.
Build APIs and schedule functions from your browser.
Comments
Nobody has commented on this val yet: be the first!
October 23, 2023