244 lines
11 KiB
HTML
244 lines
11 KiB
HTML
|
<!DOCTYPE html>
|
||
|
<html>
|
||
|
<head>
|
||
|
<meta http-equiv="Content-type" content="text/html; charset=utf-8">
|
||
|
<title>README</title>
|
||
|
<style type="text/css" media="screen">
|
||
|
body {
|
||
|
font: 18px/1.5em 'Book Antiqua', 'Palatino Linotype', Palatino, 'Minion Pro', Cambria, Georgia, serif;
|
||
|
padding: 50px 10%;
|
||
|
max-width: 1000px;
|
||
|
margin: 0 auto;
|
||
|
color: #080000;
|
||
|
background-color: #fbfbf9;
|
||
|
}
|
||
|
code {
|
||
|
font: 13px/1.4em Monaco, monospace;
|
||
|
background-color: #f3f3f3;
|
||
|
color: #444;
|
||
|
padding: 1px 2px;
|
||
|
|
||
|
border-radius: 2px;
|
||
|
-webkit-border-radius: 2px;
|
||
|
-moz-border-radius: 2px;
|
||
|
}
|
||
|
pre {
|
||
|
border: 1px solid #ddd;
|
||
|
border-left: 6px solid #c0c0c0;
|
||
|
background-color: #f3f3f3;
|
||
|
color: #444;
|
||
|
font: 13px/1.4em Monaco, monospace;
|
||
|
overflow: auto;
|
||
|
|
||
|
margin: 1em 0 1.5em 0;
|
||
|
padding: 1em;
|
||
|
text-shadow: 0 1px 0 rgba(255, 255, 255, 0.5);
|
||
|
|
||
|
-webkit-border-top-right-radius: 5px;
|
||
|
-webkit-border-bottom-right-radius: 5px;
|
||
|
-moz-border-radius-topright: 5px;
|
||
|
-moz-border-radius-bottomright: 5px;
|
||
|
border-top-right-radius: 5px;
|
||
|
border-bottom-right-radius: 5px;
|
||
|
}
|
||
|
pre code {
|
||
|
font-family: inherit;
|
||
|
font-size: inherit;
|
||
|
background-color: inherit;
|
||
|
color: inherit;
|
||
|
padding: 0;
|
||
|
|
||
|
border-radius: none;
|
||
|
-webkit-border-radius: none;
|
||
|
-moz-border-radius: none;
|
||
|
}
|
||
|
a {
|
||
|
color: #326EA1;
|
||
|
text-decoration: underline;
|
||
|
padding: 1px 2px;
|
||
|
-webkit-transition: background-color 0.15s;
|
||
|
-webkit-transition: color 0.15s;
|
||
|
-moz-transition: background-color 0.15s;
|
||
|
-moz-transition: color 0.15s;
|
||
|
transition: background-color 0.15s;
|
||
|
transition: color 0.15s;
|
||
|
-webkit-border-radius: 2px;
|
||
|
-moz-border-radius: 2px;
|
||
|
border-radius: 2px;
|
||
|
}
|
||
|
a:hover, a.hover {
|
||
|
color: #fff;
|
||
|
background-color: #333;
|
||
|
text-decoration: none;
|
||
|
padding: 1px 2px;
|
||
|
}
|
||
|
a.active {
|
||
|
font-weight: bold;
|
||
|
}
|
||
|
h1, h2, h3, h4, h5, h6 {
|
||
|
padding: 0.8em 0 0.2em 0;
|
||
|
margin: 0;
|
||
|
}
|
||
|
h1 {
|
||
|
margin-top: 30px;
|
||
|
padding: 15px 0 5px 5px;
|
||
|
font-size: 2em;
|
||
|
line-height: 1.3em;
|
||
|
border-bottom: 3px solid #cdcdcc;
|
||
|
}
|
||
|
h2 {
|
||
|
margin-top: 30px;
|
||
|
padding: 15px 0 5px 5px;
|
||
|
font-size: 1.5em;
|
||
|
line-height: 1.3em;
|
||
|
border-bottom: 1px solid #cdcdcc;
|
||
|
}
|
||
|
li {
|
||
|
margin: 0;
|
||
|
padding: 0;
|
||
|
}
|
||
|
em, i {
|
||
|
font-style: italic;
|
||
|
}
|
||
|
strong, b {
|
||
|
font-weight: bold;
|
||
|
color: #000;
|
||
|
}
|
||
|
p {
|
||
|
margin: 0;
|
||
|
padding: 0.5em 0;
|
||
|
}
|
||
|
ul.columns {
|
||
|
-moz-column-count: 3;
|
||
|
-moz-column-gap: 20px;
|
||
|
-webkit-column-count: 3;
|
||
|
-webkit-column-gap: 20px;
|
||
|
column-count: 3;
|
||
|
column-gap: 20px;
|
||
|
}
|
||
|
</style>
|
||
|
</head>
|
||
|
<body>
|
||
|
<h1>S3 Stream Wrapper</h1>
|
||
|
<p>The <strong>S3 Stream Wrapper</strong> is a stream wrapper interface for PHP that provides access to Amazon S3 using PHP’s standard File System API functions.</p>
|
||
|
|
||
|
<h2>What issues does this address?</h2>
|
||
|
<p>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.</p>
|
||
|
<p>What if we could simplify this process so that the updates required to make an existing application cloud-backed would be very minimal?</p>
|
||
|
|
||
|
<h2>Proposed solution</h2>
|
||
|
<p>PHP provides an interface for solving this exact kind of problem, called the <a href="http://php.net/streamwrapper">Stream Wrapper</a> interface. By writing a class that implements this interface and <a href="http://php.net/manual/en/function.stream-wrapper-register.php">registering it as a handler</a> 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.</p>
|
||
|
|
||
|
<h2>How do I use it?</h2>
|
||
|
<p>After including the AWS SDK for PHP in your project, use the <code>AmazonS3::register_stream_wrapper()</code> method to register <code>s3://</code> as a <a href="http://php.net/manual/en/wrappers.php">supported stream wrapper</a> for Amazon S3. <em>It's that simple</em>. Amazon S3 file patterns take the following form: <code>s3://bucket/object</code>.</p>
|
||
|
|
||
|
<pre><code>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;</code></pre>
|
||
|
|
||
|
<p>You may also pass a different protocol name as a parameter to <code>AmazonS3::register_stream_wrapper()</code> if you want to use something besides <code>s3://</code>. 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 <code>AmazonS3</code> class, configure them, and then register a stream wrapper for each of them with different protocol names.</p>
|
||
|
|
||
|
<pre><code>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');</code></pre>
|
||
|
|
||
|
<h2>Tests and usage examples</h2>
|
||
|
<p>We are also including tests written in the <a href="http://qa.php.net/phpt_details.php">PHPT</a> 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 <code>pear</code>.</p>
|
||
|
<pre><code>cd S3StreamWrapper/tests;
|
||
|
pear run-tests;</code></pre>
|
||
|
<p>If you have <a href="http://phpunit.de">PHPUnit</a> 3.6+ and <a href="http://xdebug.org">Xdebug</a> installed, you can generate a code coverage report as follows:</p>
|
||
|
<pre><code>cd S3StreamWrapper/tests && \
|
||
|
phpunit --colors --coverage-html ./_coverage_report . && \
|
||
|
open ./_coverage_report/index.html;</code></pre>
|
||
|
|
||
|
<h2>Notes and Known Issues</h2>
|
||
|
<ul>
|
||
|
<li><p><code>stream_lock()</code> and <code>stream_cast()</code> are not currently implemented, and likely won't be.</p></li>
|
||
|
<li><p>Strangely <code>touch()</code> doesn’t seem to work. I think this is because of an issue with my implementation of <code>url_stat()</code>, but I can’t find any information on the magical combination of parameters that will make this work.</p></li>
|
||
|
<li><p>Using <code>fopen()</code> will always open in <code>rb+</code> mode. Amazon S3 as a service doesn’t support anything else.</p></li>
|
||
|
<li><p>Because of the way that PHP interacts with the <a href="http://php.net/manual/en/class.streamwrapper.php">StreamWrapper</a> 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 <a href="http://docs.amazonwebservices.com/AWSSDKforPHP/latest/#m=CFRuntime/batch">batch requests</a> with the <a href="http://docs.amazonwebservices.com/AWSSDKforPHP/latest/#i=AmazonS3">standard interface</a> has better latency.</p></li>
|
||
|
<li><p><code>rmdir()</code> does not do a force-delete, so you will need to iterate over the files to delete them one-by-one.</p></li>
|
||
|
<li><p><code>realpath()</code>, <code>glob()</code>, <code>chmod()</code>, <code>chown()</code>, <code>chgrp()</code>, <code>tempnam()</code> 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.</p></li>
|
||
|
<li><p>Support for <code>ftruncate()</code> does not exist in any current release of PHP, but is implemented on the PHP trunk for a future release. <a href="http://bugs.php.net/53888">http://bugs.php.net/53888</a>.</p></li>
|
||
|
<li><p>Since <a href="http://docs.amazonwebservices.com/AmazonS3/latest/gsg/WorkingWithS3.html#WriteObject">Amazon S3 doesn’t support appending data</a>, it is best to avoid functions that expect or rely on that functionality (e.g. <a href="http://php.net/fputcsv">fputcsv()</a>).</p></li>
|
||
|
</ul>
|
||
|
|
||
|
<h2>Successfully tested with</h2>
|
||
|
<ul class="columns">
|
||
|
<li><a href="http://php.net/close_dir">close_dir()</a></li>
|
||
|
<li><a href="http://php.net/file_exists">file_exists()</a></li>
|
||
|
<li><a href="http://php.net/file_get_contents">file_get_contents()</a></li>
|
||
|
<li><a href="http://php.net/file_put_contents">file_put_contents()</a></li>
|
||
|
<li><a href="http://php.net/fclose">fclose()</a></li>
|
||
|
<li><a href="http://php.net/feof">feof()</a></li>
|
||
|
<li><a href="http://php.net/fflush">fflush()</a></li>
|
||
|
<li><a href="http://php.net/fgetc">fgetc()</a></li>
|
||
|
<li><a href="http://php.net/fgetcsv">fgetcsv()</a></li>
|
||
|
<li><a href="http://php.net/fgets">fgets()</a></li>
|
||
|
<li><a href="http://php.net/fgetss">fgetss()</a></li>
|
||
|
<li><a href="http://php.net/fopen">fopen()</a></li>
|
||
|
<li><a href="http://php.net/fpassthru">fpassthru()</a></li>
|
||
|
<li><a href="http://php.net/fputs">fputs()</a></li>
|
||
|
<li><a href="http://php.net/fread">fread()</a></li>
|
||
|
<li><a href="http://php.net/fseek">fseek()</a></li>
|
||
|
<li><a href="http://php.net/fstat">fstat()</a></li>
|
||
|
<li><a href="http://php.net/ftell">ftell()</a></li>
|
||
|
<li><a href="http://php.net/fwrite">fwrite()</a></li>
|
||
|
<li><a href="http://php.net/is_dir">is_dir()</a></li>
|
||
|
<li><a href="http://php.net/is_file">is_file()</a></li>
|
||
|
<li><a href="http://php.net/is_readable">is_readable()</a></li>
|
||
|
<li><a href="http://php.net/is_writable">is_writable()</a></li>
|
||
|
<li><a href="http://php.net/mkdir">mkdir()</a></li>
|
||
|
<li><a href="http://php.net/open_dir">open_dir()</a></li>
|
||
|
<li><a href="http://php.net/read_dir">read_dir()</a></li>
|
||
|
<li><a href="http://php.net/rewind">rewind()</a></li>
|
||
|
<li><a href="http://php.net/rewinddir">rewinddir()</a></li>
|
||
|
<li><a href="http://php.net/rmdir">rmdir()</a></li>
|
||
|
<li><a href="http://php.net/scandir">scandir()</a></li>
|
||
|
<li><a href="http://php.net/stream_get_contents">stream_get_contents()</a></li>
|
||
|
<li><a href="http://php.net/unlink">unlink()</a></li>
|
||
|
</ul>
|
||
|
|
||
|
<h2>Known issues with</h2>
|
||
|
<ul>
|
||
|
<li><a href="http://php.net/chgrp">chgrp()</a></li>
|
||
|
<li><a href="http://php.net/chmod">chmod()</a></li>
|
||
|
<li><a href="http://php.net/chown">chown()</a></li>
|
||
|
<li><a href="http://php.net/fputcsv">fputcsv()</a></li>
|
||
|
<li><a href="http://php.net/glob">glob()</a></li>
|
||
|
<li><a href="http://php.net/realpath">realpath()</a></li>
|
||
|
<li><a href="http://php.net/tempnam">tempnam()</a></li>
|
||
|
<li><a href="http://php.net/touch">touch()</a></li>
|
||
|
</ul>
|
||
|
|
||
|
<p>A future version <em>may</em> provide S3-specific implementations of some of these functions (e.g., <code>s3chmod()</code>, <code>s3glob()</code>, <code>s3touch()</code>).</p>
|
||
|
</body>
|
||
|
</html>
|