Zend_Service_Amazon_S3.xml 21 KB

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