Laravel use S3/CloudFront resources in Blade
Clash Royale CLAN TAG#URR8PPP
Laravel use S3/CloudFront resources in Blade
I'm a bit confused about what the best approach is in order to access an image, which is uploaded to S3 and delivered via CloudFront and show it in a view (using the CloudFront URL). I use Laravel 5.5
and I deposited the CDN URL already to my S3 configuration:
Laravel 5.5
's3' => [
'driver' => 's3',
'key' => env('AWS_ACCESS_KEY_ID'),
'secret' => env('AWS_SECRET_ACCESS_KEY'),
'region' => env('AWS_DEFAULT_REGION'),
'bucket' => env('AWS_BUCKET'),
'url' => 'https://xxxxxx.cloudfront.net/',
],
The following possibilities work
<img src=" Storage::url('assets/img/image.png') " />
FILESYSTEM_DRIVER
DOCROOT/public/img
asset('img/icons/time.png')
DOCROOT/storage/app/public/
local
I'm integrating CloudFront the first time to a Laravel app, so could someone who did that before tell me what the right approach is? Thank you very much.
1 Answer
1
This is a good approach. But when using the local filesystem driver, you would use the public/storage/assets/img
directory, not the public/img
directory to make it equivalent.
public/storage/assets/img
public/img
https://laravel.com/docs/5.6/filesystem#the-public-disk
The Public Disk
The public disk is intended for files that are going to be publicly accessible. By default, the public disk uses the local driver and stores these files in storage/app/public. To make them accessible from the web, you should create a symbolic link from public/storage to storage/app/public. This convention will keep your publicly accessible files in one directory that can be easily shared across deployments when using zero down-time deployment systems like Envoyer.
To create the symbolic link, you may use the storage:link Artisan command:
php artisan storage:link
File URLs
You may use the url method to get the URL for the given file. If you are using the local driver, this will typically just prepend /storage to the given path and return a relative URL to the file. If you are using the s3 or rackspace driver, the fully qualified remote URL will be returned:
php artisan storage:link
Ah now I saw this one: stackoverflow.com/a/1507967/5199242 - so it doesn't matter if the link is absolute, a
git pull
will result in a directory, not a link.– chevallier
Aug 7 at 20:37
git pull
Yeah,
/public/storage
should be in your gitignore file anyways.– Devon
Aug 7 at 20:42
/public/storage
By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.
Thank you, Devon. There just one thing I noticed I would do different: The
php artisan storage:link
creates an absolute link, which is not good if you're developing local and then pushing to a remote repository. That's why I created the same link manually, but as a relative one.– chevallier
Aug 7 at 20:33