Reputation: 1442
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:
'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') }}" />
. This one works, but is it the right approach? The problem here is, that if I change the FILESYSTEM_DRIVER
back to local I can't reference the resources in my DOCROOT/public/img
folder like I did earlier with {{ asset('img/icons/time.png') }}
, so I'm loosing flexibility - maybe I need to copy the assets to DOCROOT/storage/app/public/
which used by the local
driver?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.
Upvotes: 0
Views: 3234
Reputation: 35337
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.
https://laravel.com/docs/5.6/filesystem#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
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:
Upvotes: 2