Skip to content

🎥 Make videos programmatically with React

Endpoint -> method

Patric Salvisberg4237468f4f5c
diff --git a/packages/docs/docs/presign-url.md b/packages/docs/docs/presign-url.md
index a202a1080c..8a3bd2b292 100644
--- a/packages/docs/docs/presign-url.md
+++ b/packages/docs/docs/presign-url.md
@@ -1,9 +1,11 @@
---
id: presign-url
-title: Upload with presign URL
+title: Upload with a presigned URL
---
-In an app where users can upload videos and edit them, we want to make sure these videos get uploaded directly on our destination cloud storage (other word for destination cloud storage?). With the following presign URL workflow, a unique temporary upload url is provided with the possibility to ensure that individually set constraints (such as maximal file size, filetype,...) aren't violated. (reformulate this sentence)
+In an app where users can upload videos and edit them, we want to make sure these videos get uploaded directly to our destination cloud storage. By generating a presigned URL on the server, you allow a user to directly upload a file into your cloud storage without having to through your server.
+
+You can set constraints such as maximal file size and file type, apply rate limiting and require authentication, and predefine the key.
## What is a presign URL?
@@ -11,16 +13,16 @@ A presign URL is a URL to which you can send a file. This URL predefines the fil
## Why using presign URL?
-The traditional way of handling a file upload would be to let the client upload the file on the server, which then handles the upload to the cloud storage. While this approach works, its not ideal due to several reasons.
+The traditional way of handling a file upload would be to let the client upload the file on a server, which then handles the upload to the cloud storage. While this approach works, it's not ideal due to several reasons.
-- If many clients happen to upload big files on the server, the server can get slow or even break down under the load. With the presign workflow, the server only needs to create presign URLs, which creats way less server load than handling file transfers.
+- If many clients happen to upload big files on the same server, this server can get slow or even break down under the load. With the presign workflow, the server only needs to create presign URLs, which reduces server load than handling file transfers.
-- Since a lot of servers today are based on ephemeral environments, files should not be stored on them. There is no guarantee the files will still exist after a server restart.
+- Since a lot of hosting solutions today are ephemeral or serverless, files should not be stored on them. There is no guarantee the files will still exist after a server restart and you might run out of disk space.
## Types and helper functions
In order to be able to check the file type and file size in the backend, both contentType and contentLength are sent in the PresignRequestBody.
-With `makeS3Url()` a helperfunction is provided to set up the read URL (??)
+With `makeS3Url()` a helperfunction is provided to set up the read URL
```tsx twoslash
// ---cut---
@@ -82,38 +84,19 @@ export type PresignRequestBody = {
contentType: string;
};
// ---cut---
-
import { getSignedUrl } from "@aws-sdk/s3-request-presigner";
import { AwsRegion, getAwsClient, getOrCreateBucket } from "@remotion/lambda";
-import { NextApiRequest, NextApiResponse } from "next";
import { v4 } from "uuid";
-export default async function handler(
- req: NextApiRequest,
- res: NextApiResponse
-) {
- const { method } = req;
-
- if (method !== "POST") {
- return res.status(405).json({ message: "Method not allowed" });
- }
-
- const body = req.body as PresignRequestBody;
-
- //content type precondition
- if (body.contentType !== "application/octet-stream") {
- return res.status(400).json({
- message: `Content type ${body.contentType} is not supported`,
- });
- }
-
- const contentLength = Number(body.contentLength ?? 0);
-
- //example constraint we can set while creating the presign url
+export const generatePresignedUrl = async (
+ contentType: string,
+ contentLength: number,
+ expiresIn: number
+): Promise<string> => {
if (contentLength > 1024 * 1024 * 200) {
- return res.status(400).json({
- message: `File may not be over 200MB. Yours is ${contentLength} bytes.`,
- });
+ console.error(
+ `File may not be over 200MB. Yours is ${contentLength} bytes.`
+ );
}
const { client, sdk } = getAwsClient({
@@ -135,8 +118,8 @@ export default async function handler(
Bucket: bucketName,
Key: id,
ACL: "public-read",
- ContentLength: body.contentLength,
- ContentType: body.contentType,
+ ContentLength: contentLength,
+ ContentType: contentType,
});
//url which will be used to read the uploaded file
@@ -150,25 +133,22 @@ export default async function handler(
const presignedUrl = await getSignedUrl(client, command, {
expiresIn: 60 * 5,
});
-
- const response: PresignResponse = {
- uploadUrl: presignedUrl,
- readUrl,
- id,
- bucketName,
- region,
- };
-
- return res.status(200).json(response);
-}
+ return presignedUrl;
+};
```
After ensuring the provided file does not violate the constraint in place, the AWS client and SDK get derived with [getAwsClient()](/docs/lambda/getawsclient), which are needed to create and get the presign URL.
To create a unique ID which later will be contained in the presign URL, [UUID](https://en.wikipedia.org/wiki/Universally_unique_identifier) V4 is used.
-After deriving the AWS Region and the S3 bucket name, command object is created, which is needed to get the signed URL.
+We can derive the URL at which the file will be available using makeS3Url().
+
+The presignedUrl then gets created with the [getSignedUrl()](https://docs.aws.amazon.com/AWSJavaScriptSDK/latest/AWS/S3.html#getSignedUrl-property).
+
+## Useing the presigned URL
+
+////////////////////////////////////////////////////////////////////
-By using the helper function `makeS3Url()`, the read url will be created.
+TO BE CONTINUED NEXT WEDNESDAY
-The presignedUrl then gets created with the [getSingedUrl()](https://docs.aws.amazon.com/AWSJavaScriptSDK/latest/AWS/S3.html#getSignedUrl-property).
+////////////////////////////////////////////////////////////////////