
How to get images from AWS s3 and display in Laravel blade
You can get images from AWS s3 and display them in the Laravel blade. You have to pass the object key (image path) with s3 bucket name to getCommand method and create the presigned URL to get the image and return the presigned URL to Laravel blade to display the image.
Answers 1
-
Get image from AWS s3 using presigned url and display in Laravel blade
//routes\web.php use Illuminate\Support\Facades\Storage; Route::get('/get-s3-document', function(){ //For example your path to file or object key ($key) will look like the following. //You can get it dynamically by storing the key to the database table while uploading document in AWS s3. $key = 'project/documents/file.jpg'; $client = Storage::disk('s3')->getDriver()->getAdapter()->getClient(); $bucket = env('AWS_BUCKET'); $command = $client->getCommand('GetObject', [ 'Bucket' => $bucket, 'Key' => $key ]); $request = $client->createPresignedRequest($command, '+20 minutes'); $presignedUrl = (string)$request->getUri(); return view('welcome')->with('document_url', $presignedUrl); }); //resources\views\welcome.blade.php <embed src="{{ $document_url }}">
0You can get the key of the object after clicking on the object inside the folder in the AWS s3 console dashboard. You can also store the key of the object in the table and get the key to pass to getCommand method to get the image from AWS s3.
Additional Notes:
Import Storage Facades
use Illuminate\Support\Facades\Storage;
//.env AWS_ACCESS_KEY_ID=XXXXXXXXXXXXXXXXXXXX AWS_SECRET_ACCESS_KEY=XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX AWS_DEFAULT_REGION=XX-XXXXXXXXX-X AWS_BUCKET=XXXXXXXXXXXX
Random Code Snippet Queries: Laravel
- Define variable and use in Laravel controller method
- Page loader in laravel
- How to pass data to route in laravel?
- How to call Laravel route in jQuery
- How to get all route list
- How to display validation error in laravel
- How to check column value of a record is null or not in laravel
- Laravel form request validation
- Symlink(): No such file or directory
- How to add a key value pair to existing array in laravel
- Laravel delete all rows older than 30 days
- Retrieve count of nested relationship data in Laravel
- Multiple Level eager loading in Laravel
- How to restore deleted records in laravel
- How to get CSRF token in laravel controller
- Credit card validation in laravel
- SQLSTATE[HY000]: General error: 1215 Cannot add foreign key constraint
- How to upload image in laravel 8
- Laravel change date format
- How to add active class to menu item in laravel
- Create records using relationship in laravel
- How to send ID to another page in Laravel
- Laravel migration add foreign key to existing table
- Laravel upload file with original file name
- Array to string conversion laravel blade