S3 Stream Wrapper

The S3 Stream Wrapper is a stream wrapper interface for PHP that provides access to Amazon S3 using PHP’s standard File System API functions.

What issues does this address?

There are a large number of existing applications, code snippets and other bits of PHP that are designed to read and write from the local file system as part of their normal operations. Many of these apps could benefit from moving into the cloud, but doing so would require rewriting a substantial amount of code.

What if we could simplify this process so that the updates required to make an existing application cloud-backed would be very minimal?

Proposed solution

PHP provides an interface for solving this exact kind of problem, called the Stream Wrapper interface. By writing a class that implements this interface and registering it as a handler we can reduce both the amount of rewriting that needs to be done for existing applications, as well as substantially lower the learning curve for reading and writing from Amazon S3.

How do I use it?

After including the AWS SDK for PHP in your project, use the AmazonS3::register_stream_wrapper() method to register s3:// as a supported stream wrapper for Amazon S3. It's that simple. Amazon S3 file patterns take the following form: s3://bucket/object.

require_once 'AWSSDKforPHP/sdk.class.php';

$s3 = new AmazonS3();
$s3->register_stream_wrapper();

$directory = 's3://my-new-bucket';
$filename = $directory . '/put.txt';
$contents = '';

if (mkdir($directory))
{
    if (file_put_contents($filename, 'This is some sample data.'))
    {
        $handle = fopen($filename, 'rb+');
        $contents = stream_get_contents($handle);
        fclose($handle);
    }

    rmdir($directory);
}

echo $contents;

You may also pass a different protocol name as a parameter to AmazonS3::register_stream_wrapper() if you want to use something besides s3://. Using this technique you can create more than one stream wrapper with different configurations (e.g. for different regions). To do that you just need to create separate instances of the AmazonS3 class, configure them, and then register a stream wrapper for each of them with different protocol names.

require_once 'AWSSDKforPHP/sdk.class.php';

$s3east = new AmazonS3();
$s3east->set_region(AmazonS3::REGION_US_E1);
$s3east->register_stream_wrapper('s3east');
mkdir('s3east://my-easterly-bucket');

$s3west = new AmazonS3();
$s3west->set_region(AmazonS3::REGION_US_W1);
$s3west->register_stream_wrapper('s3west');
mkdir('s3west://my-westerly-bucket');

Tests and usage examples

We are also including tests written in the PHPT format. Not only do these tests show how the software can be used, but any tests submitted back to us should be in this format. These tests will likely fail for you unless you change the bucket names to be globally unique across S3. You can run the tests with pear.

cd S3StreamWrapper/tests;
pear run-tests;

If you have PHPUnit 3.6+ and Xdebug installed, you can generate a code coverage report as follows:

cd S3StreamWrapper/tests && \
phpunit --colors --coverage-html ./_coverage_report . && \
open ./_coverage_report/index.html;

Notes and Known Issues

Successfully tested with

Known issues with

A future version may provide S3-specific implementations of some of these functions (e.g., s3chmod(), s3glob(), s3touch()).