Sign In
Sign In

aws/aws-sdk-php is the official Amazon SDK for working with the S3 service in PHP. It allows you to perform basic object operations in S3, including uploading and downloading files, deleting objects, listing bucket contents, and generating pre-signed URLs.

Installation

Install the library using Composer:

composer require aws/aws-sdk-php

After installation, the library will be located in the vendor directory. To use it, include the autoloader:

require 'vendor/autoload.php';

The SDK requires the PHP ext-xml extension to be installed.

Check whether the extension is available:

php -m | grep xml

If the extension is not installed, install it using your package manager:

  • Debian / Ubuntu: 

sudo apt install php-xml
  • Alpine:

apk add php8-xml
  • RHEL / CentOS:

sudo yum install php-xml

Configuring Access

Access credentials can be configured in several ways.

Using ~/.aws/config and ~/.aws/credentials

When you use the AWS CLI, a .aws directory containing configuration files is automatically created in the user’s home directory. If necessary, you can create this directory and the files manually without installing the AWS CLI.

The ~/.aws/credentials file should contain:

[default]
aws_access_key_id = <ACCESS_KEY>
aws_secret_access_key = <SECRET_KEY>

And ~/.aws/config:

[default]
output = json
endpoint_url = https://s3.hmstorage.net
region = us-2

Using Environment Variables

You can also provide credentials via environment variables:

export AWS_ACCESS_KEY_ID=<ACCESS_KEY>
export AWS_SECRET_ACCESS_KEY=<SECRET_KEY>

In this case, you can use the SDK without additional configuration:

$s3 = new Aws\S3\S3Client([
    'region' => 'us-2',
    'version' => 'latest',
    'endpoint' => 'https://s3.hmstorage.net',
]);

Passing Credentials in Code

Credentials can be explicitly provided when creating the client:

use Aws\S3\S3Client;

$s3 = new S3Client([
    'region' => 'us-2',
    'version' => 'latest',
    'endpoint' => 'https://s3.hmstorage.net',
    'credentials' => [
        'key' => '<ACCESS_KEY>',
        'secret' => '<SECRET_KEY>',
    ],
]);

Using a Different AWS CLI Profile

If multiple profiles are configured in ~/.aws/credentials, you can specify the required one:

$s3 = new S3Client([
    'profile' => 'myprofile',
    'region' => 'us-2',
    'version' => 'latest',
    'endpoint' => 'https://s3.hmstorage.net',
]);

Example

The example below demonstrates basic object operations:

  • uploading a file to a bucket;
  • listing objects in the bucket;
  • downloading a file;
  • deleting an object.

Before running the script, create a file named example.txt in the same directory as the script.

Example сode:

<?php

require 'vendor/autoload.php';

use Aws\S3\S3Client;
use Aws\Exception\AwsException;

$bucket = 'bucket_name';
$key = 'example.txt';
$localUpload = 'example.txt';
$localDownload = 'downloaded_example.txt';

$s3 = new S3Client([
    'region' => 'us-2',
    'version' => 'latest',
    'endpoint' => 'https://s3.hmstorage.net',
    'credentials' => [
        'key' => '<ACCESS_KEY>',
        'secret' => '<SECRET_KEY>',
    ],
]);

try {
    // 1. Upload file to the bucket
    if (file_exists($localUpload)) {
        echo "Uploading file $localUpload...\n";
        $s3->putObject([
            'Bucket' => $bucket,
            'Key' => $key,
            'SourceFile' => $localUpload,
        ]);
        echo "Upload completed.\n";
    } else {
        echo "File $localUpload not found. Skipping upload.\n";
    }

    // 2. List objects
    echo "\nBucket contents:\n";
    $result = $s3->listObjectsV2([
        'Bucket' => $bucket,
    ]);

    if (!empty($result['Contents'])) {
        foreach ($result['Contents'] as $object) {
            echo "- {$object['Key']}\n";
        }
    } else {
        echo "(Bucket is empty)\n";
    }

    // 3. Download file
    echo "\nDownloading $key to $localDownload...\n";
    $s3->getObject([
        'Bucket' => $bucket,
        'Key' => $key,
        'SaveAs' => $localDownload,
    ]);
    echo "Download completed.\n";

    // 4. Delete file
    echo "\nDeleting $key from the bucket...\n";
    $s3->deleteObject([
        'Bucket' => $bucket,
        'Key' => $key,
    ]);
    echo "Deletion completed.\n";

} catch (AwsException $e) {
    echo "Error: {$e->getMessage()}\n";
}
Was this page helpful?
Updated on 14 January 2026

Do you have questions,
comments, or concerns?

Our professionals are available to assist you at any moment,
whether you need help or are just unsure of where to start.
Email us
Hostman's Support