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 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...

How to use trackBy in Angular with Example

In this article, we will discuss "How to use trackBy in Angular" . Basically, " trackBy " is used to improve the performance of an angular application. Today, I will try to explain the use of trackBy with an example. Why do we need trackBy in Angular? By default, no need to use trackBy in Angular. But with large collections of data, angular ngFor directive may perform poorly. For example, a small change of data such as adding a new record, editing, or deleting a record from the collection. The angular framework will remove all the DOM elements that are associated with the data and will create them again in the DOM tree even if the same data is coming. Here, a lot of DOM manipulation will happen in the background if a large amount of data comes from the API then the application performance will suffer. Angular trackBy example Angular provides us function trackBy which helps us to track the items which have been added or deleted. The trackBy function takes two argum...