1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538
|
<?xml version="1.0" encoding="UTF-8"?>
<!-- Reviewed: no -->
<sect1 id="zend.service.amazon.s3">
<title>Zend_Service_Amazon_S3</title>
<sect2 id="zend.service.amazon.s3.introduction">
<title>Introduction</title>
<para>
Amazon S3 provides a simple web services interface that can be used to
store and retrieve any amount of data, at any time, from anywhere on
the web. It gives any developer access to the same highly scalable,
reliable, fast, inexpensive data storage infrastructure that Amazon
uses to run its own global network of web sites. The service aims to
maximize benefits of scale and to pass those benefits on to developers.
</para>
</sect2>
<sect2 id="zend.service.amazon.s3.registering">
<title>Registering with Amazon S3</title>
<para>
Before you can get started with <classname>Zend_Service_Amazon_S3</classname>, you must
first register for an account. Please see the
<ulink url="http://aws.amazon.com/s3/faqs/">S3 FAQ</ulink>
page on the Amazon website for more information.
</para>
<para>
After registering, you will receive an application key and a secret key.
You will need both to access the S3 service.
</para>
</sect2>
<sect2 id="zend.service.amazon.s3.apiDocumentation">
<title>API Documentation</title>
<para>
The <classname>Zend_Service_Amazon_S3</classname> class provides the
<acronym>PHP</acronym> wrapper to the Amazon S3 REST interface. Please consult the
<ulink
url="http://developer.amazonwebservices.com/connect/kbcategory.jspa?categoryID=48">Amazon
S3 documentation</ulink> for detailed description of the service. You will need to
be familiar with basic concepts in order to use this service.
</para>
</sect2>
<sect2 id="zend.service.amazon.s3.features">
<title>Features</title>
<para>
<classname>Zend_Service_Amazon_S3</classname> provides the following functionality:
<itemizedlist>
<listitem>
<para>
A single point for configuring your amazon.s3 authentication
credentials that can be used across the amazon.s3 namespaces.
</para>
</listitem>
<listitem>
<para>
A proxy object that is more convenient to use than an
<acronym>HTTP</acronym> client alone, mostly removing the need to manually
construct <acronym>HTTP</acronym> POST requests to access the REST service.
</para>
</listitem>
<listitem>
<para>
A response wrapper that parses each response body and throws an
exception if an error occurred, alleviating the need to repeatedly
check the success of many commands.
</para>
</listitem>
<listitem>
<para>
Additional convenience methods for some of the more common operations.
</para>
</listitem>
</itemizedlist>
</para>
</sect2>
<sect2 id="zend.service.amazon.s3.storing-your-first">
<title>Getting Started</title>
<para>
Once you have registered with Amazon S3, you're ready to store your first
data object on the S3. The objects on S3 are stored in containers, called
"buckets". Bucket names are unique on S3, and each user can have no more than
100 buckets simultaneously. Each bucket can contain unlimited amount of objects,
identified by name.
</para>
<para>
The following example demonstrates creating a bucket, storing and retrieving the data.
</para>
<example id="zend.service.amazon.s3.storing-your-first.example">
<title>Zend_Service_Amazon_S3 Usage Example</title>
<programlisting language="php"><![CDATA[
require_once 'Zend/Service/Amazon/S3.php';
$s3 = new Zend_Service_Amazon_S3($my_aws_key, $my_aws_secret_key);
$s3->createBucket("my-own-bucket");
$s3->putObject("my-own-bucket/myobject", "somedata");
echo $s3->getObject("my-own-bucket/myobject");
]]></programlisting>
</example>
<para>
Since <classname>Zend_Service_Amazon_S3</classname> service requires authentication,
you should pass your credentials (AWS key and secret key) to the constructor.
If you only use one account, you can set default credentials for the service:
</para>
<programlisting language="php"><![CDATA[
require_once 'Zend/Service/Amazon/S3.php';
Zend_Service_Amazon_S3::setKeys($my_aws_key, $my_aws_secret_key);
$s3 = new Zend_Service_Amazon_S3();
]]></programlisting>
</sect2>
<sect2 id="zend.service.amazon.s3.buckets">
<title>Bucket operations</title>
<para>
All objects in S3 system are stored in buckets. Bucket has to be created
before any storage operation. Bucket name is unique in the system, so
you can not have bucket named the same as someone else's bucket.
</para>
<para>
Bucket name can contain lowercase letters, digits, periods (.), underscores (_), and
dashes (-). No other symbols allowed. Bucket name should start with letter or digit, and
be 3 to 255 characters long. Names looking like an IP address (e.g. "192.168.16.255")
are not allowed.
</para>
<itemizedlist>
<listitem>
<para>
<methodname>createBucket()</methodname> creates a new bucket.
</para>
</listitem>
<listitem>
<para>
<methodname>cleanBucket()</methodname> removes all objects that are contained in
a bucket.
</para>
</listitem>
<listitem>
<para>
<methodname>removeBucket()</methodname> removes the bucket from the system. The
bucket should be empty to be removed.
</para>
<example id="zend.service.amazon.s3.buckets.remove.example">
<title>Zend_Service_Amazon_S3 Bucket Removal Example</title>
<programlisting language="php"><![CDATA[
require_once 'Zend/Service/Amazon/S3.php';
$s3 = new Zend_Service_Amazon_S3($my_aws_key, $my_aws_secret_key);
$s3->cleanBucket("my-own-bucket");
$s3->removeBucket("my-own-bucket");
]]></programlisting>
</example>
</listitem>
<listitem>
<para>
<methodname>getBuckets()</methodname> returns the list of the names of all
buckets belonging to the user.
</para>
<example id="zend.service.amazon.s3.buckets.list.example">
<title>Zend_Service_Amazon_S3 Bucket Listing Example</title>
<programlisting language="php"><![CDATA[
require_once 'Zend/Service/Amazon/S3.php';
$s3 = new Zend_Service_Amazon_S3($my_aws_key, $my_aws_secret_key);
$list = $s3->getBuckets();
foreach($list as $bucket) {
echo "I have bucket $bucket\n";
}
]]></programlisting>
</example>
</listitem>
<listitem>
<para>
<methodname>isBucketAvailable()</methodname> check if the bucket exists and
returns <constant>TRUE</constant> if it does.
</para>
</listitem>
</itemizedlist>
</sect2>
<sect2 id="zend.service.amazon.s3.objects">
<title>Object operations</title>
<para>
The object is the basic storage unit in S3. Object stores unstructured data, which can
be any size up to 4 gigabytes. There's no limit on how many objects can be stored on the
system.
</para>
<para>
The object are contained in buckets. Object is identified by name, which can be any
utf-8 string. It is common to use hierarchical names (such as
<code>Pictures/Myself/CodingInPHP.jpg</code>) to organise object names. Object name is
prefixed with bucket name when using object functions, so for object "mydata" in bucket
"my-own-bucket" the name would be <code>my-own-bucket/mydata</code>.
</para>
<para>
Objects can be replaced (by rewriting new data with the same key) or deleted, but not
modified, appended, etc. Object is always stored whole.
</para>
<para>
By default, all objects are private and can be accessed only by their owner. However, it
is possible to specify object with public access, in which case it will be available
through the <acronym>URL</acronym>:
<code>http://s3.amazonaws.com/[bucket-name]/[object-name]</code>.
</para>
<itemizedlist>
<listitem>
<para>
<methodname>putObject($object, $data, $meta)</methodname> created an object with
name <varname>$object</varname> (should contain the bucket name as prefix!)
having <varname>$data</varname> as its content.
</para>
<para>
Optional <varname>$meta</varname> parameter is the array of metadata, which
currently supports the following parameters as keys:
</para>
<variablelist>
<varlistentry>
<term><constant>S3_CONTENT_TYPE_HEADER</constant></term>
<listitem>
<para>
<acronym>MIME</acronym> content type of the data. If not specified,
the type will be guessed according to the file extension of the
object name.
</para>
</listitem>
</varlistentry>
<varlistentry>
<term><constant>S3_ACL_HEADER</constant></term>
<listitem>
<para>
The access to the item. Following access constants can be used:
<variablelist>
<varlistentry>
<term><constant>S3_ACL_PRIVATE</constant></term>
<listitem>
<para>Only the owner has access to the item.</para>
</listitem>
</varlistentry>
<varlistentry>
<term><constant>S3_ACL_PUBLIC_READ</constant></term>
<listitem>
<para>
Anybody can read the object, but only owner can
write. This is setting may be used to store publicly
accessible content.
</para>
</listitem>
</varlistentry>
<varlistentry>
<term><constant>S3_ACL_PUBLIC_WRITE</constant></term>
<listitem>
<para>
Anybody can read or write the object. This policy is
rarely useful.
</para>
</listitem>
</varlistentry>
<varlistentry>
<term><constant>S3_ACL_AUTH_READ</constant></term>
<listitem>
<para>
Only the owner has write access to the item, and
other authenticated S3 users have read access. This
is useful for sharing data between S3 accounts
without exposing them to the public.
</para>
</listitem>
</varlistentry>
</variablelist>
By default, all the items are private.
</para>
<example id="zend.service.amazon.s3.objects.public.example">
<title>Zend_Service_Amazon_S3 Public Object Example</title>
<programlisting language="php"><![CDATA[
require_once 'Zend/Service/Amazon/S3.php';
$s3 = new Zend_Service_Amazon_S3($my_aws_key, $my_aws_secret_key);
$s3->putObject("my-own-bucket/Pictures/Me.png", file_get_contents("me.png"),
array(Zend_Service_Amazon_S3::S3_ACL_HEADER =>
Zend_Service_Amazon_S3::S3_ACL_PUBLIC_READ));
// or:
$s3->putFile("me.png", "my-own-bucket/Pictures/Me.png",
array(Zend_Service_Amazon_S3::S3_ACL_HEADER =>
Zend_Service_Amazon_S3::S3_ACL_PUBLIC_READ));
echo "Go to http://s3.amazonaws.com/my-own-bucket/Pictures/Me.png to see me!\n";
]]></programlisting>
</example>
</listitem>
</varlistentry>
</variablelist>
</listitem>
<listitem>
<para>
<methodname>getObject($object)</methodname> retrieves object data from the
storage by name.
</para>
</listitem>
<listitem>
<para>
<methodname>removeObject($object)</methodname> removes the object from the
storage.
</para>
</listitem>
<listitem>
<para>
<methodname>getInfo($object)</methodname> retrieves the metadata information
about the object. The function will return array with metadata information. Some
of the useful keys are:
<variablelist>
<varlistentry>
<term><code>type</code></term>
<listitem>
<para>The <acronym>MIME</acronym> type of the item.</para>
</listitem>
</varlistentry>
<varlistentry>
<term><code>size</code></term>
<listitem>
<para>The size of the object data.</para>
</listitem>
</varlistentry>
<varlistentry>
<term><code>mtime</code></term>
<listitem>
<para>
UNIX-type timestamp of the last modification for the object.
</para>
</listitem>
</varlistentry>
<varlistentry>
<term><code>etag</code></term>
<listitem>
<para>
The ETag of the data, which is the MD5 hash of the data,
surrounded by quotes (").
</para>
</listitem>
</varlistentry>
</variablelist>
The function will return <constant>FALSE</constant> if the key does not
correspond to any existing object.
</para>
</listitem>
<listitem>
<para>
<methodname>getObjectsByBucket($bucket)</methodname> returns the list of the
object keys, contained in the bucket.
</para>
<example id="zend.service.amazon.s3.objects.list.example">
<title>Zend_Service_Amazon_S3 Object Listing Example</title>
<programlisting language="php"><![CDATA[
require_once 'Zend/Service/Amazon/S3.php';
$s3 = new Zend_Service_Amazon_S3($my_aws_key, $my_aws_secret_key);
$list = $s3->getObjectsByBucket("my-own-bucket");
foreach($list as $name) {
echo "I have $name key:\n";
$data = $s3->getObject("my-own-bucket/$name");
echo "with data: $data\n";
}
]]></programlisting>
</example>
</listitem>
<listitem>
<para>
<methodname>isObjectAvailable($object)</methodname> checks if the object with
given name exists.
</para>
</listitem>
<listitem>
<para>
<methodname>putFile($path, $object, $meta)</methodname> puts the content of the
file in <varname>$path</varname> into the object named
<varname>$object</varname>.
</para>
<para>
The optional <varname>$meta</varname> argument is the same as for
<code>putObject</code>. If the content type is omitted, it will be guessed
basing on the source file name.
</para>
</listitem>
</itemizedlist>
</sect2>
<sect2 id="zend.service.amazon.s3.streaming">
<title>Data Streaming</title>
<para>
It is possible to get and put objects using not stream data held in memory but files or
<acronym>PHP</acronym> streams. This is especially useful when file sizes are large in
order not to overcome memory limits.
</para>
<para>
To receive object using streaming, use method
<methodname>getObjectStream($object, $filename)</methodname>. This method will return
<classname>Zend_Http_Response_Stream</classname>, which can be used as described in
<link linkend="zend.http.client.streaming">HTTP Client Data Streaming</link> section.
<example id="zend.service.amazon.s3.streaming.example1">
<title>Zend_Service_Amazon_S3 Data Streaming Example</title>
<programlisting language="php"><![CDATA[
$response = $amazon->getObjectStream("mybycket/zftest");
// copy file
copy($response->getStreamName(), "my/downloads/file");
// use stream
$fp = fopen("my/downloads/file2", "w");
stream_copy_to_stream($response->getStream(), $fp);
]]></programlisting>
</example>
</para>
<para>
Second parameter for <methodname>getObjectStream()</methodname> is optional and
specifies target file to write the data. If not specified, temporary file is used, which
will be deleted after the response object is destroyed.
</para>
<para>
To send object using streaming, use <methodname>putFileStream()</methodname> which has
the same signature as <methodname>putFile()</methodname> but will use streaming and not
read the file into memory.
</para>
<para>
Also, you can pass stream resource to <methodname>putObject()</methodname> method data
parameter, in which case the data will be read from the stream when sending the request
to the server.
</para>
</sect2>
<sect2 id="zend.service.amazon.s3.streams">
<title>Stream wrapper</title>
<para>
In addition to the interfaces described above,
<classname>Zend_Service_Amazon_S3</classname> also supports operating as a stream
wrapper. For this, you need to register the client object as the stream wrapper:
</para>
<example id="zend.service.amazon.s3.streams.example">
<title>Zend_Service_Amazon_S3 Streams Example</title>
<programlisting language="php"><![CDATA[
require_once 'Zend/Service/Amazon/S3.php';
$s3 = new Zend_Service_Amazon_S3($my_aws_key, $my_aws_secret_key);
$s3->registerStreamWrapper("s3");
mkdir("s3://my-own-bucket");
file_put_contents("s3://my-own-bucket/testdata", "mydata");
echo file_get_contents("s3://my-own-bucket/testdata");
]]></programlisting>
</example>
<para>
Directory operations (<code>mkdir</code>, <code>rmdir</code>, <code>opendir</code>,
etc.) will operate on buckets and thus their arguments should be of the form of
<code>s3://bucketname</code>. File operations operate on objects. Object creation,
reading, writing, deletion, stat and directory listing is supported.
</para>
</sect2>
</sect1>
|