Learning AWS SDK for PHP and basic S3 operations with sample code
This section introduces you about how to install AWS SDK for PHP and how to create S3 buckets, put objects, get objects using the sample code, and explains how the sample code runs as well.
Getting ready
AWS SDK for PHP is a powerful tool for PHP developers to quickly build their stable applications.
To get started with AWS SDK for PHP, it is necessary to install the following on your development machine:
- PHP-5.3.3+, compiled with the uCRL extension (http://php.net/)
- Composer (https://getcomposer.org/)
It is recommended to use Composer to install AWS SDK for PHP because it is much easier than getting the source code.
How to do it…
Proceed with the following steps to install the packages and run the sample application:
- Download the sample SDK application:
$ git clone https://github.com/awslabs/aws-php-sample.git $ cd aws-php-sample/
- Run the sample application:
$ php sample.php
How it works…
The sample code works as shown in the following diagram; initiating the credentials to allow access to Amazon S3, creating a bucket in a region, putting and getting objects into the bucket, and then finally deleting the objects and the bucket:
Now, let's run the sample application and see the output of the preceding command, as shown in the following screenshot, and then follow the source code step by step:
Now, let's examine the sample code; the path is aws-php-sample/sample.php
.
The s3Client::facory
method creates an AWS client and is the easiest way to get up and running:
$client = S3Client::factory();
The createBucket
method creates an S3 bucket with the specified name in a region defined in the credentials file:
$result = $client->createBucket(array( 'Bucket' => $bucket ));
The PutOjbect
method uploads objects into the bucket:
$key = 'hello_world.txt'; $result = $client->putObject(array( 'Bucket' => $bucket, 'Key' => $key, 'Body' => "Hello World!" ));
The getObject
method retrieves objects from the bucket:
$result = $client->getObject(array( 'Bucket' => $bucket, 'Key' => $key ));
The deleteObject
method removes objects from the bucket:
$result = $client->deleteObject(array( 'Bucket' => $bucket, 'Key' => $key ));
The deleteBucket
method deletes the bucket:
$result = $client->deleteBucket(array( 'Bucket' => $bucket ));
The whole sample code is as follows:
<?php use Aws\S3\S3Client; $client = S3Client::factory(); $bucket = uniqid("php-sdk-sample-", true); echo "Creating bucket named {$bucket}\n"; $result = $client->createBucket(array( 'Bucket' => $bucket )); $client->waitUntilBucketExists(array('Bucket' => $bucket)); $key = 'hello_world.txt'; echo "Creating a new object with key {$key}\n"; $result = $client->putObject(array( 'Bucket' => $bucket, 'Key' => $key, 'Body' => "Hello World!" )); echo "Downloading that same object:\n"; $result = $client->getObject(array( 'Bucket' => $bucket, 'Key' => $key )); echo "\n---BEGIN---\n"; echo $result['Body']; echo "\n---END---\n\n"; echo "Deleting object with key {$key}\n"; $result = $client->deleteObject(array( 'Bucket' => $bucket, 'Key' => $key )); echo "Deleting bucket {$bucket}\n"; $result = $client->deleteBucket(array( 'Bucket' => $bucket ));
See also
- AWS SDK for the PHP sample application,. available at https://github.com/aws/aws-sdk-php
- Developer Guide available at http://docs.aws.amazon.com/aws-sdk-php/guide/latest/index.html
- The API documentation available at http://docs.aws.amazon.com/aws-sdk-php/latest/