Skip to content

🎥 Make videos programmatically with React

bake aws region

Jonny Burger289ef0e06117
diff --git a/packages/lambda/src/api/get-buckets.ts b/packages/lambda/src/api/get-buckets.ts
index 1696f0fb87..8e239677e7 100644
--- a/packages/lambda/src/api/get-buckets.ts
+++ b/packages/lambda/src/api/get-buckets.ts
@@ -26,12 +26,15 @@ export const getRemotionS3Buckets = async (
);
const locations = await Promise.all(
- remotionBuckets.map((bucket) => {
- return getS3Client(region, null).send(
+ remotionBuckets.map(async (bucket) => {
+ const result = await getS3Client(region, null).send(
new GetBucketLocationCommand({
Bucket: bucket.Name as string,
})
);
+
+ // AWS docs: Buckets in Region us-east-1 have a LocationConstraint of null!!
+ return result.LocationConstraint ?? ('us-east-1' as AwsRegion);
})
);
@@ -40,8 +43,7 @@ export const getRemotionS3Buckets = async (
return {
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,
+ region: locations[i] 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 1e1afc45af..39ff9f2e52 100644
--- a/packages/lambda/src/api/get-or-create-bucket.ts
+++ b/packages/lambda/src/api/get-or-create-bucket.ts
@@ -1,6 +1,6 @@
import type {AwsRegion} from '../pricing/aws-regions';
import {REMOTION_BUCKET_PREFIX} from '../shared/constants';
-import {randomHash} from '../shared/random-hash';
+import {makeBucketName} from '../shared/validate-bucketname';
import {createBucket} from './create-bucket';
import {getRemotionS3Buckets} from './get-buckets';
@@ -37,7 +37,7 @@ export const getOrCreateBucket = async (
return {bucketName: remotionBuckets[0].name};
}
- const bucketName = REMOTION_BUCKET_PREFIX + randomHash();
+ const bucketName = makeBucketName(options.region);
await createBucket({
bucketName,
diff --git a/packages/lambda/src/shared/validate-bucketname.ts b/packages/lambda/src/shared/validate-bucketname.ts
index 82bdd22ced..b263efdb6b 100644
--- a/packages/lambda/src/shared/validate-bucketname.ts
+++ b/packages/lambda/src/shared/validate-bucketname.ts
@@ -1,4 +1,7 @@
+import type {AwsRegion} from '../client';
+import {AWS_REGIONS} from '../regions';
import {REMOTION_BUCKET_PREFIX} from './constants';
+import {randomHash} from './random-hash';
export const validateBucketName = (
bucketName: unknown,
@@ -29,3 +32,30 @@ export const validateBucketName = (
throw new Error(`The bucket ${bucketName} `);
}
};
+
+export const parseBucketName = (
+ name: string
+): {
+ region: AwsRegion | null;
+} => {
+ const parsed = name.match(
+ new RegExp(`^${REMOTION_BUCKET_PREFIX}(.*)-([a-z0-9A-Z]+)$`)
+ );
+ const region = parsed?.[1] as AwsRegion;
+
+ if (!region) {
+ return {region: null};
+ }
+
+ const realRegionFound = AWS_REGIONS.find(
+ (r) => r.replace(/-/g, '') === region
+ );
+
+ return {region: realRegionFound ?? null};
+};
+
+export const makeBucketName = (region: AwsRegion) => {
+ return `${REMOTION_BUCKET_PREFIX}${region.replace(/-/g, '')}-${randomHash({
+ randomInTests: false,
+ })}`;
+};
diff --git a/packages/lambda/src/test/unit/best-bucketnames.test.ts b/packages/lambda/src/test/unit/best-bucketnames.test.ts
new file mode 100644
index 0000000000..2e2d317937
--- /dev/null
+++ b/packages/lambda/src/test/unit/best-bucketnames.test.ts
@@ -0,0 +1,13 @@
+import {expect, test} from 'vitest';
+import {
+ makeBucketName,
+ parseBucketName,
+} from '../../shared/validate-bucketname';
+
+test('Generate and parse bucket names correctly', () => {
+ const name = makeBucketName('us-east-1');
+ expect(name).toBe('remotionlambda-useast1-abcdef');
+
+ const parsed = parseBucketName(name);
+ expect(parsed).toEqual({region: 'us-east-1'});
+});