PHP How To Upload File To AWS S3 Bucket Tutorial

Reading Time: 5 minutes
3,523 Views

Inside this article we will see i.e PHP how to upload file to AWS s3 bucket. There are very few simple steps by which we can upload files and folders into AWS s3 bucket.

AWS provides a composer package for PHP application. By the help of this package we can easily communicate with AWS s3 bucket with few keys and their APIs.

This article will give you the classified information about uploading file into S3 bucket in PHP. If you are wondering to find the solution about uploading files, then this article will help you a lot with AWS S3 bucket.

Learn More –

Let’s get started.

Install AWS PHP SDK

Before installation installation, please make sure these –

  • System should have composer installed
  • Project should have composer.json file

How To generate composer.json file

To generate a composer.json file. Open project into terminal and run this command.

$ composer init

You can provide either the information to generate else you can simply press Enter to pick default value.

So, here we have generate composer.json file with all default values in it.

Next,

Install AWS SDK.

$ composer require aws/aws-sdk-php

You will see like this –

It will create a vendor folder into your project folder.

Next,

Create Application To Upload File

Create a file with any name like upload-file.php.

You need these while working with AWS S3 bucket to upload any files or folders.

  • AWS Access Key
  • AWS Secret Key
  • Bucket Name

Step #1 – AWS S3 Connection

// Instantiate an Amazon S3 client.
$s3Client = new S3Client([
    'version' => 'latest',
    'region'  => 'us-east-1', // Your AWS region
    'credentials' => [
        'key'    => $awsAccessKey,
        'secret' => $awsSecretKey
    ]
]);

Step #2 – Upload a File to Bucket

$result = $s3Client->putObject([
    'Bucket' => $bucket,
    'Key'    => $key,
    'Body'   => fopen($file_Path, 'r'),
    'ACL'    => 'public-read',
]);

Open upload-file.php file and write this complete code into it.

<?php

require 'vendor/autoload.php';

use Aws\S3\S3Client;

$awsAccessKey = 'XXXX';
$awsSecretKey = 'YYYYYYYYYY';
$bucket = 'your-bucket-name';

// File to be upload
$filename = 'sample.txt';

// Instantiate an Amazon S3 client.
$s3Client = new S3Client([
    'version' => 'latest',
    'region'  => 'us-east-1', // Your AWS region
    'credentials' => [
        'key'    => $awsAccessKey,
        'secret' => $awsSecretKey
    ]
]);

// file to upload, file path
$file_Path = __DIR__ . $filename;

$key = basename($file_Path);

try {
    $result = $s3Client->putObject([
        'Bucket' => $bucket,
        'Key'    => $key,
        'Body'   => fopen($file_Path, 'r'),
        'ACL'    => 'public-read',
    ]);

    echo "<pre>";
    print_r($result);
    echo "file uploaded successfully. file path is: " . $result->get('ObjectURL');

} catch (Aws\S3\Exception\S3Exception $e) {

    echo "There was an error uploading the file.\n";
    echo $e->getMessage();
}

Above code will upload sample.txt into your AWS S3 bucket.

Once your file will be uploaded, it will return the response result and also object URL.

We hope this article helped you to learn about PHP How To Upload File To AWS S3 Bucket Tutorial in a very detailed way.

Online Web Tutor invites you to try Skillshike! Learn CakePHP, Laravel, CodeIgniter, Node Js, MySQL, Authentication, RESTful Web Services, etc into a depth level. Master the Coding Skills to Become an Expert in PHP Web Development. So, Search your favourite course and enroll now.

If you liked this article, then please subscribe to our YouTube Channel for PHP & it’s framework, WordPress, Node Js video tutorials. You can also find us on Twitter and Facebook.

Sanjay KumarHello friends, I am Sanjay Kumar a Web Developer by profession. Additionally I'm also a Blogger, Youtuber by Passion. I founded Online Web Tutor and Skillshike platforms. By using these platforms I am sharing the valuable knowledge of Programming, Tips and Tricks, Programming Standards and more what I have with you all. Read more