manifestor
manifestor

Reputation: 1442

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:

    '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

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

Answers (1)

Devon Bessemer
Devon Bessemer

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

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:

Upvotes: 2

Related Questions