Skip to content

🎥 Make videos programmatically with React

refactor...

Jonny Burgerb3c0e5c0ceb0
diff --git a/packages/docs/docs/lambda-changelog.md b/packages/docs/docs/lambda-changelog.md
index 3c2db50ec5..92869c36ea 100644
--- a/packages/docs/docs/lambda-changelog.md
+++ b/packages/docs/docs/lambda-changelog.md
@@ -14,6 +14,10 @@ Keep track of changes to the APIs of Remotion Lambda here.
1. Redeploy your function: `npx remotion lambda functions deploy`
1. Migrate according to the changelog below:
+## Unreleased
+
+- The `getSites()` method returns a property `sizeInBytes` which was previously `size`.
+
## October 3rd, 2021
Version hash: `2.5.0-alpha.5da9a754`
diff --git a/packages/docs/docs/lambda-getsites.md b/packages/docs/docs/lambda-getsites.md
index b763ba3df0..d4809cf8ee 100644
--- a/packages/docs/docs/lambda-getsites.md
+++ b/packages/docs/docs/lambda-getsites.md
@@ -4,4 +4,89 @@ title: getSites()
slug: /lambda/getsites
---
-TODO
+Gets an array of Remotion projects in your S3 account.
+
+The projects are located in the `sites/` subdirectory of your S3 bucket. Remember - you should only have one bucket for Remotion Lambba per region, therefore you do not need to specify the name of the bucket for this function.
+
+```ts twoslash
+// @module: ESNext
+// @target: ESNext
+import {getSites} from '@remotion/lambda';
+
+const {sites, buckets} = await getSites({
+ region: 'eu-central-1'
+})
+
+for (const site of sites) {
+ console.log(site.id) // A unique ID for referring to that project
+ console.log(site.bucketName) // In which bucket the site resides in.
+ console.log(site.lastModified) // A unix timestamp, but may also be null
+ console.log(site.sizeInBytes) // Size of all contents in the folder
+}
+
+for (const bucket of buckets) {
+ console.log(bucket.region) // 'eu-central-1'
+ console.log(bucket.name) // The name of the S3 bucket.
+ console.log(bucket.creationDate) // A unix timestamp of when the site was created.
+}
+```
+
+## Arguments
+
+An object with the following properties:
+
+### `region`
+
+The [AWS region](/docs/lambda/region-selection) which you want to query.
+
+## Return value
+
+A promise resolving to an object with the following properties:
+
+### `sites`
+
+An array of deployed Remotion projects that you can use for rendering.
+
+Each items contains the following properties:
+
+#### `id`
+
+A unique identifier for that project.
+
+#### `bucketName`
+
+The bucket in which the project resides in.
+
+#### `lastModified`
+
+When the files in that project were last changed.
+
+#### `sizeInBytes`
+
+The combined size of all files in that project.
+
+### `buckets`
+
+An array of all buckets in the selected region in your account that start with `remotionlambda-`.
+
+:::info
+You should only have 1 bucket for all your Remotion projects. Nonetheless `buckets` is an array, since we cannot prevent you from manually creating additional buckets with the `remotionlambda-` prefix.
+:::
+
+Each item contains the following properties:
+
+#### `region`
+
+The region the bucket resides in.
+
+#### `name`
+
+The name of the bucket. S3 buckets have globally unique names.
+
+#### `creationDate`
+
+A UNIX timestamp of the point when the bucket was first created.
+
+## See also
+
+- [deleteSite()](/docs/lambda/deletesite)
diff --git a/packages/lambda/src/api/get-buckets.ts b/packages/lambda/src/api/get-buckets.ts
index ed7c10230f..5147e8995f 100644
--- a/packages/lambda/src/api/get-buckets.ts
+++ b/packages/lambda/src/api/get-buckets.ts
@@ -1,13 +1,11 @@
-import {
- Bucket,
- GetBucketLocationCommand,
- ListBucketsCommand,
-} from '@aws-sdk/client-s3';
+import {GetBucketLocationCommand, ListBucketsCommand} from '@aws-sdk/client-s3';
import {AwsRegion} from '../pricing/aws-regions';
import {getS3Client} from '../shared/aws-clients';
import {REMOTION_BUCKET_PREFIX} from '../shared/constants';
-export type BucketWithLocation = Bucket & {
+export type BucketWithLocation = {
+ name: string;
+ creationDate: number;
region: AwsRegion;
};
@@ -42,7 +40,8 @@ export const getRemotionS3Buckets = async (
const bucketsWithLocation = remotionBuckets.map(
(bucket, i): BucketWithLocation => {
return {
- ...bucket,
+ creationDate: (bucket.CreationDate as Date).getTime(),
+ name: bucket.Name as string,
// AWS docs: Buckets in Region us-east-1 have a LocationConstraint of null!!
region: (locations[i].LocationConstraint ?? 'us-east-1') as AwsRegion,
};
diff --git a/packages/lambda/src/api/get-or-create-bucket.ts b/packages/lambda/src/api/get-or-create-bucket.ts
index abd20e06f4..2d28d9787d 100644
--- a/packages/lambda/src/api/get-or-create-bucket.ts
+++ b/packages/lambda/src/api/get-or-create-bucket.ts
@@ -27,7 +27,7 @@ export const getOrCreateBucket = async (
if (remotionBuckets.length > 1) {
throw new Error(
`You have multiple buckets (${remotionBuckets.map(
- (b) => b.Name
+ (b) => b.name
)}) in your S3 region (${
options.region
}) starting with "${REMOTION_BUCKET_PREFIX}". This is an error, please delete buckets so that you have one maximum.`
@@ -36,7 +36,7 @@ export const getOrCreateBucket = async (
if (remotionBuckets.length === 1) {
options.onBucketEnsured?.();
- return {bucketName: remotionBuckets[0].Name as string};
+ return {bucketName: remotionBuckets[0].name};
}
const bucketName = REMOTION_BUCKET_PREFIX + randomHash();
diff --git a/packages/lambda/src/api/get-sites.ts b/packages/lambda/src/api/get-sites.ts
index 24c7f9288c..7d5636a8c8 100644
--- a/packages/lambda/src/api/get-sites.ts
+++ b/packages/lambda/src/api/get-sites.ts
@@ -2,17 +2,27 @@ import {lambdaLs} from '../functions/helpers/io';
import {AwsRegion} from '../pricing/aws-regions';
import {getSitesKey} from '../shared/constants';
import {getAccountId} from '../shared/get-account-id';
-import {getRemotionS3Buckets} from './get-buckets';
+import {BucketWithLocation, getRemotionS3Buckets} from './get-buckets';
type Site = {
- size: number;
+ sizeInBytes: number;
lastModified: number | null;
bucketName: string;
id: string;
};
+type GetSitesReturnValue = {
+ sites: Site[];
+ buckets: BucketWithLocation[];
+};
+
+// TODO: Return the `serveUrl` as well
// TODO: Add JSDoc comments
-export const getSites = async ({region}: {region: AwsRegion}) => {
+export const getSites = async ({
+ region,
+}: {
+ region: AwsRegion;
+}): Promise<GetSitesReturnValue> => {
const {remotionBuckets} = await getRemotionS3Buckets(region);
const accountId = await getAccountId({region});
@@ -20,7 +30,7 @@ export const getSites = async ({region}: {region: AwsRegion}) => {
for (const bucket of remotionBuckets) {
const ls = await lambdaLs({
- bucketName: bucket.Name as string,
+ bucketName: bucket.name,
prefix: getSitesKey(''),
region,
expectedBucketOwner: accountId,
@@ -30,15 +40,15 @@ export const getSites = async ({region}: {region: AwsRegion}) => {
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.`
+ `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,
+ sizeInBytes: 0,
+ bucketName: bucket.name,
lastModified: null,
id: siteId,
};
@@ -55,7 +65,7 @@ export const getSites = async ({region}: {region: AwsRegion}) => {
}
if (file.Size) {
- sites[siteId].size += file.Size;
+ sites[siteId].sizeInBytes += file.Size;
}
}
}
diff --git a/packages/lambda/src/api/iam-validation/required-permissions.ts b/packages/lambda/src/api/iam-validation/required-permissions.ts
index 6f0ad8af92..6dac4389e7 100644
--- a/packages/lambda/src/api/iam-validation/required-permissions.ts
+++ b/packages/lambda/src/api/iam-validation/required-permissions.ts
@@ -1,6 +1,8 @@
import {iam, lambda, s3} from 'aws-policies';
import {REMOTION_BUCKET_PREFIX, RENDER_FN_PREFIX} from '../../shared/constants';
+export const REMOTION_AWS_ACCOUNT = '678892195805';
+
// TODO: Update docs before release
export const requiredPermissions: {
actions: (s3 | iam | lambda)[];
diff --git a/packages/lambda/src/cleanup/s3-buckets.ts b/packages/lambda/src/cleanup/s3-buckets.ts
index 81fa348f2d..aafd46a90b 100644
--- a/packages/lambda/src/cleanup/s3-buckets.ts
+++ b/packages/lambda/src/cleanup/s3-buckets.ts
@@ -68,15 +68,15 @@ export const cleanUpBuckets = async ({
}
for (const bucket of remotionBuckets) {
- onBeforeBucketDeleted?.(bucket.Name as string);
+ onBeforeBucketDeleted?.(bucket.name);
await cleanBucket({
region,
- bucket: bucket.Name as string,
+ bucket: bucket.name,
onAfterItemDeleted: onAfterItemDeleted ?? (() => undefined),
onBeforeItemDeleted: onBeforeItemDeleted ?? (() => undefined),
expectedBucketOwner,
});
- onAfterBucketDeleted?.(bucket.Name as string);
+ onAfterBucketDeleted?.(bucket.name);
}
await cleanUpBuckets({
diff --git a/packages/lambda/src/cli/cleanup.ts b/packages/lambda/src/cli/cleanup.ts
index df218741bb..6dfd3c7b82 100644
--- a/packages/lambda/src/cli/cleanup.ts
+++ b/packages/lambda/src/cli/cleanup.ts
@@ -78,7 +78,7 @@ export const cleanupCommand = async (args: string[]) => {
`${chunk(remotionBuckets, 2)
.map((b) =>
CliInternals.chalk.blue(
- b.map((a) => (a.Name as string).padEnd(40, ' ')).join(' ')
+ b.map((a) => a.name.padEnd(40, ' ')).join(' ')
)
)
.join('\n')}`
diff --git a/packages/lambda/src/cli/commands/sites/ls.ts b/packages/lambda/src/cli/commands/sites/ls.ts
index fffa085d73..389a92e2c9 100644
--- a/packages/lambda/src/cli/commands/sites/ls.ts
+++ b/packages/lambda/src/cli/commands/sites/ls.ts
@@ -44,7 +44,7 @@ export const sitesLsSubcommand = async () => {
logRow([
site.id,
site.bucketName,
- formatBytes(site.size),
+ formatBytes(site.sizeInBytes),
site.lastModified ? dateString(new Date(site.lastModified)) : 'n/a',
])
);
diff --git a/packages/lambda/src/cli/commands/sites/rm.ts b/packages/lambda/src/cli/commands/sites/rm.ts
index 675e7f551a..f6d871cc46 100644
--- a/packages/lambda/src/cli/commands/sites/rm.ts
+++ b/packages/lambda/src/cli/commands/sites/rm.ts
@@ -33,7 +33,7 @@ export const sitesRmSubcommand = async (args: string[]) => {
}
const {totalSize} = await deleteSite({
- bucketName: remotionBuckets[0].Name as string,
+ bucketName: remotionBuckets[0].name,
siteName,
region,
onAfterItemDeleted: ({itemName}) => {