Upload File to AWS S3 in PHP

Amazon Simple Storage Service (Amazon S3) is an object storage service that offers industry-leading scalability, data availability, security, and performance. This means customers of all sizes and industries can use it to store and protect any amount of data for a range of use cases, such as websites, mobile applications, backup and restore, archive, enterprise applications, IoT devices, and big data analytics.

This tutorial will show you how to upload files to aws S3 using PHP and the aws-s3 PHP SDK library.

This tutorial will provide you with a complete guide on how to create an aws s3 bucket account and how to upload files into the aws S3 bucket using PHP and the aws s3 SDK library.

How to Upload Files to AWS S3 in PHP

1. Create S3 Bucket in AWS Account

I assume you already have an AWS account, then you need to create an S3 Bucket.

  • Search for S3 services after you login to your AWS account.

  • Click the create bucket button

  • In the General Configuration section, fill in the bucket name and S3 location, I chose Singapore

  • in the object ownership section, select ACL Enabled so that files can be accessed publicly

  • Uncheck the Block all public access section so that files can be accessed publicly

  • The rest of the parts follow the default configuration from AWS, If you have clicked the create bucket button
  • You will see a notification of successfully creating S3 bucket

2. Create New Project

If you are using xampp, create a folder in the xampp/htdocs directory. And create a new folder with the name file-upload-s3.

3. Install AWS SDK for PHP

Execute the following command on the terminal to install AWS SDK for PHP:

composer require aws/aws-sdk-php

4. Create Form Upload

Create an index.html file which contains the HTML code for the form upload file, add the following code into it:

5. Create File upload.php

Create an upload.php file inside the file-upload-s3 folder and add the code below into your upload.php file. The following PHP code uploads a file from the web server into S3:

Conclusion

Uploading files to S3 in PHP is very easy, in this tutorial, you have learned how to upload files to AWS S3 bucket using PHP and the AWS PHP SDK library.

Next Post Previous Post
No Comment
Add Comment
comment url