How to Integrate Amazon s3 bucket in PHP: Laravel

How to Integrate Amazon s3 bucket in PHP: Laravel

tudip-logo

Tudip

15 July 2019

To integrate S3 bucket with Laravel application we will go through some basic understanding about S3 bucket and Laravel.

Amazon S3 bucket

It is public cloud storage. Amazon provided this resource called Simple Storage Service.

Why we need Amazon S3 bucket service?

In many applications, we need to store some important files (like – PDF, PNG etc) that we are going to use in our application.

So for that purpose many developers store these files on the local storage. In this case the server disk space will be utilized and that is not good practice.
Instead of storing files on local we can store these types of files in another location, and that is the Amazon S3 bucket.

Laravel provides an easy way to integrate S3 bucket in your application, as Laravel default configuration to use S3 bucket.

The following Laravel library will help to access the Amazon S3 bucket.

league/flysystem-aws-s3-v3

Before we move forward we should have knowledge about Composer.

Composer

It is a package manager on application-level for PHP. We can also say that it is the dependency management tool.
To use this, we must have installed a Composer.

Following command will install all dependencies present into your composer.json file (This file is present in your Laravel application).

composer install

To add an Amazon S3 bucket library into your Laravel application.

composer require league/flysystem-aws-s3-v3

This command will add the latest version of that library into your composer.json file.
composer.json file will have all the information about libraries into your application. So you will get this name “league/flysystem-aws-s3-v3” under “require” field with the latest version of it.

As I said in the Composer section, now you have to run “composer install” command in terminal under your application directory.

So till now, we have installed library into our application, you can check this by checking the directory name under the vendor directory.

The vendor directory will create after “composer install” command and it will contain all the libraries and their important classes.

As I said that Laravel has a default configuration for S3 bucket. We just have to gather the following information and put it in the .env file.

Make sure your Laravel application has a .env file.

  • FILESYSTEM_DRIVER=s3
  • AWS_ACCESS_KEY_ID=
  • AWS_SECRET_ACCESS_KEY=
  • AWS_DEFAULT_REGION=
  • AWS_BUCKET=

Our configuration part at Laravel application is over.

Now to put our files into S3 bucket we have created the S3 bucket, right?

For that got to the following link:

https://s3.console.aws.amazon.com/s3/home?region=us-east-2#

Here are some steps to create a new bucket

  • You have to login with AWS account credentials.
  • Click on Create Bucket.
  • Add Bucket Name (Name should be unique so nobody can use this bucket name in the entire Amazon S3 network).
  • Click on Next button.
  • Set Priorities (You can select both read and write permission for S3 bucket object).

We have created a bucket successfully. Now we need to create a bucket policy

For that go to the following link:

https://awspolicygen.s3.amazonaws.com/policygen.html

While creating policy:

  • Select the type of Policy as = S3 Bucket Policy.
  • In the Add Statement section, you have to select the actions which you are going to perform on that bucket. Like DeleteObject, GetObject, and PutObject.
  • To add the ARN (Amazon Resource Name) we must follow the following format. You can add multiple ARN
    arn:aws:s3:::/
  • Click on Add statement and this will generate policy JSON on the same page.

You have to put this generated policy JSON in Bucket Policy Tab. To update JSON, go to this link

https://s3.console.aws.amazon.com/s3/buckets/laravels3demo/?region=us-east-2&tab=permissions

To get Access Key Id and Secret Access Key go to the following link

https://console.aws.amazon.com/iam/home?region=us-east-2#/security_credential

Now we have created the S3 bucket along with its policy. You got details to put into the .env file.

Let’s see how we can use this library to put and get files into an S3 bucket.

Till now, we have an empty bucket. We also can create folders inside the bucket.

To store file inside a particular folder.

Storage::disk('s3')->put('/pdf/filename', file_get_contents($file));

So this statement will store $file (actual file) into S3 bucket under “pdf” folder along with the filename whatever want.

To get a file from S3 bucket.

Storage::disk('s3')->files('pdf');

This statement will fetch all files present under pdf folder.

To delete a particular file.

Storage::disk('s3')->delete('pdf/filename');

This statement will delete that particular file from pdf folder.

That’s it. We have integrated S3 bucket into our Laravel application.

search
Blog Categories
Request a quote