Skip to content

🎥 Make videos programmatically with React

Add large payload support to lambda-php via S3 upload

Helder Lucasee9a49196f69
diff --git a/packages/lambda-php/src/PHPClient.php b/packages/lambda-php/src/PHPClient.php
index 5c92bda71d..50da8a5ca1 100644
--- a/packages/lambda-php/src/PHPClient.php
+++ b/packages/lambda-php/src/PHPClient.php
@@ -1,33 +1,258 @@
<?php
namespace Remotion\LambdaPhp;
-use Aws\Lambda\LambdaClient;
-use Exception;
use stdClass;
+use Exception;
+use Aws\S3\S3Client;
+use Aws\Lambda\LambdaClient;
+use InvalidArgumentException;
+use Aws\Exception\AwsException;
class PHPClient
{
+ private const BUCKET_NAME_PREFIX = 'remotionlambda-';
+ private const REGION_US_EAST = 'us-east-1';
+
protected $client;
+ protected $s3Client;
protected $region;
protected $serveUrl;
protected $functionName;
+ protected $credential;
+ protected $forcePathStyle;
- public function __construct(string $region, string $serveUrl, string $functionName, ?callable $credential)
+ public function __construct(string $region, string $serveUrl, string $functionName, ?callable $credential, bool $forcePathStyle = false)
{
$this->client = new LambdaClient([
'version' => '2015-03-31',
'region' => $region,
'credentials' => $credential,
]);
+ $this->credential = $credential;
+ $this->forcePathStyle = $forcePathStyle;
$this->setRegion($region);
$this->setServeUrl($serveUrl);
$this->setFunctionName($functionName);
}
+ /**
+ * Create S3 client with appropriate credentials
+ */
+ private function createS3Client(): S3Client
+ {
+ if ($this->s3Client !== null) {
+ return $this->s3Client;
+ }
+
+ $config = [
+ 'version' => 'latest',
+ 'region' => $this->region,
+ ];
+
+ if ($this->forcePathStyle) {
+ $config['use_path_style_endpoint'] = true;
+ }
+
+ if ($this->credential !== null) {
+ $config['credentials'] = $this->credential;
+ }
+
+ $this->s3Client = new S3Client($config);
+
+ return $this->s3Client;
+ }
+
+ /**
+ * Generate a SHA256 hash for the payload
+ */
+ private function generateHash(string $payload): string
+ {
+ return hash('sha256', $payload);
+ }
+
+ /**
+ * Generate a random hash for bucket operations
+ */
+ private function generateRandomHash(): string
+ {
+ $alphabet = 'abcdefghijklmnopqrstuvwxyz0123456789';
+
+ return substr(str_shuffle(str_repeat($alphabet, 10)), 0, 10);
+ }
+
+ /**
+ * Generate a bucket name following Remotion conventions
+ */
+ private function makeBucketName(): string
+ {
+ $regionNoDashes = str_replace('-', '', $this->region);
+ $randomSuffix = $this->generateRandomHash();
+
+ return self::BUCKET_NAME_PREFIX . $regionNoDashes . '-' . $randomSuffix;
+ }
+
+ /**
+ * Generate S3 key for input props
+ */
+ private function inputPropsKey(string $hash): string
+ {
+ return "input-props/{$hash}.json";
+ }
+
+ /**
+ * Check if a bucket is in the current region
+ */
+ private function isBucketInCurrentRegion(S3Client $s3Client, string $bucket): bool
+ {
+ try {
+ $result = $s3Client->getBucketLocation(['Bucket' => $bucket]);
+ $location = $result['LocationConstraint'];
+
+ return $location === $this->region || ($location === null && $this->region === self::REGION_US_EAST);
+ } catch (AwsException $exception) {
+ return false;
+ }
+ }
+
+ /**
+ * Get all Remotion buckets in the current region
+ */
+ private function getRemotionBuckets(): array
+ {
+ $s3Client = $this->createS3Client();
+
+ try {
+ $result = $s3Client->listBuckets();
+ } catch (AwsException $exception) {
+ error_log("Could not list S3 buckets: " . $exception->getMessage());
+ return [];
+ }
+
+ $buckets = [];
+
+ foreach ($result['Buckets'] as $bucket) {
+ $name = $bucket['Name'];
+
+ if (strpos($name, self::BUCKET_NAME_PREFIX) !== 0) {
+ continue;
+ }
+
+ if ($this->isBucketInCurrentRegion($s3Client, $name)) {
+ $buckets[] = $name;
+ }
+ }
+
+ return $buckets;
+ }
+
+ /**
+ * Get existing bucket or create a new one following JS SDK logic
+ */
+ private function getOrCreateBucket(): string
+ {
+ $buckets = $this->getRemotionBuckets();
+
+ if (count($buckets) > 1) {
+ throw new Exception(
+ sprintf(
+ "You have multiple buckets (%s) in your S3 region (%s) starting with \"remotionlambda-\". " .
+ "Please see https://remotion.dev/docs/lambda/multiple-buckets.",
+ implode(', ', $buckets),
+ $this->region
+ )
+ );
+ }
+
+ if (count($buckets) === 1) {
+ return $buckets[0];
+ }
+
+ $bucket = $this->makeBucketName();
+ $s3Client = $this->createS3Client();
+
+ try {
+ $params = ['Bucket' => $bucket];
+
+ if ($this->region !== self::REGION_US_EAST) {
+ $params['CreateBucketConfiguration'] = [
+ 'LocationConstraint' => $this->region
+ ];
+ }
+
+ $s3Client->createBucket($params);
+
+ return $bucket;
+ } catch (AwsException $exception) {
+ throw new Exception("Failed to create bucket: " . $exception->getMessage());
+ }
+ }
+
+ /**
+ * Upload payload to S3
+ */
+ private function uploadToS3(string $bucket, string $key, string $payload): void
+ {
+ $s3Client = $this->createS3Client();
+
+ try {
+ $s3Client->putObject([
+ 'Bucket' => $bucket,
+ 'Key' => $key,
+ 'Body' => $payload,
+ 'ContentType' => 'application/json',
+ ]);
+ } catch (AwsException $exception) {
+ throw new Exception("Failed to upload to S3: {$exception->getMessage()}");
+ }
+ }
+
+ /**
+ * Determine if payload needs to be uploaded to S3
+ */
+ private function needsUpload(int $payloadSize, string $renderType): bool
+ {
+ // Constants based on AWS Lambda limits with margin for other payload data
+ $margin = 5_000 + 1_024; // 5KB margin + 1KB for webhook data
+ $maxStillInlineSize = 5_000_000 - $margin;
+ $maxVideoInlineSize = 200_000 - $margin;
+
+ $maxSize = ($renderType === 'still') ? $maxStillInlineSize : $maxVideoInlineSize;
+
+ if ($payloadSize < $maxSize) {
+ return false;
+ }
+
+ $message = sprintf(
+ "Warning: The props are over %dKB (%dKB) in size. Uploading them to S3 to " .
+ "circumvent AWS Lambda payload size, which may lead to slowdown.",
+ round($maxSize / 1000),
+ ceil($payloadSize / 1024)
+ );
+
+ error_log($message);
+
+ return true;
+ }
+
+ /**
+ * Normalize payload to ensure valid JSON object is returned
+ *
+ * @param string|null $payload The JSON payload to normalize
+ * @return string Valid JSON string, defaulting to empty object if invalid
+ */
+ private function ensureValidPayload(?string $payload): string
+ {
+ if (is_null($payload) || empty($payload) || $payload === "null") {
+ return json_encode(new stdClass());
+ }
+
+ return $payload;
+ }
+
public function constructInternals(RenderParams $render)
{
if (empty($render->getComposition())) {
- throw new ValidationException("'composition' is required.");
+ throw new InvalidArgumentException("'composition' is required.");
}
$input = $this->serializeInputProps(
@@ -54,7 +279,7 @@ class PHPClient
public function makeRenderProgressPayload(string $renderId, string $bucketName, string $logLevel = "info", $forcePathStyle = false)
{
- $params = array(
+ return json_encode([
'renderId' => $renderId,
'bucketName' => $bucketName,
'type' => 'status',
@@ -62,9 +287,7 @@ class PHPClient
"s3OutputProvider" => null,
"logLevel" => $logLevel,
"forcePathStyle" => $forcePathStyle,
- );
- $result = json_encode($params);
- return $result;
+ ]);
}
public function getRenderProgress(string $renderId, string $bucketName, string $logLevel = "info", $forcePathStyle = false): GetRenderProgressResponse
@@ -104,9 +327,11 @@ class PHPClient
if ($response['type'] === 'error') {
throw new Exception($response['message']);
}
+
$classResponse->type = $response['type'];
$classResponse->renderId = $response['renderId'];
$classResponse->bucketName = $response['bucketName'];
+
return $classResponse;
}
@@ -132,29 +357,36 @@ class PHPClient
$classResponse->renderSize = $response['renderSize'];
$classResponse->timeToFinish = $response['timeToFinish'];
$classResponse->type = $response['type'];
+
return $classResponse;
}
+ /**
+ * Serialize inputProps to a format compatible with Lambda
+ */
private function serializeInputProps($inputProps, string $region, string $type, ?string $userSpecifiedBucketName): array
{
try {
- $payload = json_encode($inputProps);
- $MAX_INLINE_PAYLOAD_SIZE = $type === 'still' ? 5000000 : 200000;
-
- if (strlen($payload) > $MAX_INLINE_PAYLOAD_SIZE) {
- throw new Exception(
- sprintf(
- "Warning: inputProps are over %dKB (%dKB) in size.\n This is not currently supported.",
- round($MAX_INLINE_PAYLOAD_SIZE / 1000),
- ceil(strlen($payload) / 1024)
- )
- );
+ $payload = json_encode($inputProps, JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE);
+ $payloadSize = strlen($payload);
+
+ if (! $this->needsUpload($payloadSize, $type)) {
+ return [
+ 'type' => 'payload',
+ 'payload' => $this->ensureValidPayload($payload),
+ ];
}
+ $hash = $this->generateHash($payload);
+ $bucketName = $this->getOrCreateBucket();
+ $key = $this->inputPropsKey($hash);
+
+ $this->uploadToS3($bucketName, $key, $payload);
+
return [
- 'type' => 'payload',
- 'payload' => !is_null($payload) && !empty($payload) && $payload !== "null" ?
- $payload : json_encode(new stdClass()),
+ 'hash' => $hash,
+ 'type' => 'bucket-url',
+ 'bucketName' => $bucketName,
];
} catch (Exception $e) {
throw new Exception(
diff --git a/packages/lambda-php/tests/LargePayloadTest.php b/packages/lambda-php/tests/LargePayloadTest.php
new file mode 100644
index 0000000000..f5bc92abf3
--- /dev/null
+++ b/packages/lambda-php/tests/LargePayloadTest.php
@@ -0,0 +1,161 @@
+<?php
+
+namespace Remotion\LambdaPhp\Tests;
+
+use PHPUnit\Framework\TestCase;
+use Remotion\LambdaPhp\PHPClient;
+use ReflectionClass;
+use ReflectionMethod;
+
+/**
+ * Test large payload compression and S3 upload functionality
+ */
+class LargePayloadTest extends TestCase
+{
+ private PHPClient $client;
+ private ReflectionClass $reflectionClass;
+
+ protected function setUp(): void
+ {
+ $this->client = new PHPClient(
+ 'us-east-1',
+ 'testbed',
+ 'remotion-render',
+ null,
+ false
+ );
+ $this->reflectionClass = new ReflectionClass(PHPClient::class);
+ }
+
+ /**
+ * Helper method to call private methods
+ */
+ private function callPrivateMethod(string $methodName, array $args = [])
+ {
+ $method = $this->reflectionClass->getMethod($methodName);
+ $method->setAccessible(true);
+ return $method->invokeArgs($this->client, $args);
+ }
+
+ /**
+ * Test that small payloads use the payload format
+ */
+ public function testSmallPayloadUsesPayloadFormat(): void
+ {
+ $smallProps = ['message' => 'small'];
+ $result = $this->callPrivateMethod('serializeInputProps', [
+ $smallProps,
+ 'us-east-1',
+ 'video-or-audio',
+ null
+ ]);
+
+ $this->assertEquals('payload', $result['type']);
+ $this->assertArrayHasKey('payload', $result);
+ $this->assertArrayNotHasKey('hash', $result);
+ $this->assertArrayNotHasKey('bucketName', $result);
+ }
+
+ /**
+ * Test the logic for determining when upload is needed
+ */
+ public function testNeedsUploadLogic(): void
+ {
+ // Small payload should not need upload
+ $this->assertFalse($this->callPrivateMethod('needsUpload', [1000, 'video-or-audio']));
+ $this->assertFalse($this->callPrivateMethod('needsUpload', [1000, 'still']));
+
+ // Large payload for video should need upload
+ $this->assertTrue($this->callPrivateMethod('needsUpload', [200000, 'video-or-audio']));
+
+ // Large payload for still should need upload (over 5MB)
+ $this->assertTrue($this->callPrivateMethod('needsUpload', [5000000, 'still']));
+
+ // Medium payload for still should not need upload
+ $this->assertFalse($this->callPrivateMethod('needsUpload', [1000000, 'still']));
+ }
+
+ /**
+ * Test that hash generation is consistent
+ */
+ public function testHashGeneration(): void
+ {
+ $payload = '{"test": "data"}';
+ $hash1 = $this->callPrivateMethod('generateHash', [$payload]);
+ $hash2 = $this->callPrivateMethod('generateHash', [$payload]);
+
+ $this->assertEquals($hash1, $hash2);
+ $this->assertIsString($hash1);
+ $this->assertEquals(64, strlen($hash1)); // SHA256 hex string length
+ }
+
+ /**
+ * Test S3 key generation for input props
+ */
+ public function testInputPropsKeyGeneration(): void
+ {
+ $hashValue = 'test123';
+ $key = $this->callPrivateMethod('inputPropsKey', [$hashValue]);
+ $this->assertEquals('input-props/test123.json', $key);
+ }
+
+ /**
+ * Test bucket name generation following JS SDK conventions
+ */
+ public function testBucketNameGeneration(): void
+ {
+ $bucketName = $this->callPrivateMethod('makeBucketName', []);
+
+ // Should start with remotionlambda- prefix
+ $this->assertStringStartsWith('remotionlambda-', $bucketName);
+
+ // Should contain region without dashes
+ $expectedRegion = str_replace('-', '', 'us-east-1');
+ $this->assertStringContainsString($expectedRegion, $bucketName);
+
+ // Should be in format: remotionlambda-{region-no-dashes}-{random-hash}
+ $parts = explode('-', $bucketName);
+ $this->assertCount(3, $parts); // remotionlambda, region, hash
+ $this->assertEquals('remotionlambda', $parts[0]);
+ $this->assertEquals($expectedRegion, $parts[1]);
+ $this->assertEquals(10, strlen($parts[2])); // random hash should be 10 chars
+ }
+
+ /**
+ * Test random hash generation
+ */
+ public function testRandomHashGeneration(): void
+ {
+ $hash1 = $this->callPrivateMethod('generateRandomHash', []);
+ $hash2 = $this->callPrivateMethod('generateRandomHash', []);
+
+ // Should be 10 characters long
+ $this->assertEquals(10, strlen($hash1));
+ $this->assertEquals(10, strlen($hash2));
+
+ // Should be different (extremely unlikely to be the same)
+ $this->assertNotEquals($hash1, $hash2);
+
+ // Should only contain lowercase alphanumeric characters
+ $this->assertMatchesRegularExpression('/^[a-z0-9]{10}$/', $hash1);
+ $this->assertMatchesRegularExpression('/^[a-z0-9]{10}$/', $hash2);
+ }
+
+ /**
+ * Test payload size calculation matches expected thresholds
+ */
+ public function testPayloadSizeThresholds(): void
+ {
+ $margin = 5000 + 1024;
+ $maxStillInlineSize = 5000000 - $margin;
+ $maxVideoInlineSize = 200000 - $margin;
+
+ // Test video threshold
+ $this->assertFalse($this->callPrivateMethod('needsUpload', [$maxVideoInlineSize - 1, 'video-or-audio']));
+ $this->assertTrue($this->callPrivateMethod('needsUpload', [$maxVideoInlineSize + 1, 'video-or-audio']));
+
+ // Test still threshold
+ $this->assertFalse($this->callPrivateMethod('needsUpload', [$maxStillInlineSize - 1, 'still']));
+ $this->assertTrue($this->callPrivateMethod('needsUpload', [$maxStillInlineSize + 1, 'still']));
+ }
+}