Skip to main content

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') . "

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

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.

Laravel Artisan Command

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 :)

Comments

Popular posts from this blog

Basic Use of Model Factories in Laravel

In this article, we will discuss the basic use of Model Factories in Laravel. Laravel comes with the feature called model factories that are offered to made fake models quickly. It’s used in the database testing and database seeding. Let’s start the discussion on this feature by... Read out the full post at here

How to setup Amazon Kinesis Data Stream with Amazon Pinpoint (Part 3)?

In this article, we will discuss "How to setup Amazon Kinesis Data Stream with Amazon Pinpoint (Part 3)?". This article is the third part of our Amazon Pinpoint Series. For better understanding, I recommend to readout the previous article. How to Setup AWS Pinpoint (Part 1) How to Setup AWS Pinpoint SMS Two-Way Communication (Part 2)? Streaming Amazon Pinpoint events to Kinesis In Amazon Pinpoint, when we send a transactional SMS or email message then an event will occur as per the action performed. In a simple way, Amazon Pinpoint sends information about events to Amazon Kinesis. Which, we read and process as per our requirement. We are talking about the SMS so we read the stream data to fetch the delivery reports of our SMSs. There are two types of streams given by the Amazon Kinesis such as Data Firehose, and Data Streams. Amazon Pinpoint can also stream data to Kinesis Data Streams, which ingests and stores multiple data streams for processing by analytics applications. F...

How to Manage Elastic IP in AWS?

In this article, we will discuss "How to Manage Elastic IP in AWS?" . Here, you will learn the use of Elastic IP addresses and how to assign it to your EC2 Instance. If you are new with EC2 Instance then check out my previous article, "How to setup an EC2 Instance on AWS" . EC2 (Amazon Elastic Compute Cloud) provide us an ability to create, start, stop and terminate the instance at any time. This will creates a challenge with IP addresses, because restarting an instance or replacing a terminated instance with newly created instance, will result in a new IP address. Now the question is "How to reference a machine when the IP is constantly change?" . We can handle this situation with the use of Elastic IP address. We can associate a single Elastic IP address to different Ec2 Instances. You can immediately associate a new Ec2 Instance with the Elastic IP address if the EC2 instance is stopped or terminated. After the back-end EC2 instance changes, our exist...