Friday, September 2, 2016

Get video url using bucket path of aws s3 in laravel 5.2

Get video url using bucket path of aws s3 in laravel 5.2

I have bucket path of AWS S3 video and I want to get a streaming video url from  AWS CloudFront.

For this, Firstly I have to install AWS SDK in my project.

In composer.json , put it

"aws/aws-sdk-php-laravel": "~3.0"

and run composer update.

In app.php file write this in providers

Aws\Laravel\AwsServiceProvider::class,

and in aliases

'AWS' => Aws\Laravel\AwsFacade::class,

Now goto your controller and include this

use Aws\CloudFront\CloudFrontClient;

Now create a method like this

public static function getVideoUrl($video){ 
 try { 
      $cloudFront = CloudFrontClient::factory([ 
      'region' => 'us-east-1', 
      'version' => 'latest' 
      ]); 
      $expiry = new \DateTime('+5256000 minutes'); 
      return $cloudFront->getSignedUrl([ 
     'url' => env('AWS_CLOUDFRONT_URL') . "/$video", 
     'expires' => $expiry->getTimestamp(), 
     'private_key' => public_path() . '/' . env('AWS_PRIVATE_KEY'), 
     'key_pair_id' => env('AWS_KEY_PAIR_ID') 
     ]); 
   } catch (Exception $e) { 
   return 'false'; 
   }}


Here $video is bucket video name, and in env variables these are CloudFront url,
private key, aws key pair id which you get from aws and write in your .env file.

Thanks

No comments:

Post a Comment