Retrieve your Amazon S3 Buckets list using the Amazon S3 SOAP API and WsdlToPhp

  27 June 2013       This post has 0 comment       By Mikaël DELSOL

Requirements

Your server must run the latest PHP version with the native SoapClient class.

You first need to download the generated package from the Amazon S3 generated package page. It includes :

  • the sample file: the file that shows you how to start using the generated package
  • the autoload file: the file which loads all the generated class with the good path
  • the main WsdlClass class: the class from which each generated class inherits in order to inherit usefull methods and generic methods to deal with the SoapClient
  • the ClassMap class: the class which defines the mapping between native Amazon S3 SOAP structs and generated classes
  • all the classes required to communicate with the Amazon S3 SOAP API

If you have a good IDE, then it should be really easy to navigate trough all these numerous classes.

A simple case: get your Buckets list on Amazon S3

First of all, you need to have an Amazon S3 Account. It's free and easy to get one.

Then, you'll have to get your AWS Security Credentials from the Security Credentials Page or generate an access key in the "Access Credentials" part if you don't have done it yet.

First step: load classes and set configuration

The generated package includes an autoload file which makes easy to load all the generated classes at once.

require_once __DIR__ . '/AmazonS3Autoload.php';

We suppose your file is located in the root directory of the extracted package you just downloaded.

When all classes are loaded, you can define the configuration to call the SOAP API. This configuration is required to instantiate any AmazonS3 Service class.

$wsdl = array(); // we use the WSDL latest version $wsdl[AmazonS3WsdlClass::WSDL_URL] = 'http://s3.amazonaws.com/doc/2006-03-01/AmazonS3.wsdl'; $wsdl[AmazonS3WsdlClass::WSDL_CACHE_WSDL] = WSDL_CACHE_NONE; $wsdl[AmazonS3WsdlClass::WSDL_TRACE] = true;

Last step: call the operation ListAllMyBuckets

Calling the operation is pretty trivial and is then done by calling the AmazonS3ServiceList::ListAllMyBuckets() method using the configuration set previously as the $wsdl variable and by passing the correct parameter to the method. You can read the official online documentation about the method ListAllMyBuckets on Amazon.

$amazonS3ServiceList = new AmazonS3ServiceList($wsdl); // The sample code is within the sample-amazons3.php file, // so you don't have to know each operation parameter class to use in your mind. // Just a little explanation about each required parameters for the AmazonS3StructListAllMyBuckets object : // - AWSAccessKeyId: from the Security Credentials Page, copy paste your AWS Access Key ID // - Timestamp: This must be a dateTime // - Signature: The RFC 2104 HMAC-SHA1 digest of the concatenation of "AmazonS3"+OPERATION+Timestamp, using your AWS Secret Access Key as the key // Find all the details on http://docs.aws.amazon.com/AmazonS3/latest/dev/SOAPAuthentication.html // Your AWS Access Key ID $awsAccessKeyId = 'Your AWS Access Key ID'; // The Timestamp parameter value $timestamp = gmdate('Y-m-d\TH:i:s.000\Z'); // The Signature parameter value $signature = base64_encode(hash_hmac('sha1','AmazonS3' . 'ListAllMyBuckets' . $timestamp,'Your AWS Secret Access Key',true)); if($amazonS3ServiceList->ListAllMyBuckets(new AmazonS3StructListAllMyBuckets($awsAccessKeyId,$timestamp,$signature))) print_r($amazonS3ServiceList->getResult()); else print_r($amazonS3ServiceList->getLastError());

If everything works fine, the $amazonS3ServiceList->getResult() returns a AmazonS3StructListAllMyBucketsResponse object which contains the results of your Buckets list such as:

AmazonS3StructListAllMyBucketsResponse Object ( [ListAllMyBucketsResponse] => AmazonS3StructListAllMyBucketsResult Object ( [Owner] => AmazonS3StructCanonicalUser Object ( [ID] => ************************************************************** [DisplayName] => delsol.mikael ) [Buckets] => AmazonS3StructListAllMyBucketsList Object ( [Bucket] => Array ( [0] => AmazonS3StructListAllMyBucketsEntry Object ( [Name] => wsdltophp [CreationDate] => 2013-06-27T21:03:36.000Z ) [1] => AmazonS3StructListAllMyBucketsEntry Object ( [Name] => wsdltophp-amazon [CreationDate] => 2013-06-27T21:14:17.000Z ) ) ) ) )

The XML request can be fetched using the AmazonS3ServiceList::getLastRequest() method which should return something like this:

<?xml version="1.0" encoding="UTF-8"?> <SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" xmlns:ns1="http://s3.amazonaws.com/doc/2006-03-01/">   <SOAP-ENV:Body>     <ns1:ListAllMyBuckets>       <ns1:AWSAccessKeyId>******************</ns1:AWSAccessKeyId>       <ns1:Timestamp>2013-06-27T21:38:52.000Z</ns1:Timestamp>       <ns1:Signature>**************************=</ns1:Signature>     </ns1:ListAllMyBuckets>   </SOAP-ENV:Body> </SOAP-ENV:Envelope>

Then we can also look to the XML response by calling the AmazonS3ServiceList::getLastResponse() method which should return something like this:

<?xml version="1.0" encoding="utf-8"?> <soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:wsa="http://schemas.xmlsoap.org/ws/2004/08/addressing">   <soapenv:Header>     <wsa:MessageID soapenv:mustUnderstand="0">uuid:**************************</wsa:MessageID>     <wsa:To soapenv:mustUnderstand="0">http://schemas.xmlsoap.org/ws/2004/08/addressing/role/anonymous</wsa:To>   </soapenv:Header>   <soapenv:Body>     <ListAllMyBucketsResponse xmlns="http://s3.amazonaws.com/doc/2006-03-01/">       <ListAllMyBucketsResponse>         <Owner>           <ID>**************************************************************</ID>           <DisplayName>delsol.mikael</DisplayName>         </Owner>         <Buckets>           <Bucket>             <Name>wsdltophp</Name>             <CreationDate>2013-06-27T21:03:36.000Z</CreationDate>           </Bucket>           <Bucket>             <Name>wsdltophp-amazon</Name>             <CreationDate>2013-06-27T21:14:17.000Z</CreationDate>           </Bucket>         </Buckets>       </ListAllMyBucketsResponse>     </ListAllMyBucketsResponse>   </soapenv:Body> </soapenv:Envelope>

Otherwise, you have to analyze the last error catched when the AmazonS3ServiceList::ListAllMyBuckets() method has been called by calling the generic AmazonS3WsdlClass::getLastError() method.

Conclusion

As you can see, getting your Amazon S3 Buckets list is pretty trivial and quick. We saw that each value is OOP and that responses are entirely returned as objects so we can easily manipulate them. The generated classes aim to ease the communication with SOAP Web services at each step. If you have any question, feel free to send me comments on this topic. Now, you can easily call any other Amazon S3 SOAP API operation by following this methodology.

Edit: Now you can do it using the soap client web interface.


Post comment
Security text: *