Hello tutorial for the Amazon SDK for PHP - Amazon SDK for PHP
Services or capabilities described in Amazon Web Services documentation might vary by Region. To see the differences applicable to the China Regions, see Getting Started with Amazon Web Services in China (PDF).

Hello tutorial for the Amazon SDK for PHP

Say hello to Amazon S3 using the Amazon SDK for PHP. The following example displays a list of your Amazon S3 buckets.

Including the SDK in your code

No matter which technique you used to install the SDK, you can include the SDK in your code with just a single require statement. See the following table for the PHP code that best fits your installation technique. Replace any instances of /path/to/ with the actual path on your system.

Installation Technique Require Statement

Using Composer

require '/path/to/vendor/autoload.php';

Using the phar

require '/path/to/aws.phar';

Using the ZIP

require '/path/to/aws-autoloader.php';

In this topic, we assume the Composer installation method. If you’re using a different installation method, you can refer back to this section to find the correct require code to use.

Write the code

Copy and paste the following code into a new source file. Save and name the file hello-s3.php.

require 'vendor/autoload.php'; use Aws\S3\S3Client; /** * List your Amazon S3 buckets. * * This code expects that you have AWS credentials set up per: * https://docs.aws.amazon.com/sdk-for-php/v3/developer-guide/guide_credentials.html */ //Create a S3Client $s3Client = new S3Client([ 'profile' => 'default', 'region' => 'us-west-2', 'version' => '2006-03-01' ]); //Listing all S3 Bucket $buckets = $s3Client->listBuckets(); foreach ($buckets['Buckets'] as $bucket) { echo $bucket['Name'] . "\n"; }

Running the program

Open a command prompt to run your PHP program. The typical command syntax to run a PHP program is:

php [source filename] [arguments...]

This sample code uses no arguments. To run this code, enter the following into the command prompt:

$ php hello-s3.php

Next steps

To test out many other Amazon S3 operations, check out the Amazon Code Examples Repository on GitHub.