How to Upload File to AWS S3 Bucket Laravel

In this article, we will discuss “How to Upload File to AWS S3 Bucket Laravel”. It’s known as Amazon Simple Storage Service and an object storage service that offers scalability, security, performance, and data availability. We can use AWS S3 Bucket to store and retrieve any amount of data at any time. Data is stored as objects in S3 Buckets.

You have to check the pricing of the AWS S3 bucket. Also, AWS provides us the S3 bucket 5GB standard storage under a free tire for 12 months.

Prerequisites

  1. AWS Account
  2. AWS Management Console Access
  3. Basic knowledge of AWS, If you are new to AWS then read service pricing properly. Otherwise, AWS charges your card when the free tier limit is crossed.
  4. IAM User (Check my previous post)

Create AWS S3 Bucket

Login with the AWS Management Console and go to the S3 bucket dashboard.

Click on the Create bucket button, then you are redirected to the next step. Here, you have to add the Bucket name.

You can apply encryption, versioning, or advanced settings as per your requirements. In this example, I’m using the default settings. Our bucket is ready for further use.

Create User or Add Permissions to Existing User

Check my previous article “How to Create AWS IAM User with Programmatic Access”. Here, you learn about creating the IAM User and attaching the permission.

For AWS S3 Bucket, we have to add “AmazonS3FullAcess” permission to our user. So go to the IAM dashboard, then click on the “Add permission” button. After that, you are redirected to the Grant permissions page. Now, select “Attach existing policies directly” and in the search box write S3, allow “AmazonS3FullAcess” and click “Next: Review”.

On the permission review screen, you got the details of the attached permission. Now, click on the “Add permissions” button.

Now, you can use your AWS User Key and Secret with your SDK kit.


Create Laravel Project

Use the following composer command to install the Laravel project.

composer create-project laravel/laravel laravel-project --prefer-dist

Also, check back our previous post “How to install Laravel 5 with Xampp using Composer“.

Install Laravel AWS S3 Dependencies

Run the following composer command on the terminal. This will install the required dependencies, click here for more details.

composer require --with-all-dependencies league/flysystem-aws-s3-v3 "^1.0"

Update AWS S3 Configurations

Open the “.env” file and update the AWS configurations.

AWS_ACCESS_KEY_ID=********************************
AWS_SECRET_ACCESS_KEY=********************************
AWS_DEFAULT_REGION=us-east-1
AWS_BUCKET=*********************
AWS_USE_PATH_STYLE_ENDPOINT=false

Upload DB Backup File

You have to check my previous article “How to Create Database Backup in Laravel”. Here, we create a custom Artisan command to generate the DB backup and run this command as a daily CRON task.

Open the command file “DatabaseBackup.php” located at “app\Console\Commands”. And, update the following code snippet with the handle() function.

public function handle()
{
    $filename = "backup-" . Carbon::now()->format('Y-m-d') . ".sql";

    // Create backup folder and set permission if not exist.
    $storageAt = storage_path() . "/app/backup/";
    if(!File::exists($storageAt)) {
        File::makeDirectory($storageAt, 0755, true, true);
    }

    $command = "".env('DB_DUMP_PATH', 'mysqldump')." --user=" . env('DB_USERNAME') ." --password=" . env('DB_PASSWORD') . " --host=" . env('DB_HOST') . " " . env('DB_DATABASE') . "  | gzip > " . $storageAt . $filename;

    $returnVar = NULL;
    $output = NULL;
    exec($command, $output, $returnVar);

    // Upload backup file to AWS S3 Bucket 
    $backupFilePath = $storageAt . $filename;
    if(File::exists($backupFilePath)) {
        $path = Storage::disk('s3')->put($filename, $backupFilePath);
        $path = Storage::disk('s3')->url($path); // Get the bucket url for uploaded DB backup file.
        echo $path; // Save to Database for backup log or etc. And, delete the local file from storage.
    }
}

Whenever the command executes, our DB backup file is uploaded to the AWS S3 bucket.

Also, we can store the bucket uploaded path into the DB for further use. For example, I’m creating an uploaded file path. If required, then we can save the in-log table or anything else. I tried the same on my local machine and received the path at the end of the execution.

Conclusion

In this article, we are discussing “How to Upload File to AWS S3 Bucket Laravel”. I’m trying to explain to you how AWS S3 Bucket is used to store DB backups. Hope you like this article, and sure you learn a lot. Feel free to add comments if any queries or you can send your suggestions.

Keep learning and stay safe 🙂

If you like our content, please consider buying us a coffee.
Thank you for your support!
Buy Me a Coffee

AWS IAMAWS S3 BucketLaravelLaravel Code Snippet
Comments (0)
Add Comment