Skip to content

🎥 Make videos programmatically with React

Test and refactor list buckets

Vlad Mb3a42f92ca97
diff --git a/packages/lambda-python/remotion_lambda/remotionclient.py b/packages/lambda-python/remotion_lambda/remotionclient.py
index 6721ec66c2..72a9e5c28d 100644
--- a/packages/lambda-python/remotion_lambda/remotionclient.py
+++ b/packages/lambda-python/remotion_lambda/remotionclient.py
@@ -4,7 +4,7 @@ import random
import json
import hashlib
from math import ceil
-from typing import Optional, Union
+from typing import Optional, Union, List
from enum import Enum
import boto3
from botocore.exceptions import ClientError
@@ -20,8 +20,11 @@ from .models import (
RenderStillParams,
RenderType,
)
+import logging
+logger = logging.getLogger(__name__)
+
BUCKET_NAME_PREFIX = 'remotionlambda-'
REGION_US_EAST = 'us-east-1'
@@ -94,33 +97,41 @@ class RemotionClient:
return boto3.client('s3', **kwargs)
- def _get_remotion_buckets(self):
- """Get existing Remotion buckets in the region."""
+ def _get_remotion_buckets(self) -> List[str]:
s3_client = self._create_s3_client()
+
try:
response = s3_client.list_buckets()
- buckets = []
- for bucket in response['Buckets']:
- bucket_name = bucket['Name']
- if bucket_name.startswith(BUCKET_NAME_PREFIX):
- # Check if bucket is in the correct region
- try:
- bucket_region = s3_client.get_bucket_location(
- Bucket=bucket_name
- )
- location = bucket_region.get('LocationConstraint')
- # us-east-1 returns None for LocationConstraint
- if location == self.region or (
- location is None and self.region == REGION_US_EAST
- ):
- buckets.append(bucket_name)
- except ClientError:
- # Ignore buckets we can't access
- continue
- return buckets
- except ClientError:
+ except ClientError as e:
+ logger.warning(f"Could not list S3 buckets: {e}")
return []
+ remotion_buckets = []
+
+ for bucket in response.get('Buckets', []):
+ bucket_name = bucket['Name']
+
+ if not bucket_name.startswith(BUCKET_NAME_PREFIX):
+ continue
+
+ if self._is_bucket_in_current_region(s3_client, bucket_name):
+ remotion_buckets.append(bucket_name)
+
+ return remotion_buckets
+
+ def _is_bucket_in_current_region(self, s3_client, bucket_name: str) -> bool:
+ try:
+ bucket_region = s3_client.get_bucket_location(Bucket=bucket_name)
+ location = bucket_region.get('LocationConstraint')
+
+ # us-east-1 returns None for LocationConstraint
+ return location == self.region or (
+ location is None and self.region == REGION_US_EAST
+ )
+ except ClientError:
+ # Ignore buckets we can't access (permission issues, etc.)
+ return False
+
def _get_or_create_bucket(self):
"""Get existing bucket or create a new one following JS SDK logic."""
buckets = self._get_remotion_buckets()
@@ -178,7 +189,7 @@ class RemotionClient:
if payload_size > max_size:
# Log warning similar to JavaScript implementation
- print(
+ logger.warning(
f"Warning: The props are over {round(max_size / 1000)}KB "
f"({ceil(payload_size / 1024)}KB) in size. Uploading them to S3 to "
f"circumvent AWS Lambda payload size, which may lead to slowdown."
diff --git a/packages/lambda-python/tests/test_remotion_client.py b/packages/lambda-python/tests/test_remotion_client.py
index 1fb82ae66a..e8fe56ed52 100644
--- a/packages/lambda-python/tests/test_remotion_client.py
+++ b/packages/lambda-python/tests/test_remotion_client.py
@@ -121,3 +121,56 @@ def test_create_s3_client_with_path_style(mock_boto_client, mock_s3_client):
assert isinstance(config, Config)
assert config.s3['addressing_style'] == 'path'
assert result == mock_s3_client
+
+
[email protected](RemotionClient, '_create_s3_client')
+def test_get_remotion_buckets_empty_response(
+ mock_create_client, mock_s3_client, remotion_client
+):
+ mock_s3_client.list_buckets.return_value = {'Buckets': []}
+ mock_create_client.return_value = mock_s3_client
+
+ result = remotion_client._get_remotion_buckets()
+
+ assert result == []
+ mock_s3_client.list_buckets.assert_called_once()
+
+
[email protected](RemotionClient, '_create_s3_client')
+def test_get_remotion_buckets_no_remotion_buckets(
+ mock_create_client, mock_s3_client, remotion_client
+):
+ mock_s3_client.list_buckets.return_value = {
+ 'Buckets': [
+ {'Name': 'my-app-bucket'},
+ {'Name': 'some-other-bucket'},
+ {'Name': 'user-data-bucket'},
+ ]
+ }
+ mock_create_client.return_value = mock_s3_client
+
+ result = remotion_client._get_remotion_buckets()
+
+ assert result == []
+ mock_s3_client.list_buckets.assert_called_once()
+ mock_s3_client.get_bucket_location.assert_not_called()
+
+
[email protected](RemotionClient, '_create_s3_client')
+def test_get_remotion_buckets_single_match_us_east_1(
+ mock_create_client, mock_s3_client, remotion_client
+):
+ test_bucket_name = 'remotionlambda-useast1-abc123'
+ mock_s3_client.list_buckets.return_value = {
+ 'Buckets': [
+ {'Name': test_bucket_name},
+ {'Name': 'other-bucket'},
+ ]
+ }
+ mock_s3_client.get_bucket_location.return_value = {'LocationConstraint': None}
+ mock_create_client.return_value = mock_s3_client
+
+ result = remotion_client._get_remotion_buckets()
+
+ assert result == [test_bucket_name]
+ mock_s3_client.get_bucket_location.assert_called_once_with(Bucket=test_bucket_name)