Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Create R2 upload generator
  • Loading branch information
regalijan committed Mar 22, 2024
1 parent 866876f commit 6d50600
Showing 1 changed file with 40 additions and 0 deletions.
40 changes: 40 additions & 0 deletions functions/api/upload.ts
@@ -0,0 +1,40 @@
import { AwsClient } from "aws4fetch";

const contentTypes: { [k: string]: string } = {
gif: "image/gif",
m4v: "video/x-m4v",
mkv: "video/x-matroska",
mov: "video/mp4",
mp4: "video/mp4",
webm: "video/webm",
wmv: "video/x-ms-wmv",
};

export default async function (
env: Env,
path: string,
size: number,
extension: string,
) {
const aws = new AwsClient({
accessKeyId: env.R2_ACCESS_KEY,
secretAccessKey: env.R2_SECRET_KEY,
});

return (
await aws.sign(
`https://car-crushers.${env.R2_ZONE}.r2.cloudflarestorage.com/${path}`,
{
aws: {
allHeaders: true,
signQuery: true,
},
headers: {
"content-length": size.toString(),
"content-type": contentTypes[extension],
},
method: "PUT",
},
)
).url;
}

0 comments on commit 6d50600

Please sign in to comment.