Skip to content

🎥 Make videos programmatically with React

`@remotion/lambda`: Add S3 output provider CLI option (#9007)

Jonny Burger374400ebf3b6
diff --git a/packages/docs/docs/lambda/cli/render.mdx b/packages/docs/docs/lambda/cli/render.mdx
index 1752200d6e..ff0438815d 100644
--- a/packages/docs/docs/lambda/cli/render.mdx
+++ b/packages/docs/docs/lambda/cli/render.mdx
@@ -219,6 +219,39 @@ A number describing how long the render may take to resolve all [`delayRender()`
The file name of the media output as stored in the S3 bucket. By default, it is `out` plus the appropriate file extension, for example: `out.mp4`. Must match `/([0-9a-zA-Z-!_.*'()/]+)/g`.
+### `--s3-output-provider-endpoint`
+
+The endpoint for an [`s3OutputProvider`](/docs/lambda/custom-destination#saving-to-another-cloud). Use it to save the rendered media to an S3-compatible provider such as Cloudflare R2.
+
+When using this option, also pass `--force-bucket-name` and `--out-name`. If your provider requires credentials, set `REMOTION_S3_OUTPUT_PROVIDER_ACCESS_KEY_ID` and `REMOTION_S3_OUTPUT_PROVIDER_SECRET_ACCESS_KEY`.
+
+```bash
+REMOTION_S3_OUTPUT_PROVIDER_ACCESS_KEY_ID=<access-key-id> \
+REMOTION_S3_OUTPUT_PROVIDER_SECRET_ACCESS_KEY=<secret-access-key> \
+npx remotion lambda render testbed my-comp \
+ --force-bucket-name=my-r2-bucket \
+ --out-name=renders/video.mp4 \
+ --privacy=no-acl \
+ --s3-output-provider-endpoint=https://<account-id>.r2.cloudflarestorage.com \
+ --s3-output-provider-region=auto
+```
+
+### `REMOTION_S3_OUTPUT_PROVIDER_ACCESS_KEY_ID`
+
+The access key ID to use with `--s3-output-provider-endpoint`.
+
+### `REMOTION_S3_OUTPUT_PROVIDER_SECRET_ACCESS_KEY`
+
+The secret access key to use with `--s3-output-provider-endpoint`.
+
+### `--s3-output-provider-region`
+
+The region to use with `--s3-output-provider-endpoint`.
+
+### `--s3-output-provider-force-path-style`
+
+Passes `forcePathStyle` to the S3 client created for `--s3-output-provider-endpoint`.
+
### `--overwrite`<AvailableFrom v="3.2.25" />
If a custom out name is specified and a file already exists at this key in the S3 bucket, decide whether that file will be deleted before the render begins. Default `false`.
diff --git a/packages/docs/docs/lambda/cli/still.mdx b/packages/docs/docs/lambda/cli/still.mdx
index 5582890419..18ff097f88 100644
--- a/packages/docs/docs/lambda/cli/still.mdx
+++ b/packages/docs/docs/lambda/cli/still.mdx
@@ -87,6 +87,39 @@ How many times a single chunk is being retried if it fails to render. Default `1
The file name of the media output as stored in the S3 bucket. By default, it is `out` plus the appropriate file extension, for example: `out.png`. Must match `/([0-9a-zA-Z-!_.*'()/]+)/g`.
+### `--s3-output-provider-endpoint`
+
+The endpoint for an [`s3OutputProvider`](/docs/lambda/custom-destination#saving-to-another-cloud). Use it to save the rendered still to an S3-compatible provider such as Cloudflare R2.
+
+When using this option, also pass `--force-bucket-name` and `--out-name`. If your provider requires credentials, set `REMOTION_S3_OUTPUT_PROVIDER_ACCESS_KEY_ID` and `REMOTION_S3_OUTPUT_PROVIDER_SECRET_ACCESS_KEY`.
+
+```bash
+REMOTION_S3_OUTPUT_PROVIDER_ACCESS_KEY_ID=<access-key-id> \
+REMOTION_S3_OUTPUT_PROVIDER_SECRET_ACCESS_KEY=<secret-access-key> \
+npx remotion lambda still testbed my-comp \
+ --force-bucket-name=my-r2-bucket \
+ --out-name=renders/still.png \
+ --privacy=no-acl \
+ --s3-output-provider-endpoint=https://<account-id>.r2.cloudflarestorage.com \
+ --s3-output-provider-region=auto
+```
+
+### `REMOTION_S3_OUTPUT_PROVIDER_ACCESS_KEY_ID`
+
+The access key ID to use with `--s3-output-provider-endpoint`.
+
+### `REMOTION_S3_OUTPUT_PROVIDER_SECRET_ACCESS_KEY`
+
+The secret access key to use with `--s3-output-provider-endpoint`.
+
+### `--s3-output-provider-region`
+
+The region to use with `--s3-output-provider-endpoint`.
+
+### `--s3-output-provider-force-path-style`
+
+Passes `forcePathStyle` to the S3 client created for `--s3-output-provider-endpoint`.
+
### `--image-format`
<Options id="still-image-format" />
diff --git a/packages/lambda/src/cli/args.ts b/packages/lambda/src/cli/args.ts
index 6e5925a156..6c6db77dc1 100644
--- a/packages/lambda/src/cli/args.ts
+++ b/packages/lambda/src/cli/args.ts
@@ -14,6 +14,7 @@ const LambdaBooleanFlags = [
'default-only',
'compatible-only',
'force-path-style',
+ 's3-output-provider-force-path-style',
];
type LambdaCommandLineOptions = {
@@ -36,6 +37,9 @@ type LambdaCommandLineOptions = {
['concurrency']?: number;
['concurrency-per-lambda']?: number;
['out-name']: string | undefined;
+ ['s3-output-provider-endpoint']: string | undefined;
+ ['s3-output-provider-region']: AwsRegion | (string & {}) | undefined;
+ ['s3-output-provider-force-path-style']: boolean;
['custom-role-arn']: string | undefined;
privacy: Privacy;
webhook: string | undefined;
diff --git a/packages/lambda/src/cli/commands/render/render.ts b/packages/lambda/src/cli/commands/render/render.ts
index e53868a18f..82dfc91eb2 100644
--- a/packages/lambda/src/cli/commands/render/render.ts
+++ b/packages/lambda/src/cli/commands/render/render.ts
@@ -23,6 +23,10 @@ import {validateMaxRetries} from '../../../shared/validate-retries';
import {parsedLambdaCli} from '../../args';
import {getAwsRegion} from '../../get-aws-region';
import {findFunctionName} from '../../helpers/find-function-name';
+import {
+ getS3OutputProviderFromCli,
+ makeOutNameWithCustomCredentials,
+} from '../../helpers/get-s3-output-provider-from-cli';
import {getWebhookCustomData} from '../../helpers/get-webhook-custom-data';
import {quit} from '../../helpers/quit';
import {Log} from '../../log';
@@ -309,6 +313,17 @@ export const renderCommand = async ({
}
const outName = parsedLambdaCli['out-name'];
+ const s3OutputProvider = getS3OutputProviderFromCli({
+ endpoint: parsedLambdaCli['s3-output-provider-endpoint'],
+ region: parsedLambdaCli['s3-output-provider-region'],
+ forcePathStyle:
+ parsedLambdaCli['s3-output-provider-force-path-style'] || undefined,
+ });
+ const resolvedOutName = makeOutNameWithCustomCredentials({
+ bucketName: parsedLambdaCli['force-bucket-name'],
+ key: outName,
+ s3OutputProvider,
+ });
const downloadName = args[2] ?? null;
const {value: codec, source: reason} = videoCodecOption.getValue(
@@ -371,7 +386,7 @@ export const renderCommand = async ({
privacy,
logLevel,
frameRange: frameRange ?? null,
- outName: parsedLambdaCli['out-name'] ?? null,
+ outName: resolvedOutName,
timeoutInMilliseconds,
chromiumOptions,
scale,
@@ -517,6 +532,7 @@ export const renderCommand = async ({
region: getAwsRegion(),
logLevel,
skipLambdaInvocation: Boolean(adheresToFunctionNameConvention),
+ s3OutputProvider: s3OutputProvider ?? undefined,
});
progressBar.update(
makeProgressString({
@@ -535,6 +551,7 @@ export const renderCommand = async ({
renderId: res.renderId,
region: getAwsRegion(),
logLevel,
+ s3OutputProvider: s3OutputProvider ?? undefined,
});
progressBar.update(
makeProgressString({
@@ -571,7 +588,7 @@ export const renderCommand = async ({
providerSpecifics: providerSpecifics,
forcePathStyle: parsedLambdaCli['force-path-style'],
signal: new AbortController().signal,
- customCredentials: null,
+ customCredentials: s3OutputProvider,
requestHandler: null,
});
downloadOrNothing = download;
diff --git a/packages/lambda/src/cli/commands/still.ts b/packages/lambda/src/cli/commands/still.ts
index 0ea87a4a2d..39547ae634 100644
--- a/packages/lambda/src/cli/commands/still.ts
+++ b/packages/lambda/src/cli/commands/still.ts
@@ -18,6 +18,10 @@ import {validateMaxRetries} from '../../shared/validate-retries';
import {parsedLambdaCli} from '../args';
import {getAwsRegion} from '../get-aws-region';
import {findFunctionName} from '../helpers/find-function-name';
+import {
+ getS3OutputProviderFromCli,
+ makeOutNameWithCustomCredentials,
+} from '../helpers/get-s3-output-provider-from-cli';
import {quit} from '../helpers/quit';
import {Log} from '../log';
import {makeArtifactProgress} from './render/progress';
@@ -218,6 +222,17 @@ export const stillCommand = async ({
const downloadName = args[2] ?? null;
const outName = parsedLambdaCli['out-name'];
+ const s3OutputProvider = getS3OutputProviderFromCli({
+ endpoint: parsedLambdaCli['s3-output-provider-endpoint'],
+ region: parsedLambdaCli['s3-output-provider-region'],
+ forcePathStyle:
+ parsedLambdaCli['s3-output-provider-force-path-style'] || undefined,
+ });
+ const resolvedOutName = makeOutNameWithCustomCredentials({
+ bucketName: parsedLambdaCli['force-bucket-name'],
+ key: outName,
+ s3OutputProvider,
+ });
const functionName = await findFunctionName({logLevel, providerSpecifics});
@@ -275,7 +290,7 @@ export const stillCommand = async ({
frame: stillFrame,
jpegQuality,
logLevel,
- outName: outName ?? null,
+ outName: resolvedOutName,
chromiumOptions,
timeoutInMilliseconds,
scale,
@@ -364,7 +379,7 @@ export const stillCommand = async ({
providerSpecifics: providerSpecifics,
forcePathStyle: parsedLambdaCli['force-path-style'],
signal: new AbortController().signal,
- customCredentials: null,
+ customCredentials: s3OutputProvider,
onProgress: () => undefined,
requestHandler: null,
});
diff --git a/packages/lambda/src/cli/helpers/get-s3-output-provider-from-cli.ts b/packages/lambda/src/cli/helpers/get-s3-output-provider-from-cli.ts
new file mode 100644
index 0000000000..e225e9acea
--- /dev/null
+++ b/packages/lambda/src/cli/helpers/get-s3-output-provider-from-cli.ts
@@ -0,0 +1,77 @@
+import {LambdaClientInternals} from '@remotion/lambda-client';
+import type {CustomCredentials, OutNameInput} from '@remotion/serverless';
+import type {AwsProvider} from '../../client';
+
+export const S3_OUTPUT_PROVIDER_ACCESS_KEY_ID_ENV_NAME =
+ 'REMOTION_S3_OUTPUT_PROVIDER_ACCESS_KEY_ID';
+export const S3_OUTPUT_PROVIDER_SECRET_ACCESS_KEY_ENV_NAME =
+ 'REMOTION_S3_OUTPUT_PROVIDER_SECRET_ACCESS_KEY';
+
+export const getS3OutputProviderFromCli = ({
+ endpoint,
+ region,
+ forcePathStyle,
+}: {
+ endpoint: string | undefined;
+ region: AwsProvider['region'] | (string & {}) | undefined;
+ forcePathStyle: boolean | undefined;
+}): CustomCredentials<AwsProvider> | null => {
+ const hasAnyS3OutputProviderFlag =
+ endpoint !== undefined ||
+ region !== undefined ||
+ forcePathStyle !== undefined;
+
+ if (!hasAnyS3OutputProviderFlag) {
+ return null;
+ }
+
+ if (!endpoint) {
+ throw new Error(
+ 'Pass --s3-output-provider-endpoint when using S3 output provider flags.',
+ );
+ }
+
+ return {
+ endpoint,
+ accessKeyId:
+ LambdaClientInternals.getEnvVariable(
+ S3_OUTPUT_PROVIDER_ACCESS_KEY_ID_ENV_NAME,
+ ) ?? null,
+ secretAccessKey:
+ LambdaClientInternals.getEnvVariable(
+ S3_OUTPUT_PROVIDER_SECRET_ACCESS_KEY_ENV_NAME,
+ ) ?? null,
+ region,
+ forcePathStyle,
+ };
+};
+
+export const makeOutNameWithCustomCredentials = ({
+ bucketName,
+ key,
+ s3OutputProvider,
+}: {
+ bucketName: string | undefined;
+ key: string | undefined;
+ s3OutputProvider: CustomCredentials<AwsProvider> | null;
+}): OutNameInput<AwsProvider> | null => {
+ if (!s3OutputProvider) {
+ return key ?? null;
+ }
+
+ if (!bucketName) {
+ throw new Error(
+ 'Pass --force-bucket-name when using S3 output provider flags.',
+ );
+ }
+
+ if (!key) {
+ throw new Error('Pass --out-name when using S3 output provider flags.');
+ }
+
+ return {
+ bucketName,
+ key,
+ s3OutputProvider,
+ };
+};
diff --git a/packages/lambda/src/test/unit/get-s3-output-provider-from-cli.test.ts b/packages/lambda/src/test/unit/get-s3-output-provider-from-cli.test.ts
new file mode 100644
index 0000000000..cd8b40993c
--- /dev/null
+++ b/packages/lambda/src/test/unit/get-s3-output-provider-from-cli.test.ts
@@ -0,0 +1,98 @@
+import {expect, test} from 'bun:test';
+import {
+ getS3OutputProviderFromCli,
+ makeOutNameWithCustomCredentials,
+ S3_OUTPUT_PROVIDER_ACCESS_KEY_ID_ENV_NAME,
+ S3_OUTPUT_PROVIDER_SECRET_ACCESS_KEY_ENV_NAME,
+} from '../../cli/helpers/get-s3-output-provider-from-cli';
+
+test('Should get S3 output provider credentials from env vars', () => {
+ const previousAccessKeyId =
+ process.env[S3_OUTPUT_PROVIDER_ACCESS_KEY_ID_ENV_NAME];
+ const previousSecretAccessKey =
+ process.env[S3_OUTPUT_PROVIDER_SECRET_ACCESS_KEY_ENV_NAME];
+
+ process.env[S3_OUTPUT_PROVIDER_ACCESS_KEY_ID_ENV_NAME] = 'access-key';
+ process.env[S3_OUTPUT_PROVIDER_SECRET_ACCESS_KEY_ENV_NAME] = 'secret-key';
+
+ try {
+ expect(
+ getS3OutputProviderFromCli({
+ endpoint: 'https://example.com',
+ region: 'auto',
+ forcePathStyle: true,
+ }),
+ ).toEqual({
+ endpoint: 'https://example.com',
+ accessKeyId: 'access-key',
+ secretAccessKey: 'secret-key',
+ region: 'auto',
+ forcePathStyle: true,
+ });
+ } finally {
+ if (previousAccessKeyId === undefined) {
+ delete process.env[S3_OUTPUT_PROVIDER_ACCESS_KEY_ID_ENV_NAME];
+ } else {
+ process.env[S3_OUTPUT_PROVIDER_ACCESS_KEY_ID_ENV_NAME] =
+ previousAccessKeyId;
+ }
+
+ if (previousSecretAccessKey === undefined) {
+ delete process.env[S3_OUTPUT_PROVIDER_SECRET_ACCESS_KEY_ENV_NAME];
+ } else {
+ process.env[S3_OUTPUT_PROVIDER_SECRET_ACCESS_KEY_ENV_NAME] =
+ previousSecretAccessKey;
+ }
+ }
+});
+
+test('Should require an endpoint when any S3 output provider flag is passed', () => {
+ expect(() =>
+ getS3OutputProviderFromCli({
+ endpoint: undefined,
+ region: undefined,
+ forcePathStyle: true,
+ }),
+ ).toThrow(
+ 'Pass --s3-output-provider-endpoint when using S3 output provider flags.',
+ );
+});
+
+test('Should require bucket and key when using an S3 output provider', () => {
+ const s3OutputProvider = getS3OutputProviderFromCli({
+ endpoint: 'https://example.com',
+ region: undefined,
+ forcePathStyle: undefined,
+ });
+ if (!s3OutputProvider) {
+ throw new Error('Expected S3 output provider to be parsed');
+ }
+
+ expect(() =>
+ makeOutNameWithCustomCredentials({
+ bucketName: undefined,
+ key: 'video.mp4',
+ s3OutputProvider,
+ }),
+ ).toThrow('Pass --force-bucket-name when using S3 output provider flags.');
+
+ expect(() =>
+ makeOutNameWithCustomCredentials({
+ bucketName: 'bucket',
+ key: undefined,
+ s3OutputProvider,
+ }),
+ ).toThrow('Pass --out-name when using S3 output provider flags.');
+
+ expect(
+ makeOutNameWithCustomCredentials({
+ bucketName: 'bucket',
+ key: 'video.mp4',
+ s3OutputProvider,
+ }),
+ ).toEqual({
+ bucketName: 'bucket',
+ key: 'video.mp4',
+ s3OutputProvider,
+ });
+});