Zend_Service_Amazon_S3.xml 19 KB

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