PHP How To Upload File To AWS S3 Bucket Tutorial

Share this Article
Reading Time: 5 minutes
2,235 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.

Buy Me a Coffee

Online Web Tutor invites you to try Skillshare free for 1 month! Learn CakePHP 4, Laravel APIs Development, CodeIgniter 4, Node Js, etc into a depth level. Master the Coding Skills to Become an Expert in Web Development. So, Search your favourite course and enroll now. Click here to join.

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.

Was this post helpful?

Learn Web Development Courses Risk Free @ $5 only.
Web Development Courses @ $5