PHP S3 Examples

Installing AWS PHP SDK

This installs AWS PHP SDK using composer (see here how to install composer).

  1. $ composer install aws/aws-sdk-php

Creating a Connection

This creates a connection so that you can interact with the server.

Note

The client initialization requires a region so we use ''.

  1. <?php
  2. use Aws\S3\S3Client;
  3. define('AWS_KEY', 'place access key here');
  4. define('AWS_SECRET_KEY', 'place secret key here');
  5. $ENDPOINT = 'http://objects.dreamhost.com';
  6. // require the amazon sdk from your composer vendor dir
  7. require __DIR__.'/vendor/autoload.php';
  8. // Instantiate the S3 class and point it at the desired host
  9. $client = new S3Client([
  10. 'region' => '',
  11. 'version' => '2006-03-01',
  12. 'endpoint' => $ENDPOINT,
  13. 'credentials' => [
  14. 'key' => AWS_KEY,
  15. 'secret' => AWS_SECRET_KEY
  16. ],
  17. // Set the S3 class to use objects.dreamhost.com/bucket
  18. // instead of bucket.objects.dreamhost.com
  19. 'use_path_style_endpoint' => true
  20. ]);

Listing Owned Buckets

This gets a AWS\Result instance that is more convenient to visit using array access way. This also prints out the bucket name and creation date of each bucket.

  1. <?php
  2. $listResponse = $client->listBuckets();
  3. $buckets = $listResponse['Buckets'];
  4. foreach ($buckets as $bucket) {
  5. echo $bucket['Name'] . "\t" . $bucket['CreationDate'] . "\n";
  6. }

The output will look something like this:

  1. mahbuckat1 2011-04-21T18:05:39.000Z
  2. mahbuckat2 2011-04-21T18:05:48.000Z
  3. mahbuckat3 2011-04-21T18:07:18.000Z

Creating a Bucket

This creates a new bucket called my-new-bucket and returns a AWS\Result object.

  1. <?php
  2. $client->createBucket(['Bucket' => 'my-new-bucket']);

List a Bucket’s Content

This gets a AWS\Result instance that is more convenient to visit using array access way. This then prints out each object’s name, the file size, and last modified date.

  1. <?php
  2. $objectsListResponse = $client->listObjects(['Bucket' => $bucketname]);
  3. $objects = $objectsListResponse['Contents'] ?? [];
  4. foreach ($objects as $object) {
  5. echo $object['Key'] . "\t" . $object['Size'] . "\t" . $object['LastModified'] . "\n";
  6. }

Note

If there are more than 1000 objects in this bucket, you need to check $objectsListResponse[‘isTruncated’] and run again with the name of the last key listed. Keep doing this until isTruncated is not true.

The output will look something like this if the bucket has some files:

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

Deleting a Bucket

This deletes the bucket called my-old-bucket and returns a AWS\Result object

Note

The Bucket must be empty! Otherwise it won’t work!

  1. <?php
  2. $client->deleteBucket(['Bucket' => 'my-old-bucket']);

Creating an Object

This creates an object hello.txt with the string "Hello World!"

  1. <?php
  2. $client->putObject([
  3. 'Bucket' => 'my-bucket-name',
  4. 'Key' => 'hello.txt',
  5. 'Body' => "Hello World!"
  6. ]);

Change an Object’s ACL

This makes the object hello.txt to be publicly readable and secret_plans.txt to be private.

  1. <?php
  2. $client->putObjectAcl([
  3. 'Bucket' => 'my-bucket-name',
  4. 'Key' => 'hello.txt',
  5. 'ACL' => 'public-read'
  6. ]);
  7. $client->putObjectAcl([
  8. 'Bucket' => 'my-bucket-name',
  9. 'Key' => 'secret_plans.txt',
  10. 'ACL' => 'private'
  11. ]);

Delete an Object

This deletes the object goodbye.txt

  1. <?php
  2. $client->deleteObject(['Bucket' => 'my-bucket-name', 'Key' => 'goodbye.txt']);

Download an Object (to a file)

This downloads the object poetry.pdf and saves it in /home/larry/documents/

  1. <?php
  2. $object = $client->getObject(['Bucket' => 'my-bucket-name', 'Key' => 'poetry.pdf']);
  3. file_put_contents('/home/larry/documents/poetry.pdf', $object['Body']->getContents());

Generate Object Download URLs (signed and unsigned)

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

  1. <?php
  2. $hello_url = $client->getObjectUrl('my-bucket-name', 'hello.txt');
  3. echo $hello_url."\n";
  4. $secret_plans_cmd = $client->getCommand('GetObject', ['Bucket' => 'my-bucket-name', 'Key' => 'secret_plans.txt']);
  5. $request = $client->createPresignedRequest($secret_plans_cmd, '+1 hour');
  6. echo $request->getUri()."\n";

The output of this will look something like:

  1. http://objects.dreamhost.com/my-bucket-name/hello.txt
  2. http://objects.dreamhost.com/my-bucket-name/secret_plans.txt?X-Amz-Content-Sha256=UNSIGNED-PAYLOAD&X-Amz-Algorithm=AWS4-HMAC-SHA256&X-Amz-Credential=sandboxAccessKey%2F20190116%2F%2Fs3%2Faws4_request&X-Amz-Date=20190116T125520Z&X-Amz-SignedHeaders=host&X-Amz-Expires=3600&X-Amz-Signature=61921f07c73d7695e47a2192cf55ae030f34c44c512b2160bb5a936b2b48d923