Skip to content

Instantly share code, notes, and snippets.

@GarryOne
Last active March 28, 2021 19:50
Show Gist options
  • Select an option

  • Save GarryOne/26c9da1bbb62700b5de880fb3e933fb0 to your computer and use it in GitHub Desktop.

Select an option

Save GarryOne/26c9da1bbb62700b5de880fb3e933fb0 to your computer and use it in GitHub Desktop.

Revisions

  1. GarryOne revised this gist Mar 28, 2021. 1 changed file with 0 additions and 1 deletion.
    1 change: 0 additions & 1 deletion save-twilio-compositions-to-s3-bucket.php
    Original file line number Diff line number Diff line change
    @@ -34,7 +34,6 @@ public function __construct()
    function saveCompositionMedia()
    {
    $compositionSid = $_POST['CompositionSid'];
    $rolePlayId = $_GET['rolePlayId'];

    $client = new Client(self::TWILIO_API_KEY, self::TWILIO_API_SECRET);
    $compositionPath = str_replace("%CompositionSid%", $compositionSid, self::TWILIO_COMPOSITION_MEDIA_PATH);
  2. GarryOne created this gist Mar 28, 2021.
    84 changes: 84 additions & 0 deletions save-twilio-compositions-to-s3-bucket.php
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,84 @@
    <?php
    //
    // You'll need to install the following composer packages:
    // "twilio/sdk": "^6.20",
    // "aws/aws-sdk-php": "~3.0",
    // "guzzlehttp/guzzle": "^7.0"

    ini_set( 'memory_limit', '-1' );
    set_time_limit(0);

    require 'vendor/autoload.php';

    use Aws\S3\Exception\S3Exception;
    use Aws\S3\S3Client;
    use Twilio\Rest\Client;

    class App {

    const TWILIO_BASE_VIDEO_URL = 'https://video.twilio.com';
    const TWILIO_API_KEY = 'X';
    const TWILIO_API_SECRET = 'X';
    const TWILIO_COMPOSITION_MEDIA_PATH = '/v1/Compositions/%CompositionSid%/Media';

    const AWS_ACCESS_KEY_ID = 'X';
    const AWS_SECRET_ACCESS_KEY = 'X';
    const AWS_BUCKET = 'X';
    const AWS_DEFAULT_REGION = 'eu-central-1';

    public function __construct()
    {
    $this->saveCompositionMedia();
    }

    function saveCompositionMedia()
    {
    $compositionSid = $_POST['CompositionSid'];
    $rolePlayId = $_GET['rolePlayId'];

    $client = new Client(self::TWILIO_API_KEY, self::TWILIO_API_SECRET);
    $compositionPath = str_replace("%CompositionSid%", $compositionSid, self::TWILIO_COMPOSITION_MEDIA_PATH);
    $requestUrl = sprintf("%s%s", self::TWILIO_BASE_VIDEO_URL, $compositionPath);
    $response = $client->request('GET', $requestUrl);

    $responseContent = $response->getContent();
    if (!empty($responseContent) && isset($responseContent['redirect_to'])) {
    $redirectUri = $responseContent['redirect_to'];
    $fileContent = file_get_contents($redirectUri);

    $fileUrl = $this->uploadToS3($fileContent);
    return $fileUrl;
    }
    }

    function uploadToS3($fileContent): string {
    $env = $_GET['env'];

    $fileName = 'filename.mp4';
    $filePath = $env . '/' . $fileName;

    $s3 = new S3Client([
    'version' => 'latest',
    'region' => self::AWS_DEFAULT_REGION,
    'credentials' => [
    'key' => self::AWS_ACCESS_KEY_ID,
    'secret' => self::AWS_SECRET_ACCESS_KEY,
    ]
    ]);

    try {
    // Upload data.
    $result = $s3->putObject([
    'Bucket' => self::AWS_BUCKET,
    'Key' => $filePath,
    'Body' => $fileContent,
    ]);

    return $result['ObjectURL'] . PHP_EOL;
    } catch (S3Exception $e) {
    return $e->getMessage() . PHP_EOL;
    }
    }
    }

    new App();