Subversion-Projekte lars-tiefland.php_share

Revision

Details | Letzte Änderung | Log anzeigen | RSS feed

Revision Autor Zeilennr. Zeile
1 lars 1
<!DOCTYPE html>
2
<html>
3
	<head>
4
		<meta http-equiv="Content-type" content="text/html; charset=utf-8">
5
		<title>README</title>
6
		<style type="text/css" media="screen">
7
		body {
8
			font: 18px/1.5em 'Book Antiqua', 'Palatino Linotype', Palatino, 'Minion Pro', Cambria, Georgia, serif;
9
			padding: 50px 10%;
10
			max-width: 1000px;
11
			margin: 0 auto;
12
			color: #080000;
13
			background-color: #fbfbf9;
14
		}
15
		code {
16
			font: 13px/1.4em Monaco, monospace;
17
			background-color: #f3f3f3;
18
			color: #444;
19
			padding: 1px 2px;
20
 
21
			border-radius: 2px;
22
			-webkit-border-radius: 2px;
23
			-moz-border-radius: 2px;
24
		}
25
		pre {
26
			border: 1px solid #ddd;
27
			border-left: 6px solid #c0c0c0;
28
			background-color: #f3f3f3;
29
			color: #444;
30
			font: 13px/1.4em Monaco, monospace;
31
			overflow: auto;
32
 
33
			margin: 1em 0 1.5em 0;
34
			padding: 1em;
35
			text-shadow: 0 1px 0 rgba(255, 255, 255, 0.5);
36
 
37
			-webkit-border-top-right-radius: 5px;
38
			-webkit-border-bottom-right-radius: 5px;
39
			-moz-border-radius-topright: 5px;
40
			-moz-border-radius-bottomright: 5px;
41
			border-top-right-radius: 5px;
42
			border-bottom-right-radius: 5px;
43
		}
44
		pre code {
45
			font-family: inherit;
46
			font-size: inherit;
47
			background-color: inherit;
48
			color: inherit;
49
			padding: 0;
50
 
51
			border-radius: none;
52
			-webkit-border-radius: none;
53
			-moz-border-radius: none;
54
		}
55
		a {
56
			color: #326EA1;
57
			text-decoration: underline;
58
			padding: 1px 2px;
59
			-webkit-transition: background-color 0.15s;
60
			-webkit-transition: color 0.15s;
61
			-moz-transition: background-color 0.15s;
62
			-moz-transition: color 0.15s;
63
			transition: background-color 0.15s;
64
			transition: color 0.15s;
65
			-webkit-border-radius: 2px;
66
			-moz-border-radius: 2px;
67
			border-radius: 2px;
68
		}
69
		a:hover, a.hover {
70
			color: #fff;
71
			background-color: #333;
72
			text-decoration: none;
73
			padding: 1px 2px;
74
		}
75
		a.active {
76
			font-weight: bold;
77
		}
78
		h1, h2, h3, h4, h5, h6 {
79
			padding: 0.8em 0 0.2em 0;
80
			margin: 0;
81
		}
82
		h1 {
83
			margin-top: 30px;
84
			padding: 15px 0 5px 5px;
85
			font-size: 2em;
86
			line-height: 1.3em;
87
			border-bottom: 3px solid #cdcdcc;
88
		}
89
		h2 {
90
			margin-top: 30px;
91
			padding: 15px 0 5px 5px;
92
			font-size: 1.5em;
93
			line-height: 1.3em;
94
			border-bottom: 1px solid #cdcdcc;
95
		}
96
		li {
97
			margin: 0;
98
			padding: 0;
99
		}
100
		em, i {
101
			font-style: italic;
102
		}
103
		strong, b {
104
			font-weight: bold;
105
			color: #000;
106
		}
107
		p {
108
			margin: 0;
109
			padding: 0.5em 0;
110
		}
111
		ul.columns {
112
			-moz-column-count: 3;
113
			-moz-column-gap: 20px;
114
			-webkit-column-count: 3;
115
			-webkit-column-gap: 20px;
116
			column-count: 3;
117
			column-gap: 20px;
118
		}
119
		</style>
120
	</head>
121
	<body>
122
		<h1>S3 Stream Wrapper</h1>
123
		<p>The <strong>S3 Stream Wrapper</strong> is a stream wrapper interface for PHP that provides access to Amazon S3 using PHP&#8217;s standard File System API functions.</p>
124
 
125
		<h2>What issues does this address?</h2>
126
		<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>
127
		<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>
128
 
129
		<h2>Proposed solution</h2>
130
		<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>
131
 
132
		<h2>How do I use it?</h2>
133
		<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>
134
 
135
		<pre><code>require_once 'AWSSDKforPHP/sdk.class.php';
136
 
137
$s3 = new AmazonS3();
138
$s3->register_stream_wrapper();
139
 
140
$directory = 's3://my-new-bucket';
141
$filename = $directory . '/put.txt';
142
$contents = '';
143
 
144
if (mkdir($directory))
145
{
146
    if (file_put_contents($filename, 'This is some sample data.'))
147
    {
148
        $handle = fopen($filename, 'rb+');
149
        $contents = stream_get_contents($handle);
150
        fclose($handle);
151
    }
152
 
153
    rmdir($directory);
154
}
155
 
156
echo $contents;</code></pre>
157
 
158
		<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>
159
 
160
		<pre><code>require_once 'AWSSDKforPHP/sdk.class.php';
161
 
162
$s3east = new AmazonS3();
163
$s3east->set_region(AmazonS3::REGION_US_E1);
164
$s3east->register_stream_wrapper('s3east');
165
mkdir('s3east://my-easterly-bucket');
166
 
167
$s3west = new AmazonS3();
168
$s3west->set_region(AmazonS3::REGION_US_W1);
169
$s3west->register_stream_wrapper('s3west');
170
mkdir('s3west://my-westerly-bucket');</code></pre>
171
 
172
		<h2>Tests and usage examples</h2>
173
		<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>
174
		<pre><code>cd S3StreamWrapper/tests;
175
pear run-tests;</code></pre>
176
		<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>
177
		<pre><code>cd S3StreamWrapper/tests && \
178
phpunit --colors --coverage-html ./_coverage_report . && \
179
open ./_coverage_report/index.html;</code></pre>
180
 
181
		<h2>Notes and Known Issues</h2>
182
		<ul>
183
			<li><p><code>stream_lock()</code> and <code>stream_cast()</code> are not currently implemented, and likely won't be.</p></li>
184
			<li><p>Strangely <code>touch()</code> doesn&#8217;t seem to work. I think this is because of an issue with my implementation of <code>url_stat()</code>, but I can&#8217;t find any information on the magical combination of parameters that will make this work.</p></li>
185
			<li><p>Using <code>fopen()</code> will always open in <code>rb+</code> mode. Amazon S3 as a service doesn&#8217;t support anything else.</p></li>
186
			<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&#8217;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>
187
			<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>
188
			<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&#8217;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>
189
			<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>
190
			<li><p>Since <a href="http://docs.amazonwebservices.com/AmazonS3/latest/gsg/WorkingWithS3.html#WriteObject">Amazon S3 doesn&#8217;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>
191
		</ul>
192
 
193
		<h2>Successfully tested with</h2>
194
		<ul class="columns">
195
			<li><a href="http://php.net/close_dir">close_dir()</a></li>
196
			<li><a href="http://php.net/file_exists">file_exists()</a></li>
197
			<li><a href="http://php.net/file_get_contents">file_get_contents()</a></li>
198
			<li><a href="http://php.net/file_put_contents">file_put_contents()</a></li>
199
			<li><a href="http://php.net/fclose">fclose()</a></li>
200
			<li><a href="http://php.net/feof">feof()</a></li>
201
			<li><a href="http://php.net/fflush">fflush()</a></li>
202
			<li><a href="http://php.net/fgetc">fgetc()</a></li>
203
			<li><a href="http://php.net/fgetcsv">fgetcsv()</a></li>
204
			<li><a href="http://php.net/fgets">fgets()</a></li>
205
			<li><a href="http://php.net/fgetss">fgetss()</a></li>
206
			<li><a href="http://php.net/fopen">fopen()</a></li>
207
			<li><a href="http://php.net/fpassthru">fpassthru()</a></li>
208
			<li><a href="http://php.net/fputs">fputs()</a></li>
209
			<li><a href="http://php.net/fread">fread()</a></li>
210
			<li><a href="http://php.net/fseek">fseek()</a></li>
211
			<li><a href="http://php.net/fstat">fstat()</a></li>
212
			<li><a href="http://php.net/ftell">ftell()</a></li>
213
			<li><a href="http://php.net/fwrite">fwrite()</a></li>
214
			<li><a href="http://php.net/is_dir">is_dir()</a></li>
215
			<li><a href="http://php.net/is_file">is_file()</a></li>
216
			<li><a href="http://php.net/is_readable">is_readable()</a></li>
217
			<li><a href="http://php.net/is_writable">is_writable()</a></li>
218
			<li><a href="http://php.net/mkdir">mkdir()</a></li>
219
			<li><a href="http://php.net/open_dir">open_dir()</a></li>
220
			<li><a href="http://php.net/read_dir">read_dir()</a></li>
221
			<li><a href="http://php.net/rewind">rewind()</a></li>
222
			<li><a href="http://php.net/rewinddir">rewinddir()</a></li>
223
			<li><a href="http://php.net/rmdir">rmdir()</a></li>
224
			<li><a href="http://php.net/scandir">scandir()</a></li>
225
			<li><a href="http://php.net/stream_get_contents">stream_get_contents()</a></li>
226
			<li><a href="http://php.net/unlink">unlink()</a></li>
227
		</ul>
228
 
229
		<h2>Known issues with</h2>
230
		<ul>
231
			<li><a href="http://php.net/chgrp">chgrp()</a></li>
232
			<li><a href="http://php.net/chmod">chmod()</a></li>
233
			<li><a href="http://php.net/chown">chown()</a></li>
234
			<li><a href="http://php.net/fputcsv">fputcsv()</a></li>
235
			<li><a href="http://php.net/glob">glob()</a></li>
236
			<li><a href="http://php.net/realpath">realpath()</a></li>
237
			<li><a href="http://php.net/tempnam">tempnam()</a></li>
238
			<li><a href="http://php.net/touch">touch()</a></li>
239
		</ul>
240
 
241
		<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>
242
	</body>
243
</html>