Sample AWS PHP v3 SDK code


It's not recommended to store credentials in an executable file. A better option is to load credentials from 'environment variables' or from a 'credentials ini file'. View the following site for further details:

Creating a connection

Creates an S3 client object to interact with the DHO server:

define('AWS_KEY', 'your_access_key');
define('AWS_SECRET_KEY', 'your_secret_key');
define('HOST', '');
define('REGION', 'us-east-1');

// require the AWS SDK for PHP library
require 'aws-autoloader.php';

use Aws\S3\S3Client;

// Establish connection with DreamObjects with an S3 client.
$client = new Aws\S3\S3Client([
    'version'     => '2006-03-01',
    'region'      => REGION,
    'endpoint'    => HOST,
        'credentials' => [
        'key'      => AWS_KEY,
        'secret'   => AWS_SECRET_KEY,

Listing owned Buckets

Lists buckets owned by the S3 user:

$buckets = $client->listBuckets();
try {
    foreach ($buckets['Buckets'] as $bucket){
        echo "{$bucket['Name']}\t{$bucket['CreationDate']}\n";
} catch (S3Exception $e) {
    echo $e->getMessage();
    echo "\n";

The response appears similar to the following:

mahbuckat1   2011-04-21T18:05:39.000Z
mahbuckat2   2011-04-21T18:05:48.000Z
mahbuckat3   2011-04-21T18:07:18.000Z

Creating a Bucket

try {
    $result = $client->createBucket([
        'Bucket' => $BUCKET_NAME,
} catch (S3Exception $e) {
    // output error message if fails
    echo $e->getMessage();
    echo "\n";

Listing a Bucket’s contents

$objects = $client->listObjectsV2([
        'Bucket' => 'my-new-bucket',
foreach ($objects['Contents'] as $object){
    echo "{$object['Key']}\t{$object['LastModified']}\n";

The response appears similar to the following if the bucket contains any files:

myphoto1.jpg 251262  2011-08-08T21:35:48.000Z
myphoto2.jpg 262518  2011-08-08T21:38:01.000Z

Deleting the entire contents of a Bucket

$batch = Aws\S3\BatchDelete::fromListObjects($client, ['Bucket' => 'my-new-bucket']);

Deleting a Bucket

The Bucket must be empty, otherwise the following call does not work.

$client->deleteBucket(array('Bucket' => 'my-new-bucket'));

Creating an object

Uploads a file from the filesystem and sets it to 'private':

$bucket = 'my-bucket-name';
$file_Path = './hello.txt';
$key = basename($file_Path);
    $result = $client->putObject([
        'Bucket'     => $bucket,
        'Key'        => $key,
        'SourceFile' => $file_Path,
        'ACL'        => 'private',
} catch (S3Exception $e) {
    echo $e->getMessage() . "\n";

Changing an object’s Access Control List (ACL)

Changes the availability of the object hello.txt to 'publicly readable', and object secret_plans.txt to 'private'.

    'Bucket' => 'my-bucket-name',
    'Key'    => 'hello.txt',
    'ACL'    => 'public-read'
    'Bucket' => 'my-bucket-name',
    'Key'    => 'secret_plans.txt',
    'ACL'    => 'private'

An ACL can be either private, public-read, public-read-write, authenticated-read, bucket-owner-read, or bucket-owner-full-control. See the following for more information:

Deleting an object

Deletes the object goodbye.txt:

    'Bucket' => 'my-bucket-name',
    'Key'    => 'goodbye.txt',

Downloading an object to a file

Downloads the object poetry.pdf from ‘my-bucket-name’ and saves it in /home/username/documents. . Make sure to change username to your Shell user.

    'Bucket' => 'my-bucket-name',
    'Key'    => 'poetry.pdf',
    'SaveAs' => '/home/username/documents/poetry.pdf'

Generating object download URLs (unsigned and pre-signed)

Generates an unsigned download URL for hello.txt. This works because hello.txt was made public by setting the ACL above, which then generates a signed download URL for secret_plans.txt that works for 1 hour. Signed download URLs work for the time period even if the object is private (when the time period is up, the URL stops working):

$plain_url = $client->getObjectUrl('my-bucket-name', 'hello.txt');
echo $plain_url . "\n";

$cmd = $client->getCommand('GetObject', [
        'Bucket' => 'my-bucket-name',
        'Key'    => 'secret_plans.txt'
$signed_url = $client->createPresignedRequest($cmd, '+1 hour');
echo $signed_url->getUri() . "\n";

The response appears similar to the following:

Did this article answer your questions?

Article last updated PST.

Still not finding what you're looking for?