Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Laravel and AWS Cloudfront

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.

like image 697
bernadd Avatar asked Sep 08 '16 13:09

bernadd


People also ask

What is AWS CloudFront used for?

CloudFront speeds up the distribution of your content by routing each user request through the AWS backbone network to the edge location that can best serve your content. Typically, this is a CloudFront edge server that provides the fastest delivery to the viewer.

Is CloudFront and CDN same?

Amazon CloudFront is a content delivery network (CDN) service built for high performance, security, and developer convenience.

What is the difference between S3 and CloudFront?

Amazon CloudFront works with S3 but copies files from S3 to the outer "edge" of Amazon's servers, allowing for fast retrieval. My tests show that it retrieves files in about half the time of S3. There's a slight increase in price from Amazon S3, but not much.

Is CloudFront a push or pull CDN?

Amazon CloudFront Explained Instead, CloudFront will automatically “pull” the data from your origin server onto CloudFront's network of servers around the world. It's also possible to “push” your content on to CloudFront – more on this later.


2 Answers

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

like image 52
Azole Avatar answered Oct 13 '22 05:10

Azole


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.

like image 42
Joseph Avatar answered Oct 13 '22 05:10

Joseph