PHP & AWS S3: Store and Retrieve Data to the Cloud Tutorial

Last Updated on

CraftyTechie is reader-supported. When you buy through links on our site, we may earn an affiliate commission.

I love using S3 to help me scale any PHP application. I recommend switching to S3 to store and retrieve your data to the cloud as one of the first steps as you begin to scale your application. Check out this article as I help walk you through the entire process.

Steps to Store and Retrieve Data to the Cloud in PHP with AWS S3

  1. Create an Amazon Web Service Account (AWS).
  2. Create an S3 Bucket via IaaC, CLI, or PHP
  3. Install AWS SDK for PHP.
  4. Upload your data to S3 using the putObject function from the AWS SDK for PHP.
  5. List all the file contents of your S3 Bucket using the listObjects function from AWS SDK.
  6. Download your data from S3 using the getObject function from the PHP AWS SDK.

Store and Retrieve Data to the Cloud Code Snippet

Here are all the functions accumulated from this article for quick reference.

?php
require 'vendor/autoload.php';
 
use Aws\S3\S3Client;
use Aws\Exception\AwsException;
 
function uploadData($s3Client, $key, $bucket, $src) {
    try {
       
        $result = $s3Client->putObject([
            'Key' => $key,
            'Bucket' => $bucket,
            'SourceFile' => $src,
        ]);
 
        print_r(
            'The object\'s URL is: ' . $result['ObjectURL']);
    } catch (AwsException $e) {
        print_r('Error: ' . $e->getAwsErrorMessage());
    }
}
 
function listObjects($s3Client, $bucket) {
 
    try {
        //Listing all objects
        $objects = $s3Client->listObjects([
            'Bucket' => $bucket,
        ]);
 
        foreach ($objects['Contents'] as $object) {
            echo $object['Key'] . "\n";
        }
    } catch(AwsException $e) {
        print_r('Error: ' . $e->getAwsErrorMessage());
    }
}
 
function getObject($s3Client, $bucket, $key) {
    try {
        //Retrieve object from bucket.
        $object = $s3Client->getObject([
            'Bucket' => $bucket,
            'Key' => $key
        ]);
 
        return $object;
    } catch(AwsException $e) {
        print_r('Error: ' . $e->getAwsErrorMessage());
    }  
}
 
//Creates an S3 client from .aws file
$s3Client = new S3Client([
    'profile' => 'S3-Sudo-User',
    'region' => 'us-east-1',
    'version' => 'latest'
]);
 
$key = 'cat-image-00001';
$bucket = 's3-bucket-sdk';
$src = './images/cat-1.jpg';
 
 
uploadData($s3Client, $key, $bucket, $src);
 
listObjects($s3Client, $bucket);
 
$object = getObject($s3Client, $bucket, $key);
?>

Free AWS Development Guide

Stop running in circles and develop your applications faster and cheaper in AWS. This guide will walk you through ways to maximize AWS to generate real value for your needs. We pick the right services to scale, tighten security and maximize costs.

Download our free guide now and get started with confidence.

Getting Started

The article assumes that you have the following.

  1. Amazon Web Services (AWS) account.
  2. S3 bucket with appropriate bucket policy.
  3. AWS SDK for PHP.

Bucket Policy for Public Access

We will use the following bucket policy for this walkthrough. The policy makes the bucket objects publicly accessible. Feel free to copy if you are trying the examples along.

{
  "Id": "Policy1667595309304",
  "Version": "2012-10-17",
  "Statement": [
    {
      "Sid": "Stmt1667595306377",
      "Action": [
        "s3:GetObject"
      ],
      "Effect": "Allow",
      "Resource": "arn:aws:s3:::s3-bucket-sdk",
      "Principal": "*"
    }
  ]
}

How to Upload Data to AWS S3 Bucket in PHP

  1. Create or select an existing S3 bucket
  2. Instantiate an S3 Client from PHP S3 SDK
  3. Get file data or source location to upload
  4. Create a reusable class function to upload your S3 Data
  5. Select the various config options you need
  6. Run the S3Client putObject function in a try/catch block
  7. Catch and log any errors.
  8. Return and verify the source url
  9. Continue in your processing requirements

AWS S3 SDK for PHP | PutObject Function

The PutObject ( array $params = [] ) adds an object to the S3 bucket. This function takes loads of parameters, but only a few are required. The following example will use these parameters.

ParameterDescription
KeyA key value for an S3 object
BucketS3 bucket name
SourceFileURI of the file to be uploaded

General Guidelines

  • This function returns a success message only if it can upload the complete object. It doesn’t add partial objects.
  • Multiple write requests overwrite the latest upload unless versioning is enabled on the bucket.
  • You can upload objects upto 5GB in size. Use multi-part uploads for larger files.
  • Object ACLs are useful only if the bucket doesn’t enforce bucket owner enforced settings.

Upload Data to S3 Bucket in PHP Using Source File Code Snippet

<?php
require 'vendor/autoload.php';
 
use Aws\S3\S3Client;
use Aws\Exception\AwsException;
 
function uploadData($s3Client, $key, $bucket, $src) {
    try {
       
        $result = $s3Client->putObject([
            'Key' => $key,
            'Bucket' => $bucket,
            'SourceFile' => $src,
        ]);
 
        print_r(
            'The object\'s URL is: ' . $result['ObjectURL']);
    } catch (AwsException $e) {
        print_r('Error: ' . $e->getAwsErrorMessage());
    }
}
 
//Creates an S3 client from .aws file
$s3Client = new S3Client([
    'profile' => 'S3-Sudo-User',
    'region' => 'us-east-1',
    'version' => 'latest'
]);
 
$key = 'cat-image-00001';
$bucket = 's3-bucket-sdk';
$src = './images/cat-1.jpg';
 
 
uploadData($s3Client, $key, $bucket, $src);
?>

PHP S3 Bucket Upload Response

Running the example would return the object’s URI.

The object's URL is: https://s3-bucket-sdk.s3.amazonaws.com/cat-image-00001

Because the bucket policy allows public-read, we can view this resource in the browser.

how to store and retrieve data to cloud php aws
A Cute Cat

How to List Objects from S3 Bucket in PHP

AWS S3 SDK for PHP | ListObjects Function

The ListObjects ( array $params = [] ) returns objects and their associated data in a specified bucket.

General Guidelines

  • The function returns upto 10,000 objects from the bucket. You can use selection criteria to retrieve a subset of objects.
  • A successful response may have a valid or invalid XML. Make sure to parse the content at your end.

List Objects from S3 Bucket Code Snippet

<?php
require 'vendor/autoload.php';
 
use Aws\S3\S3Client;
use Aws\Exception\AwsException;
 
function listObjects($s3Client, $bucket) {
 
    try {
        //Listing all objects
        $objects = $s3Client->listObjects([
            'Bucket' => $bucket,
        ]);
 
        foreach ($objects['Contents'] as $object) {
            echo $object['Key'] . "\n";
        }
    } catch(AwsException $e) {
        print_r('Error: ' . $e->getAwsErrorMessage());
    }
}
 
//Creates an S3 client from .aws file
$s3Client = new S3Client([
    'profile' => 'S3-Sudo-User',
    'region' => 'us-east-1',
    'version' => 'latest'
]);
 
listObjects($s3Client, 's3-bucket-sdk');
?>

PHP S3 Bucket List Objects Response

The last example uploaded a cat image with the key ‘cat-image-0001’, and the current example returns the object’s key.

cat-image-00001

How to Retrieve Data from S3 Bucket in PHP

AWS S3 SDK for PHP | GetObject Function

The GetObject ( array $params = [] ) function retrieves object data from a specified S3 bucket.

General Guidelines

  • The GET request returns the latest version of the object. Pass the VersionID argument to the function if you want a different version.
  • You must restore objects before retrieving them from S3 Glacier tier types. 

Retrieve Data from S3 Bucket in PHP Code Snippet

<?php
require 'vendor/autoload.php';
 
use Aws\S3\S3Client;
use Aws\Exception\AwsException;
 
function getObject($s3Client, $bucket, $key) {
    try {
        //Retrieve object from bucket.
        $object = $s3Client->getObject([
            'Bucket' => $bucket,
            'Key' => $key
        ]);
 
        return $object;
    } catch(AwsException $e) {
        print_r('Error: ' . $e->getAwsErrorMessage());
    }  
}
 
//Creates an S3 client from .aws file
$s3Client = new S3Client([
    'profile' => 'S3-Sudo-User',
    'region' => 'us-east-1',
    'version' => 'latest'
]);
 
 
$key = 'cat-image-00001';
$bucket = 's3-bucket-sdk';
 
$object = getObject($s3Client, $bucket, $key);
?>

Response of PHP S3 Retrieve Data Function

The function returns an array with a bunch of keys and values reflecting all about the object. For the sake of the demo, the example index the array as follows.

print_r($object["@metadata"]['effectiveUri']);

The output is as follows.

https://s3-bucket-sdk.s3.amazonaws.com/cat-image-00001

Common S3 Scenarios with PHP AWS SDK

How to Read an S3 File Without Downloading in PHP

You can read a file using streams. Streams pipe in bytes of data and do not download a complete file in PHP. Streams also enable you to use PHP native file handling function and give you much more control over the upcoming bytes of files.

The following example reads bytes of the video we uploaded before and saves them to a destination file.

<?php
require 'vendor/autoload.php';
 
use Aws\S3\S3Client;
 
 
//Creates an S3 client from .aws file
$s3Client = new S3Client([
    'profile' => 'S3-Sudo-User',
    'region' => 'us-east-1',
    'version' => 'latest'
]);
 
 
$s3Client->registerStreamWrapper();
 
$newFile = fopen("receivedvideo.mp4", "a+");
 
if ($stream = fopen('s3://s3-bucket-sdk/bunny-video', 'r')) {
    // While the stream is still open
    while (!feof($stream)) {
        // Read 1024 bytes from the stream
        $data = fread($stream, 1024);
        //Writes the stream of data to a new file.
        fwrite($newFile, $data);
    }
    // Close stream
    fclose($stream);
}
 
?>

How to Upload Large Files to S3 with PHP

Use the MultipartUploader object To upload a file larger than 5GB up to 5TB. This object reads the source file in parts. It keeps track of the parts that fail to upload and retries uploading them in the end. If they still fail to upload, then it finally throws an error.

<?php
require 'vendor/autoload.php';
 
use Aws\S3\S3Client;
use Aws\Exception\AwsException;
use Aws\S3\MultipartUploader;
use Aws\Exception\MultipartUploadException;
 
 
function multiPartUpload($s3Client, $source, $bucket, $key) {
    $uploader = new MultipartUploader($s3Client, $source, [
        'bucket' => $bucket,
        'key' => $key,
    ]);
   
    try {
        $result = $uploader->upload();
        echo "Upload complete: {$result['ObjectURL']}\n";
    } catch (MultipartUploadException $e) {
        echo $e->getMessage() . "\n";
    }
}
 
//Creates an S3 client from .aws file
$s3Client = new S3Client([
    'profile' => 'S3-Sudo-User',
    'region' => 'us-east-1',
    'version' => 'latest'
]);
 
 
$key = 'cat-image-00002';
$bucket = 's3-bucket-sdk';
$src = './images/cat-1.jpg';
 
multiPartUpload($s3Client, $src, $bucket, $key);
?>

How to get S3 Filesize in PHP 

The ListObject SDK function returns an array of all the objects in an S3 bucket. The example below shows an object that’s in that returned array. Observe that the Size key has a numeric value representing bytes of data.

Array
(
    [Key] => cat-image-00002
    [LastModified] => Aws\Api\DateTimeResult Object
        (
            [date] => 2022-11-04 22:55:48.000000
            [timezone_type] => 2
            [timezone] => Z
        )
 
    [ETag] => "8bf95c9bba8f024cc9a70352b49d14e9-1"
    [Size] => 41611
    [StorageClass] => STANDARD
    [Owner] => Array
        (
            [DisplayName] => stephen+fuelingphp
            [ID] => 76b4b2355196c748f784711a5a093147b8380cc43c259c22749771ab59c599ee
        )
 
)

How do I upload video data to S3 using PHP

Similar to what we have seen in the case of the image example above. Let’s upload a video for demo purposes using the same example.

//Creates an S3 client from .aws file
$s3Client = new S3Client([
    'profile' => 'S3-Sudo-User',
    'region' => 'us-east-1',
    'version' => 'latest'
]);
 
$key = 'bunny-video';
$bucket = 's3-bucket-sdk';
$src = './videos/bunny.mp4';
 
 
uploadData($s3Client, $key, $bucket, $src);
//The object's URL is: https://s3-bucket-sdk.s3.amazonaws.com/bunny-video
how to store and retrieve data to cloud php aws
A Bunny

How to Upload Private Data to an S3 bucket

You can limit access via bucket policies. Alternatively, you can block all public access when creating an S3 bucket. If you use ACLs on objects, you can pass the value private in the ACL parameter in the PutObject SDK function.

How to Upload a file to S3 using a Pre-signed URL PHP

What is a Pre-signed URL?

An S3-authorised user generates a pre-signed URL to grant temporary access to an unauthorized user.

How to create an S3 Pre-signed URL in PHP

We need to create a command object and generate a pre-signed URL via this object. The createPresignedRequest function expects this command object and an expiry time. Chaining this function with withMethod(‘PUT’) means that we are granting write permissions.

<?php
require 'vendor/autoload.php';
 
use Aws\S3\S3Client;
 
function createPresignedURL($s3Client, $bucket, $key) {
    //Creating a presigned URL
    $cmd = $s3Client->getCommand('PutObject', [
        'Bucket' => $bucket,
        'Key' =>  $key
    ]);
 
    $request = $s3Client->createPresignedRequest($cmd, '+30 minutes')->withMethod('PUT');
 
    // Get the actual presigned-url
    return $request;
}
 
//Creates an S3 client from .aws file
$s3Client = new S3Client([
    'profile' => 'S3-Sudo-User',
    'region' => 'us-east-1',
    'version' => 'latest'
]);
 
 
$key = 'cat-image-00003';
$bucket = 's3-bucket-sdk';
 
$request =  createPresignedURL($s3Client, $bucket, $key);
 
echo (string) $request->getUri();
 
//https://s3-bucket-sdk.s3.amazonaws.com/cat-image-00003?X-Amz-Content-Sha256=UNSIGNED-PAYLOAD&X-Amz-Algorithm=AWS4-HMAC-SHA256&X-Amz-Credential=AKIAXP3OL5ATBAMQWJVU%2F20221104%2Fus-east-1%2Fs3%2Faws4_request&X-Amz-Date=20221104T234446Z&X-Amz-SignedHeaders=host&X-Amz-Expires=1800&X-Amz-Signature=c5f62f88e93a6fcf8743a815a59a2f3fc3690b5ad4cd5984c7b0ef3cde45df71
?>

How to Upload data to S3 using a Pre-signed URL with PHP

You can use a cURL PUT request to upload data using the pre-signed URL. The example is as follows.

$request =  createPresignedURL($s3Client, $bucket, $key);
 
//Initialise the cURL var
$curl = curl_init();
 
//Create a POST array with the file in it
$imgData = file_get_contents('./images/cat-1.jpg');
 
//All options in an array
$options = [
   CURLOPT_URL => $request->getUri(),
   CURLOPT_CUSTOMREQUEST => 'PUT',
   CURLOPT_RETURNTRANSFER => true,
   CURLOPT_POSTFIELDS => $imgData
 
];
 
//Set the Options array.
curl_setopt_array($curl, $options);
 
// Execute the request
$response = curl_exec($curl);
 
//If error occurs
if (curl_errno($curl)) {
  echo "CURL ERROR - " . curl_error($ch);
}
 
//Close to remove $curl from memory
curl_close($curl);

Can I use AWS to Store Files with PHP

Definitely YES, AWS S3 is an excellent service to persist files, including text, images, videos, and even static websites. AWS S3 has some of the most inspiring features, including enhanced security using policies, ACLs and encryption, and high availability and durability. 

AWS SDK for PHP makes it easier to interact with S3. It exposes easy-to-use API functions to programmatically do almost every possible action on your AWS S3 buckets and objects. The examples above perform some basic actions – Upload, List, and Get.

What type of Data can I Store in S3 with PHP

You can upload any kind of data to S3, but I recommend that static data like images, json, csv, video, media, etc is best. Data that changes frequently on the fly may not be the best type to keep in S3. An example would be a database or an operating system because S3 would be slower and more expensive than alternatives.

Can I use AWS S3 as a Database using PHP

No. S3 is not a database for PHP. We do not recommend using S3 as a database. You cannot query S3 buckets like you would a database. It would also be slow. Two alternatives would be to load the data in AWS Athena and query that way or use AWS DynamoDB as a key-value store. Those will be smarter alternatives if you need to use database-like features. 

How do I Install & use AWS SDK for PHP

AWS Software Development Kit (SDK) is an open-source library for PHP which helps integrate AWS into your PHP applications. You can programmatically access AWS services and resources using AWS SDK.

Want to see how to install AWS SDK? FuelingPHP features a complete walkthrough of “How to install and use AWS SDK for PHP”. 

Can I host a PHP Website in AWS S3

No, you cannot host a PHP website on S3 because PHP requires a web server. Only compiled Javascript websites can be hosted on S3 because it can statically host them. For instance – you can host your portfolio website. Unlike a fully functioning web application, a portfolio website is usually just presentational.

Check out our article on Hosting PHP Websites in AWS to learn about your options.

Does AWS Support PHP

Yes, AWS does support PHP. You can host your website directly on AWS. You can also use the AWS SDK for PHP to use many of their services such as S3, SQS, DynamoDB and more.

Elastic Beanstalk is the quickest and most straightforward, EC2 comes with PHP pre-installed, and you can deploy containers using AWS Fargate & ECS. Check out this article if you want to host your PHP app on Elastic Beanstalk.

PHP & AWS S3: Store and Retrieve Data to the Cloud

This article explores how to store and retrieve data from the cloud via PHP AWS SDK. The article includes examples of uploading, listing, and downloading AWS S3 objects. Following these basic examples, the article covers many frequently occurring scenarios with PHP AWS S3.

Free AWS Development Guide

Stop running in circles and develop your applications faster and cheaper in AWS. This guide will walk you through ways to maximize AWS to generate real value for your needs. We pick the right services to scale, tighten security and maximize costs.

Download our free guide now and get started with confidence.

Building Web Applications in AWS

This article is part of our series to make AWS easy. We love AWS, but let’s be honest. It isn’t effortless. It’s way too complicated. We’ve created this learning path to help level you up and onboard your PHP app quickly.

Did you find this article helpful?

Join the best weekly newsletter where I deliver content on building better web applications. I curate the best tips, strategies, news & resources to help you develop highly-scalable and results-driven applications.

Build Better Web Apps

I hope you're enjoying this article.

Get the best content on building better web apps delivered to you.