Menu

Step by Step Guide: Upload files to AWS S3 Bucket using Laravel

We all know, AWS (Amazon Web Services) is leading the web hosting market nowadays and AWS is providing such awesome services for a very cheap price. S3 is a service provided by AWS for storage media files for a cheaper price. You can get s3 storage as cheap as 40gb per dollar. We're storing files on s3 with Laravel Framework. Laravel give us feature to configure any disk for storage. By default, the cloud storage for Laravel is S3, you can check this thing in your project's config/filesystem.php file. We're dividing the whole process into 3 parts.

  1. Creating AWS S3 Bucket
  2. Setting up Laravel environment for S3 storage
  3. Uploading files via Laravel

Let's dive straight into the process.

Creating AWS S3 Bucket

  1. Signup with AWS account to use any AWS service.
  2. Click on Services in the header and click S3
  3. Click on Create Bucket
  4. Enter bucket name and other details according to your permission and region preferences.
  5. Finally, submit the 4-step form by click on Create Bucket Button.
  6. Now you'll be able to see the bucket name which you've entered while creating the bucket. 
  7. By clicking on the bucket name, you can add many files as you want in the bucket directly. But we're creating a system through Laravel app. So, Let's setup the environment in your Laravel project for S3.

Setting up Laravel Environment

  • To know about all the parameters you need in your .env file. You can check out your config/filesystems.php file. where you can see something like this
's3' => [
   'driver' => 's3',
   'key' => env('AWS_ACCESS_KEY_ID'),
   'secret' => env('AWS_SECRET_ACCESS_KEY'),
   'region' => env('AWS_DEFAULT_REGION'),
   'bucket' => env('AWS_BUCKET'),
   'http' => [
    'verify' => false
   ],
  ], 
  • Now, we have to enter all these values in .env file. Scroll down to the last and add these
AWS_ACCESS_KEY_ID= 
AWS_SECRET_ACCESS_KEY= 
AWS_BUCKET= 
AWS_DEFAULT_REGION= 
AWS_URL= 
  • Let's find out AWS_ACCESS_KEY_ID and AWS_SECRET_ACCESS_KEY
  • In order to get the all the values, we need to create one user for S3, otherwise anyone having the URL of the S3 disk can access our content.
  • To create the user, click on your name in header and then click on My Security credentials.
  • Then click on the Get started with the IAM User and provide appropriate username and access type according to your needs. In our case, we need programmatic access
  • Click on Next:Permission
  • To get the policy list. Click on the Attach Existing Policies directly and type S3 in filter policy search bar. You'll see 4 kind of S3 policy.
  • We want full access, so we have to select AmazonS3FullAccess
  • Click on next and Create User
  • You'll get the Access Key ID and Secret Access Key with a success message
  • Just enter these two values in your project's .env file.
AWS_ACCESS_KEY_ID= xxxx-YOUR_ACCESS_KEY_ID-xxxxxx
AWS_SECRET_ACCESS_KEY= xxxx-YOUR_SECRET_ACCESS_KEY-xxxxxx
AWS_BUCKET= 
AWS_DEFAULT_REGION= 
AWS_URL= 
  • AWS_BUCKET is your bucket name which you entered while creating the bucket or You can get the name in your S3 dashboard
  • AWS_DEFAULT_REGION is your selected region while creating the bucket. You can see the region name in your S3 dashboard, for me it is Asia Pacific (Mumbai) But to enter in .env file, we have to find it's value
  • Go to https://docs.aws.amazon.com/general/latest/gr/rande.html#s3_region
  • And find your area and copy it's region column value.
  • For me it's ap-south-1.
  • We got 4 out of 5 values for .env file. Now we just need AWS_URL. You can create your AWS_URL using this template URL
  • https://your-bukcet-name.your-region.amazonaws.com
  • For me, the URL became: https://demo-bucket.ap-south-1.amazonaws.com
  • Enter this in your .env AWS_URL
AWS_ACCESS_KEY_ID= xxxx-YOUR_ACCESS_KEY_ID-xxxxxx
AWS_SECRET_ACCESS_KEY= xxxx-YOUR_SECRET_ACCESS_KEY-xxxxxx
AWS_BUCKET= demo-bucket
AWS_DEFAULT_REGION= ap-south-1
AWS_URL= https://demo-bucket.ap-south-1.amazonaws.com
  • The last thing in environment setup is to install one composer package to let Laravel know that we're using S3 filesystem. Just run this command in your shell
composer require league/flysystem-aws-s3-v3 ~1.0
  • Now, restart the server as we have made changes in the environment file.

Uploading files to S3 Bucket

  • Create an HTML form first to upload the file with simple one file input and submit button.
<form action="upload-file" mehtod="post" enctype="multipart/form-data">
 <input type="file" name="file">
 <input type="submit" name="submit">
</form>
  • Now we need two routes. One get route to show this form and one post route to upload the file. I am using closure function in routes itself. You can use controller also.
Route::get('/upload-file', function(){
	return view('upload-file');
});
  • To Upload file to S3, we have store method in Laravel, which will take two parameters
  • Folder name (it will create one inside your bucket if doesn't exist.
  • And disk name. i.e., s3 in our case
  • Create one post route
Route::post('/upload-file', function(){
  request()->file('file')->store('new-folder', 's3');
});
  • Above post URL will create a new-folder in your S3 Bucket if doesn't exist.
  • Now just hit the get URL we just created, using any browser and try uploading any file.
  • Check your S3 Bucket.
  • Tadaa! We're done.

I'll show you how to retrieve this data from S3 in next tutorial. Happy coding till then. Drop your queries in comments below.

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

0 Comments