Working with S3 and Cloud Storage in PHP: AWS SDK for PHP

Online Python Trainer for Beginners

Learn Python easily without overwhelming theory. Solve practical tasks with automatic checking, get hints in Russian, and write code directly in your browser — no installation required.

Start Course

Working with S3 and Cloud Storage in PHP: AWS SDK for PHP



Modern web applications increasingly use cloud storage for storing files, images, backups, and other content. Amazon S3 (Simple Storage Service) is one of the most popular object storage services, providing scalable, secure, and high-performance storage. In this article, we will look at how to work with S3 and cloud storage in PHP using the official AWS SDK for PHP.



What is AWS SDK for PHP?



AWS SDK for PHP is the official library from Amazon that allows you to interact with AWS services, including S3, from PHP applications. It provides a convenient object-oriented interface, automatic request signature management, error handling, and support for all modern PHP versions (7.2+).



Installing AWS SDK



Installation is done via Composer. Run the command in the root of your project:



composer require aws/aws-sdk-php


After installation, include the Composer autoloader in your PHP file:



require 'vendor/autoload.php';


Configuring S3 Connection



To work with S3, you will need an Access Key ID and Secret Access Key, which can be obtained from the AWS IAM console. You also need to specify the region of your bucket (e.g., us-east-1 or eu-central-1).



Example of basic S3 client configuration:



use Aws\\S3\\S3Client;

$s3Client = new S3Client([ 'version' => 'latest', 'region' => 'us-east-1', 'credentials' => [ 'key' => 'YOUR_ACCESS_KEY', 'secret' => 'YOUR_SECRET_KEY', ],]);


For production, it is recommended to use environment variables or AWS configuration files to avoid storing keys in code:



putenv('AWS_ACCESS_KEY_ID=YOUR_ACCESS_KEY');putenv('AWS_SECRET_ACCESS_KEY=YOUR_SECRET_KEY');putenv('AWS_DEFAULT_REGION=us-east-1');


Basic S3 Operations in PHP



1. Creating a Bucket



A bucket is a container for your objects (files). The bucket name must be globally unique:



try {    $result = $s3Client->createBucket([        'Bucket' => 'my-unique-bucket-name',    ]);    echo "Bucket created: " . $result['Location'];} catch (Aws\\Exception\\AwsException $e) {    echo "Error: " . $e->getMessage();}


2. Uploading a File (PutObject)



Uploading a file from the local file system to S3:



try {    $result = $s3Client->putObject([        'Bucket' => 'my-unique-bucket-name',        'Key'    => 'images/photo.jpg',         // path to the object in the bucket        'SourceFile' => '/local/path/to/photo.jpg', // local file        'ACL'    => 'public-read',               // access permissions    ]);    echo "File uploaded. URL: " . $result['ObjectURL'];} catch (Aws\\Exception\\AwsException $e) {    echo "Upload error: " . $e->getMessage();}


You can also upload data directly from a string using the Body parameter:



$result = $s3Client->putObject([    'Bucket' => 'my-unique-bucket-name',    'Key'    => 'data.txt',    'Body'   => 'File content',]);


3. Downloading a File (GetObject)



You can download a file from S3 to a file or to a variable:



// To a file$result = $s3Client->getObject([    'Bucket' => 'my-unique-bucket-name',    'Key'    => 'images/photo.jpg',    'SaveAs' => '/local/path/to/downloaded_photo.jpg',]);

// To a variable$result = $s3Client->getObject([ 'Bucket' => 'my-unique-bucket-name', 'Key' => 'data.txt',]);$content = $result['Body']->getContents();echo $content;


4. Deleting a File (DeleteObject)



try {    $                    

Recommendations