11

I was researching and read a lot of articles on one specific topic but couldn't find relevant answers anywhere.

However I'm doing some things as a fun/private project in Laravel and I wanted to use Amazon S3 storage. I have everything set up, and it's working, I'm able to upload images to S3 from my application using Laravel Filesystem, but what I'm trying to do is use CloudFront (I have this set up and working too, the cloudfront is serving files properly) for serving images and files.

Now my question is: How to integrate AWS CloudFront "urls" into Laravel? I mean how to store CloudFront urls inside a Laravel application? How do I retrieve file urls (ie images) so I can display it inside my application. Do I store just file paths in a database and use "some specific way" to serve the full url or do I save the full AWS CloudFront urls in a database?

How does this works at all? Sorry if the question is stupid, but I just dived into Laravel and I'm pretty new there.

LeCodex
  • 1,636
  • 14
  • 20
bernadd
  • 660
  • 1
  • 10
  • 19

2 Answers2

16

Set url to your cloudfront url in config/filesystems/s3

's3' => [
        'driver' => 's3',
        'key' => env('AWS_KEY'),
        'secret' => env('AWS_SECRET'),
        'region' => env('AWS_REGION'),
        'bucket' => env('AWS_BUCKET'),
        'url' => 'http://xxx.cloudfront.net']

It works in L5.5

Azole
  • 256
  • 3
  • 5
  • 1
    I setted 'url' but it still return url of S3, not url of CloudFront. Iam using L5.5. – Quy Nguyen Vu Dec 28 '17 at 23:54
  • Using L5.6. It does seem this solution should work according to `Illuminate\Filesystem\FilesystemAdapter.php` in `getAwsUrl()`, but the config doesn't seem to be taking. Is there something happening with the cache system maybe? What else could it be? Adding `dd()` statements into getAwsUrl() for debugging don't even get hit. – dhildreth Sep 14 '18 at 17:04
  • Ah, okay, you need to use `Storage::url('file.jpg')`. This will reflect the `url` setting. Thanks! – dhildreth Sep 14 '18 at 17:46
3

You can use this package https://github.com/publiux/laravelcdn

Content Delivery Network Package for Laravel

The package provides the developer the ability to upload his assets (or any public file) to a CDN with a single artisan command. And then it allows him to switch between the local and the online version of the files.

Joseph
  • 2,737
  • 1
  • 30
  • 56