Menu

Retrieve files from AWS S3 Bucket using Laravel Framework

This blog is the next part of Step by Step Guide: Upload files to AWS S3 Bucket using Laravel. We've created a S3 bucket and store files in the S3 bucket using Laravel. Now we're trying to retrieve those files from S3 storage.

Whenever we upload any file, Laravel provide a temporary name to a file and we need that name. It's a very simple process. We just have to store file name in the database and to access that file in browser. We Just have to append that file name in the S3 URL. Ain't is simple? Let's do it.

We're starting this article from the last step of this tutorial

Route::post('/upload-file', function(){
   request()->file('file')->store('new-folder', 's3');
});

Now, we are saving this path in the database. Make a table with any preferred name. Let's say videos having path column.

Replace the last step with this:

Route::post('/upload-file', function(){
   $path = request()->file('file')->store('new-folder', 's3');
});

And save this in the database

Route::post('/upload-file', function(){
   $path = request()->file('file')->store('new-folder', 's3');

   $video = new Video;
	   $video->path = $path;
   $video->save();
});

Now open your S3 bucket and click on your uploaded file and check the Key and Object URL. Key is your stored path in the database and object URL is your complete path you want. As we made a demo-bucket in last tutorial. I got this URL from the object URL: https://s3.ap-south-1.amazonaws.com/demo-bucket/

And append your stored path with it, let's say: new-folder/ygD6K2L27vOzGo5XbvU36PTeSaSluL2rmX4ZY25q.mp4.

So, the complete path you'll get is

https://s3.ap-south-1.amazonaws.com/demo-bucket/new-folder/ygD6K2L27vOzGo5XbvU36PTeSaSluL2rmX4ZY25q.mp4

Just use this URL to access your content. If you are getting some kinda code while accessing the things. Check your S3 Bucket permissions.

Drop your queries in the comment section below.


Posted In:
AWS
Do share till i am pulling them away.😂

1 Comment

  1. El-Angelo Ferrer
    February 27, 2020

    The image does not appear in the blade. Using this code "https://*****.s3.us-east-1.amazonaws.com/{{$car->image_path}}"