Skip to content

🎥 Make videos programmatically with React

feat: adds presignUrl and getS3Object helper

imlegend193394cf928d58
diff --git a/packages/docs/docs/lambda/gets3object.md b/packages/docs/docs/lambda/gets3object.md
new file mode 100644
index 0000000000..c05a214dd7
--- /dev/null
+++ b/packages/docs/docs/lambda/gets3object.md
@@ -0,0 +1,43 @@
+---
+id: gets3object
+title: getS3Object()
+slug: /lambda/gets3object
+---
+
+Retrieves an object stored in Remotion's S3 bucket.
+
+## Example
+
+```ts twoslash
+import fs from "fs";
+import { getS3Object } from "@remotion/lambda";
+
+const data = await getS3Object({
+ region: 'us-east-1',
+ bucketName: 'remotionlambda-fz1zu45ztk',
+ objectKey: 'assets/sample.jpeg',
+});
+
+const file = fs.createWriteStream('sample.jpeg');
+data.pipe(file);
+```
+
+## Arguments
+
+An object with the following properties:
+
+### `region`
+
+The AWS region in which the bucket resides.
+
+### `bucketName`
+
+The bucket where the asset exists. The bucket must have been created by Remotion Lambda.
+
+### `objectKey`
+
+They key that uniquely identifies the object stored in the bucket.
+
+## See also
+
+- [`presignUrl()`](/docs/lambda/presignurl)
diff --git a/packages/docs/docs/lambda/presignurl.md b/packages/docs/docs/lambda/presignurl.md
new file mode 100644
index 0000000000..455a481b4e
--- /dev/null
+++ b/packages/docs/docs/lambda/presignurl.md
@@ -0,0 +1,50 @@
+---
+id: presignurl
+title: presignUrl()
+slug: /lambda/presignurl
+---
+
+Returns a presigned url for public access of an object stored in Remotion's S3 bucket.
+
+## Example
+
+```ts twoslash
+import { presignUrl } from "@remotion/lambda";
+
+const url = await presignUrl({
+ region: "us-east-1",
+ bucketName: "remotionlambda-c7fsl3d",
+ objectKey: "assets/sample.png",
+ checkIfObjectExists: true,
+});
+
+console.log(url); // `null` if object doesn't exist
+```
+
+## Arguments
+
+An object with the following properties:
+
+### `region`
+
+The AWS region in which the bucket resides.
+
+### `bucketName`
+
+The bucket where the asset exists. The bucket must have been created by Remotion Lambda.
+
+### `objectKey`
+
+They key that uniquely identifies the object stored in the bucket.
+
+### `checkIfObjectExists`
+
+Whether the function should check if the object exists in the bucket before generating the presigned url. Default false.
+
+### `expiresIn`
+
+The number of seconds before the presigned URL expires. Default 120.
+
+## See also
+
+- [`getS3Object()`](/docs/lambda/gets3object)
diff --git a/packages/lambda/remotionlambda.zip b/packages/lambda/remotionlambda.zip
index 496722ebca..b8c2857396 100644
Binary files a/packages/lambda/remotionlambda.zip and b/packages/lambda/remotionlambda.zip differ
diff --git a/packages/lambda/src/api/get-s3-object.ts b/packages/lambda/src/api/get-s3-object.ts
new file mode 100644
index 0000000000..1f06eae1e8
--- /dev/null
+++ b/packages/lambda/src/api/get-s3-object.ts
@@ -0,0 +1,42 @@
+import {GetObjectCommand} from '@aws-sdk/client-s3';
+import {Readable} from 'stream';
+import {AwsRegion} from '../pricing/aws-regions';
+import {getS3Client} from '../shared/aws-clients';
+import {REMOTION_BUCKET_PREFIX} from '../shared/constants';
+
+export type GetS3ObjectInput = {
+ region: AwsRegion;
+ bucketName: string;
+ objectKey: string;
+};
+
+/**
+ * @description Retrieves an object stored in Remotion's S3 bucket.
+ * @link https://v3.remotion.dev/docs/lambda/gets3object
+ * @param {AwsRegion} params.region The region in which the S3 bucket resides in.
+ * @param {string} params.bucketName The name of the bucket to fetch the object from.
+ * @param {string} params.objectKey Key of the S3 object to get.
+ * @returns {Promise<Readable>} A `stream.Readable` object.
+ */
+export const getS3Object = async ({
+ region,
+ bucketName,
+ objectKey,
+}: GetS3ObjectInput): Promise<Readable> => {
+ if (!bucketName.startsWith(REMOTION_BUCKET_PREFIX)) {
+ throw new Error(
+ `The bucketName parameter must start with ${REMOTION_BUCKET_PREFIX}.`
+ );
+ }
+
+ const s3Client = getS3Client(region);
+
+ const data = await s3Client.send(
+ new GetObjectCommand({
+ Bucket: bucketName,
+ Key: objectKey,
+ })
+ );
+
+ return data.Body as Readable;
+};
diff --git a/packages/lambda/src/api/presign-url.ts b/packages/lambda/src/api/presign-url.ts
new file mode 100644
index 0000000000..77b163fb9d
--- /dev/null
+++ b/packages/lambda/src/api/presign-url.ts
@@ -0,0 +1,64 @@
+import {GetObjectCommand, HeadObjectCommand} from '@aws-sdk/client-s3';
+import {getSignedUrl} from '@aws-sdk/s3-request-presigner';
+import {AwsRegion} from '../pricing/aws-regions';
+import {getS3Client} from '../shared/aws-clients';
+import {REMOTION_BUCKET_PREFIX} from '../shared/constants';
+
+export type PresignURLInput = {
+ region: AwsRegion;
+ bucketName: string;
+ objectKey: string;
+ checkIfObjectExists?: boolean;
+ expiresIn?: number;
+};
+
+/**
+ * @description Returns a public url of an object stored in Remotion's S3 bucket.
+ * @link https://v3.remotion.dev/docs/lambda/presignurl
+ * @param {AwsRegion} params.region The region in which the S3 bucket resides in.
+ * @param {string} params.bucketName The name of the bucket to fetch the object from.
+ * @param {string} params.objectKey Key of the S3 object to get.
+ * @param {string} params.expiresIn The number of seconds before the presigned URL expires. Default 120.
+ * @param {boolean} params.checkIfObjectExists Whether the function should check if the object exists in the bucket before generating the presigned url.
+ * @returns {Promise<string | null>} The public url of an object or `null` if `checkIfObjectExists=true` & object does not exist.
+ */
+export const presignUrl = async ({
+ region,
+ bucketName,
+ objectKey,
+ checkIfObjectExists = false,
+ expiresIn = 120,
+}: PresignURLInput): Promise<string | null> => {
+ if (!bucketName.startsWith(REMOTION_BUCKET_PREFIX)) {
+ throw new Error(
+ `The bucketName parameter must start with ${REMOTION_BUCKET_PREFIX}.`
+ );
+ }
+
+ const s3Client = getS3Client(region);
+
+ const params = {
+ Bucket: bucketName,
+ Key: objectKey,
+ };
+
+ if (checkIfObjectExists) {
+ try {
+ await s3Client.send(new HeadObjectCommand(params));
+ } catch (err) {
+ if ((err as {name: string}).name === 'NotFound') {
+ return null;
+ }
+
+ throw err;
+ }
+ }
+
+ const objCommand = new GetObjectCommand(params);
+
+ const publicUrl = await getSignedUrl(s3Client, objCommand, {
+ expiresIn,
+ });
+
+ return publicUrl;
+};