The best way to control Claude Code, Codex and more from any computer or phone

main
ts 43 lines 1.36 KB
Raw
1 import { AwsClient } from "aws4fetch";
2
3 export const signStorageRead = (config: {
4 endpoint: string;
5 region: string;
6 accessKeyId: string;
7 secretAccessKey: string;
8 }) =>
9 new AwsClient({
10 accessKeyId: config.accessKeyId,
11 secretAccessKey: config.secretAccessKey,
12 region: config.region,
13 service: "s3",
14 }).sign(config.endpoint);
15
16 export default {
17 async fetch(request: Request) {
18 if (request.headers.get("authorization") !== `Bearer ${process.env.APP_TOKEN}`)
19 return new Response("Unauthorized", { status: 401 });
20 const client = new AwsClient({
21 region: process.env.AWS_REGION!,
22 service: "s3",
23 accessKeyId: process.env.AWS_ACCESS_KEY_ID!,
24 secretAccessKey: process.env.AWS_SECRET_ACCESS_KEY!,
25 });
26 const url = `${process.env.AWS_ENDPOINT_URL_S3!.replace(/\/$/, "")}/${encodeURIComponent(process.env.BUCKET_NAME!)}/native.txt`;
27 if (new URL(request.url).pathname === "/presign") {
28 const target = new URL(url);
29 target.searchParams.set("X-Amz-Expires", "60");
30 const signed = await client.sign(target, {
31 method: "PUT",
32 aws: { signQuery: true },
33 });
34 return Response.json({ url: signed.url });
35 }
36 if (request.method === "PUT")
37 return client.fetch(url, {
38 method: "PUT",
39 body: await request.arrayBuffer(),
40 });
41 return client.fetch(url);
42 },
43 };