Laravel Storage: saving files on AWS S3

Thiago Alves • 04/25/2020

A few days ago I demonstrated in a simple way how to upload files using Laravel's Storage class.

I am going to use the same code in order to show how to save files in AWS S3, Amazon's storage service.

Follow the repository link: thiagoalves-dev/laravel-storage-example.

Assuming you have already created an AWS account, let's follow the steps below:

Bucket

In Services > Storage > S3, create a new bucket to store your application's files.

Credentials

In your AWS user menu, go to My Security Credentials, create a new Access Key and copy the keys that are generated.

Setup

Laravel already comes with the S3 configuration variables by default in the .env file, just add your account and bucket data.

# .env

AWS_ACCESS_KEY_ID=xxxx
AWS_SECRET_ACCESS_KEY=xxxxxx
AWS_DEFAULT_REGION=us-east-2 # exemplo
AWS_BUCKET=bucket-name
// config/filesystems.php

'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' => env('AWS_URL'),
    'endpoint' => env('AWS_ENDPOINT'),
]

S3 Package

You need to install a package in order for the integration with the S3 service to work correctly.

No configuration or coding are needed at this step, just execute the following command in your terminal: composer require league/flysystem-aws-s3-v3.

You're all set! Let's start coding.

Upload

I added a new route for the method where I will implement the upload the files by integrating with the S3 service.

// routes/web.php

Route::post('/storeS3', 'HomeController@storeS3')->name('home.storeS3');

The second parameter of the store method is the disk where we will save the file. We pass s3 into it and Laravel does the rest on its own.

// HomeController

public function storeS3(Request $request)
{
    $request->file('profile_image')->store('/', 's3');
}

On the Bucket page, you have access to the uploaded file.

AWS S3 Bucket

Preview

There are several ways to access files on S3: through download, temporary addresses, etc. This also varies depending on the access permissions that are configured on the bucket.

Below, I share an example of a route where I pass the file name that I want to access and, in the controller, I redirect it to a temporary address, then it is possible to see the image I uploaded.

// routes/web.php

Route::get('/show-s3/{filename}', 'HomeController@showS3')->name('home.showS3');
// HomeController

public function showS3(string $filename)
{
    return redirect(
        Storage::disk('s3')
            ->temporaryUrl($filename, now()->addMinutes(5))
    );
}

Result:

Visualização da imagem

See you later!

Thiago Alves

Thiago Alves

Software Engineer, in the software development market since 2011. Specialist in PHP, Laravel and Vue.js.

Share your thoughts about this post in the comments below, in case you have any questions or would like to suggest a topic.