---
title: "aws/aws-sdk-php for Working With S3 Storage in PHP | Hostman Docs"
description: "Learn how to use aws/aws-sdk-php to work with S3-compatible storage in PHP. Configure credentials, connect to storage endpoints, and perform file upload, download, listing, and deletion operations with practical examples."
---

> For the complete documentation index for AI agents, see [llms.txt](https://hostman.com/llms.txt).

[aws/aws-sdk-php](https://github.com/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:

```shell
composer require aws/aws-sdk-php
```

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

```shell
require 'vendor/autoload.php';
```

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

Check whether the extension is available:

```shell
php -m | grep xml
```

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

-   Debian / Ubuntu: 
    

```shell
sudo apt install php-xml
```

-   Alpine:
    

```shell
apk add php8-xml
```

-   RHEL / CentOS:
    

```shell
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](https://hostman.com/docs/s3/tools/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:

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

And `~/.aws/config`:

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

### Using Environment Variables

You can also provide credentials via environment variables:

```shell
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:

```php
$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:

```php
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:

```php
$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 code:

```php
<?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";
}
```
