README

# 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

- `stream_lock()` and `stream_cast()` are not currently implemented, and likely won't be.

- Strangely `touch()` doesn’t seem to work. I think this is because of an issue with my implementation of `url_stat()`, but I can’t find any information on the magical combination of parameters that will make this work.

- Using `fopen()` will always open in `rb+` mode. Amazon S3 as a service doesn’t support anything else.

- Because of the way that PHP interacts with the StreamWrapper interface, it’s difficult to optimize for batch requests under the hood. If you need to push or pull data from several objects, you may find that using batch requests with the standard interface has better latency.

- `rmdir()` does not do a force-delete, so you will need to iterate over the files to delete them one-by-one.

- `realpath()`, `glob()`, `chmod()`, `chown()`, `chgrp()`, `tempnam()` and a few other functions don’t support the StreamWrapper interface at the PHP-level because they're designed to work with the (no/low-latency) local file system.

- Support for `ftruncate()` does not exist in any current release of PHP, but is implemented on the PHP trunk for a future release. http://bugs.php.net/53888.

- Since Amazon S3 doesn’t support appending data, it is best to avoid functions that expect or rely on that functionality (e.g. fputcsv()).

## Successfully tested with

- close_dir()
- file_exists()
- file_get_contents()
- file_put_contents()
- fclose()
- feof()
- fflush()
- fgetc()
- fgetcsv()
- fgets()
- fgetss()
- fopen()
- fpassthru()
- fputs()
- fread()
- fseek()
- fstat()
- ftell()
- fwrite()
- is_dir()
- is_file()
- is_readable()
- is_writable()
- mkdir()
- open_dir()
- read_dir()
- rewind()
- rewinddir()
- rmdir()
- scandir()
- stream_get_contents()
- unlink()

## Known issues with

- chgrp()
- chmod()
- chown()
- fputcsv()
- glob()
- realpath()
- tempnam()
- touch()

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