Guides

Storage OnLine Solutions: Using S3 API with PHP

Hosting Solutions' Storage OnLine services guarantee secure, reliable and high-performance access to your data. All available solutions provide access via S3 (Simple Storage Service) REST API, iprogramming interfaces created by Amazon that have become a standard for accessing remotely stored data. The specifications of these Application Program Interfaces (API) are well documented and publicly available, which has led to the emergence of a large number of libraries and programs that use them.

What is REST

The S3 REST API fully conforms, as the name suggests, to the costraints of REST - Representational State Transfer architectural style for accessing resources via the web. Without going into too much detail we can say that, compared to other protocols (such as SOAP), REST has several advantages:

  • it is much simpler to implement a client: this allows programs to be written more quickly and with less errors;
  • it uses the available bandwidth more efficiently;
  • it makes it possible to exploit advanced features of the HTTP protocol, especially in terms of caching.

However, in spite of the simplicity of the API, writing the code yourself to perform HTTP calls is a long and complex process; therefore, we normally use the many free libraries available where there is plenty of documentation and which are already extensively tested.

S3 and PHP

To use the S3 API with the PHP language, you can refer to AWS SDK for PHP, which stands for Amazon Web Services Software Development Kit.

In our examples we will simply use Amazon S3 PHP Class, which is much easier and has fewer requirements than the AWS SDK for PHP.

So let's see how you can use this library in your PHP applications:

  • download the library from the official website;
  • extract the archive to a folder on the server;
  • include the S3.php file (the only source) using the require or require_once;
  • configure the S3.php class.

Here is an example:

setEndpoint(s3EndPoint);
$s3->setSSL(true, false);
?>      

Note that:

  • the library (the S3.php file) has been placed in the lib folder;
  • USERNAME, PASSWORD AND_S3 ACCESS are shown on the S3 protocol activation page on the control panel and are also provided in the email following activation of the service.

We will use this code in all the PHP examples in these guides.

Resources: