🎥 Make videos programmatically with React
`sites ls` command
Jonny Burger08cdd69b13cb
diff --git a/packages/docs/docs/deploylambda.md b/packages/docs/docs/deployfunction.mdsimilarity index 99%rename from packages/docs/docs/deploylambda.mdrename to packages/docs/docs/deployfunction.mdindex 918879acd0..709cb69b04 100644--- a/packages/docs/docs/deploylambda.md+++ b/packages/docs/docs/deployfunction.md@@ -42,7 +42,7 @@ The AWS region which you want to deploy the Lambda function too. It must be theHow long the Lambda function may run before it gets killed. Must be below 900 seconds.We recommend a timeout of 120 seconds or lower - remember, Remotion Lambda is the fastest if you render with a high concurrency. If your video takes longer to render, the concurrency should be increased rather than the timeout.-## `memorySize`+### `memorySize`How many megabytes of RAM the Lambda function should have. By default we recommend a value of 1024MB. You may increase or decrease it depending on how memory-consuming your video is. The minimum allowed number is `512`, the maximum allowed number is `10240`. Since the costs of Remotion Lambda is directly proportional to the amount of RAM, we recommend to keep this amount as low as possiblr.diff --git a/packages/lambda/src/api/get-buckets.ts b/packages/lambda/src/api/get-buckets.tsindex f08f4e62e3..343cd96143 100644--- a/packages/lambda/src/api/get-buckets.ts+++ b/packages/lambda/src/api/get-buckets.ts@@ -11,6 +11,7 @@ type BucketWithLocation = Bucket & {region: AwsRegion;};+// TODO: In the CLI, warn everytime this function gets called about more than 1 bucket.export const getRemotionS3Buckets = async (region: AwsRegion) => {const {Buckets} = await getS3Client(region).send(new ListBucketsCommand({}));if (!Buckets) {diff --git a/packages/lambda/src/api/get-sites.ts b/packages/lambda/src/api/get-sites.tsnew file mode 100644index 0000000000..5e15ba5486--- /dev/null+++ b/packages/lambda/src/api/get-sites.ts@@ -0,0 +1,63 @@+import {lambdaLs} from '../functions/helpers/io';+import {AwsRegion} from '../pricing/aws-regions';+import {getSitesKey} from '../shared/constants';+import {getRemotionS3Buckets} from './get-buckets';++type Site = {+ size: number;+ lastModified: number | null;+ bucketName: string;+ id: string;+};++export const getSites = async ({region}: {region: AwsRegion}) => {+ const {remotionBuckets} = await getRemotionS3Buckets(region);++ const sites: {[key: string]: Site} = {};++ for (const bucket of remotionBuckets) {+ const ls = await lambdaLs({+ bucketName: bucket.Name as string,+ prefix: getSitesKey(''),+ region,+ });++ for (const file of ls) {+ const siteKeyMatch = file.Key?.match(/sites\/(.*)\/(.*)$/);+ if (!siteKeyMatch) {+ throw new Error(+ `An file was found in the bucket "${bucket.Name}" with the key ${file.Key} which is an unexpected folder structure. Delete this file.`+ );+ }++ const [, siteId] = siteKeyMatch;+ if (!sites[siteId]) {+ sites[siteId] = {+ size: 0,+ bucketName: bucket.Name as string,+ lastModified: null,+ id: siteId,+ };+ }++ if (file.LastModified) {+ const currentLastModified = sites[siteId].lastModified;+ if (+ currentLastModified === null ||+ file.LastModified.getTime() > currentLastModified+ ) {+ sites[siteId].lastModified = file.LastModified.getTime();+ }+ }++ if (file.Size) {+ sites[siteId].size += file.Size;+ }+ }+ }++ const sitesArray: Site[] = Object.keys(sites).map((siteId) => {+ return sites[siteId];+ });+ return {sites: sitesArray, buckets: remotionBuckets};+};diff --git a/packages/lambda/src/cli/commands/sites/index.ts b/packages/lambda/src/cli/commands/sites/index.tsnew file mode 100644index 0000000000..b2e68eec76--- /dev/null+++ b/packages/lambda/src/cli/commands/sites/index.ts@@ -0,0 +1,29 @@+import {CliInternals} from '@remotion/cli';+import {Log} from '@remotion/cli/dist/log';+import {BINARY_NAME} from '../../../shared/constants';+import {sitesLsSubcommand, SITES_LS_SUBCOMMAND} from './ls';++export const SITES_COMMAND = 'sites';++export const printSitesHelp = () => {+ Log.info(`${BINARY_NAME} ${SITES_COMMAND} <subcommand>`);+ Log.info();+ Log.info('Available subcommands:');+ Log.info();+ Log.info(`${BINARY_NAME} ${SITES_COMMAND} ${SITES_LS_SUBCOMMAND}`);+ Log.info(CliInternals.chalk.gray('Lists the sites currently deployed'));+};++export const sitesCommand = (args: string[]) => {+ if (args[0] === SITES_LS_SUBCOMMAND) {+ return sitesLsSubcommand();+ }++ if (args[0]) {+ Log.error(`Subcommand ${args[0]} not found.`);+ printSitesHelp();+ process.exit(1);+ }++ printSitesHelp();+};diff --git a/packages/lambda/src/cli/commands/sites/ls.ts b/packages/lambda/src/cli/commands/sites/ls.tsnew file mode 100644index 0000000000..a89694778f--- /dev/null+++ b/packages/lambda/src/cli/commands/sites/ls.ts@@ -0,0 +1,67 @@+import {CliInternals} from '@remotion/cli';+import {Log} from '@remotion/cli/dist/log';+import {getSites} from '../../../api/get-sites';+import {makeS3Url} from '../../../shared/make-s3-url';+import {getAwsRegion} from '../../get-aws-region';+import {formatBytes} from '../../helpers/format-bytes';++export const SITES_LS_SUBCOMMAND = 'ls';++// TODO: extract+const dateString = (date: Date) =>+ date.getFullYear() ++ '-' ++ String(date.getMonth() + 1).padEnd(2, '0') ++ '-' ++ String(date.getDate()).padEnd(2, '0');++const COLS: [number, number, number, number] = [20, 30, 10, 15];++const logRow = (data: [string, string, string, string]) => {+ return [+ data[0].padEnd(COLS[0], ' '),+ data[1].padEnd(COLS[1], ' '),+ data[2].padEnd(COLS[2], ' '),+ String(data[3]).padEnd(COLS[3], ' '),+ ].join('');+};++export const sitesLsSubcommand = async () => {+ const region = getAwsRegion();+ const {sites, buckets} = await getSites({region});++ if (buckets.length > 1) {+ Log.warn(+ 'Warning: You have more than one Remotion S3 bucket, but only one is needed. This can lead to conflicts. Remove all but one of them.'+ );+ }++ const sitesPluralized = sites.length === 1 ? 'site' : 'sites';+ Log.info(`${sites.length} ${sitesPluralized} in the ${region} region.`);++ Log.info();+ Log.info(+ CliInternals.chalk.gray(+ logRow(['Site ID', 'Bucket', 'Size', 'Last updated'])+ )+ );++ for (const site of sites) {+ Log.info(+ logRow([+ site.id,+ site.bucketName,+ formatBytes(site.size),+ site.lastModified ? dateString(new Date(site.lastModified)) : 'n/a',+ ])+ );+ Log.info(+ makeS3Url({+ bucketName: site.bucketName,+ region,+ subFolder: `sites/${site.id}`,+ })+ );+ Log.info();+ }+};diff --git a/packages/lambda/src/cli/index.ts b/packages/lambda/src/cli/index.tsindex 5bb575d981..55e6ee9121 100644--- a/packages/lambda/src/cli/index.ts+++ b/packages/lambda/src/cli/index.ts@@ -2,6 +2,7 @@ import {CliInternals} from '@remotion/cli';import {parsedLambdaCli} from './args';import {cleanupCommand, CLEANUP_COMMAND} from './cleanup';import {functionsCommand, FUNCTIONS_COMMAND} from './commands/functions';+import {sitesCommand, SITES_COMMAND} from './commands/sites';import {printHelp} from './help';import {Log} from './log';import {policiesCommand, POLICIES_COMMAND} from './policies';@@ -34,6 +35,10 @@ const matchCommand = async () => {return policiesCommand(parsedLambdaCli._.slice(1));}+ if (parsedLambdaCli._[0] === SITES_COMMAND) {+ return sitesCommand(parsedLambdaCli._.slice(1));+ }+Log.error(`Command ${parsedLambdaCli._[0]} not found.`);printHelp();process.exit(1);diff --git a/packages/lambda/src/functions/helpers/io.ts b/packages/lambda/src/functions/helpers/io.tsindex 274e24ae5a..4f84cd61c6 100644--- a/packages/lambda/src/functions/helpers/io.ts+++ b/packages/lambda/src/functions/helpers/io.ts@@ -18,6 +18,7 @@ export const lambdaLs = async ({prefix: string;region: AwsRegion;}): Promise<_Object[]> => {+ // TODO: Should paginate with list.ContinuationTokenconst list = await getS3Client(region).send(new ListObjectsV2Command({Bucket: bucketName,