t3-code-android-nightly/.repos/alchemy-effect/packages/alchemy/test/Neon/fixtures/StorageNative.ts
Julius Marminge 6f9cea00ae
chore(refs): sync Effect and Alchemy references to 4.0.1 and beta.80 (#16170)
Co-authored-by: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
2026-10-05 13:22:30 -07:00

45 lines
1.4 KiB
TypeScript

import { AwsClient } from "aws4fetch";
export const signStorageRead = (config: {
endpoint: string;
region: string;
accessKeyId: string;
secretAccessKey: string;
}) =>
new AwsClient({
accessKeyId: config.accessKeyId,
secretAccessKey: config.secretAccessKey,
region: config.region,
service: "s3",
}).sign(config.endpoint);
export default {
async fetch(request: Request) {
if (
request.headers.get("authorization") !== `Bearer ${process.env.APP_TOKEN}`
)
return new Response("Unauthorized", { status: 401 });
const client = new AwsClient({
region: process.env.AWS_REGION!,
service: "s3",
accessKeyId: process.env.AWS_ACCESS_KEY_ID!,
secretAccessKey: process.env.AWS_SECRET_ACCESS_KEY!,
});
const url = `${process.env.AWS_ENDPOINT_URL_S3!.replace(/\/$/, "")}/${encodeURIComponent(process.env.BUCKET_NAME!)}/native.txt`;
if (new URL(request.url).pathname === "/presign") {
const target = new URL(url);
target.searchParams.set("X-Amz-Expires", "60");
const signed = await client.sign(target, {
method: "PUT",
aws: { signQuery: true },
});
return Response.json({ url: signed.url });
}
if (request.method === "PUT")
return client.fetch(url, {
method: "PUT",
body: await request.arrayBuffer(),
});
return client.fetch(url);
},
};