PHP code example of haridarshan / laravel-url-signer-cloudfront
1. Go to this page and download the library: Download haridarshan/laravel-url-signer-cloudfront library . Choose the download type require .
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
haridarshan / laravel-url-signer-cloudfront example snippets
composer
php artisan vendor:publish --provider="Haridarshan\Laravel\UrlSigner\AwsCloudFront\CloudFrontServiceProvider"
return [
'credentials' => [
'key' => env('AWS_ACCESS_KEY_ID', ''),
'secret' => env('AWS_SECRET_ACCESS_KEY', ''),
],
'region' => env('AWS_REGION', 'us-east-1'),
'version' => 'latest',
// You can override settings for specific services
'Ses' => [
'region' => 'us-east-1',
],
];
return [
'default_expiration_time_in_seconds' => 60 * 60 * 24,
'private_key_path' => get_base_path(env('CLOUDFRONT_PRIVATE_KEY_PATH', '')),
'key_pair_id' => env('CLOUDFRONT_KEY_PAIR_ID', ''),
];
$url = config('filesystems.disks.s3.url') . '/example.mp4';
// Signed CloudFront URL with 1 day expiry
echo \Haridarshan\Laravel\UrlSigner\AwsCloudFront\Facades\CloudFrontFacade::signedUrl($url);
$url = config('filesystems.disks.s3.url') . '/example.mp4';
$expiry = 60 * 60; // Optional in seconds (Default: 1 day)
// Signed CloudFront URL with 1 hour expiry
echo \Haridarshan\Laravel\UrlSigner\AwsCloudFront\Facades\CloudFrontFacade::signedUrl(
$url,
$expiry
);
$url = config('filesystems.disks.s3.url') . '/example.mp4';
$policy = <<<POLICY
{
"Statement": [
{
"Resource": "{$url}",
"Condition": {
"IpAddress": {"AWS:SourceIp": "{$_SERVER['REMOTE_ADDR']}/32"},
"DateLessThan": {"AWS:EpochTime": 3600}
}
}
]
}
POLICY;
// Signed CloudFront URL with custom policy
echo \Haridarshan\Laravel\UrlSigner\AwsCloudFront\Facades\CloudFrontFacade::signedUrl(
$url,
null,
$policy
);
$url = config('filesystems.disks.s3.url') . '/example.mp4';
// CloudFront Signed Cookies with 1 day expiry
result = \Haridarshan\Laravel\UrlSigner\AwsCloudFront\Facades\CloudFrontFacade::signedCookie($url);
/* If successful, returns something like:
CloudFront-Expires = 1589926678
CloudFront-Signature = Lv1DyC2q...2HPXaQ__
CloudFront-Key-Pair-Id = AAPKAJIKZATYYYEXAMPLE
*/
foreach($result as $key => $value) {
echo $key . ' = ' . $value . "\n";
}
$url = config('filesystems.disks.s3.url') . '/example.mp4';
$policy = <<<POLICY
{
"Statement": [
{
"Resource": "{$url}",
"Condition": {
"IpAddress": {"AWS:SourceIp": "{$_SERVER['REMOTE_ADDR']}/32"},
"DateLessThan": {"AWS:EpochTime": 3600}
}
}
]
}
POLICY;
// CloudFront Signed Cookies with custom policy
$result = \Haridarshan\Laravel\UrlSigner\AwsCloudFront\Facades\CloudFrontFacade::signedUrl(
null,
null,
$policy
);
$url = "https://example.cloudfront.net/test.mp4";
Dotenv::create(
Env::getRepository(),
get_base_path(),
'.env'
)->safeLoad();
$cloudfront = new \Haridarshan\Laravel\UrlSigner\AwsCloudFront\CloudFront(
(new Sdk([
'credentials' => [
'key' => env('AWS_ACCESS_KEY_ID', ''),
'secret' => env('AWS_SECRET_ACCESS_KEY', ''),
],
'region' => env('AWS_REGION', ''),
'version' => 'latest',
]))->createClient('cloudfront'),
[
'key_pair_id' => env('CLOUDFRONT_KEY_PAIR_ID', ''),
'private_key_path' => env('CLOUDFRONT_PRIVATE_KEY_PATH', '')
]
);
$signedUrl = $cloudfront->signedUrl($url);