Zend_Cloud_StorageService.xml 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662
  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <!-- Reviewed: no -->
  3. <sect1 id="zend.cloud.storageservice">
  4. <title>StorageService Introduction</title>
  5. <para>
  6. The storage service in the Simple Cloud API implements a basic interface for file storage on
  7. the cloud. The files have no internal structure as far as the service is concerned, and are
  8. identified by a string key that is analogous to a filepath on a filesystem.
  9. </para>
  10. <sect2 id="zend.cloud.storageservice.adapters">
  11. <title>StorageService Adapters</title>
  12. <para>
  13. The interface <classname>Zend_Cloud_StorageService_Adapter</classname> defines methods
  14. that each concrete storage service adapter must implement. The following adapters are
  15. shipped with the Simple Cloud API:
  16. </para>
  17. <itemizedlist>
  18. <listitem>
  19. <para>
  20. <ulink
  21. url="http://aws.amazon.com/s3/"><classname>Zend_Cloud_StorageService_Adapter_S3</classname></ulink>
  22. </para>
  23. </listitem>
  24. <listitem>
  25. <para>
  26. <ulink
  27. url="http://msdn.microsoft.com/en-us/library/dd179423.aspx"><classname>Zend_Cloud_StorageService_Adapter_WindowsAzure</classname></ulink>
  28. </para>
  29. </listitem>
  30. <listitem>
  31. <para>
  32. <classname>Zend_Cloud_StorageService_Adapter_FileSystem</classname>
  33. </para>
  34. </listitem>
  35. </itemizedlist>
  36. <para>
  37. To create the service object, call the static method
  38. <methodname>Zend_Cloud_StorageService_Factory::getAdapter()</methodname>, which accepts
  39. either an array or a <classname>Zend_Config</classname> object. The key named
  40. <varname>storage_adapter</varname> should specify the concrete adapter class.
  41. Adapter-specific keys may also be passed in this configuration parameter.
  42. </para>
  43. <example id="zend.cloud.storageservice.factory.example">
  44. <title>Using the StorageService Factory</title>
  45. <programlisting language="php"><![CDATA[
  46. $storage = Zend_Cloud_StorageService_Factory::getAdapter(array(
  47. Zend_Cloud_StorageService_Factory::STORAGE_ADAPTER_KEY => 'Zend_Cloud_StorageService_Adapter_S3',
  48. Zend_Cloud_StorageService_Adapter_S3::AWS_ACCESS_KEY => $amazonKey,
  49. Zend_Cloud_StorageService_Adapter_S3::AWS_SECRET_KEY => $amazonSecret,
  50. ));
  51. ]]></programlisting>
  52. </example>
  53. <sect3 id="zend.cloud.storageservice.adapters.options">
  54. <title>StorageService Adapter Options</title>
  55. <table frame='all' id="zend.cloud.storageservice.options.s3">
  56. <title>Zend_Cloud_StorageService_Adapter_S3 options</title>
  57. <tgroup cols="5">
  58. <thead>
  59. <row>
  60. <entry>Option key</entry>
  61. <entry>Description</entry>
  62. <entry>Used in</entry>
  63. <entry>Required</entry>
  64. <entry>Default</entry>
  65. </row>
  66. </thead>
  67. <tbody>
  68. <row>
  69. <entry>aws_accesskey</entry>
  70. <entry>Amazon AWS access key</entry>
  71. <entry>Constructor</entry>
  72. <entry>Yes</entry>
  73. <entry>None</entry>
  74. </row>
  75. <row>
  76. <entry>aws_secretkey</entry>
  77. <entry>Amazon AWS secret key</entry>
  78. <entry>Constructor</entry>
  79. <entry>Yes</entry>
  80. <entry>None</entry>
  81. </row>
  82. <row>
  83. <entry>bucket_name</entry>
  84. <entry>The name of the S3 bucket for this item</entry>
  85. <entry>
  86. Used in the constructor to set the default bucket for the
  87. instantiated service. This option can also be specified in any of
  88. the item access operations.
  89. </entry>
  90. <entry>Yes</entry>
  91. <entry>None</entry>
  92. </row>
  93. <row>
  94. <entry>bucket_as_domain</entry>
  95. <entry>
  96. Indicates that the bucket name is part of the domain name
  97. </entry>
  98. <entry>
  99. Used in constructor to set the default behavior for the instantiated
  100. service. This option can also be specified in any of the item access
  101. operations.
  102. </entry>
  103. <entry>No</entry>
  104. <entry>False</entry>
  105. </row>
  106. <row>
  107. <entry>metadata</entry>
  108. <entry>Array of metadata to associate with the item</entry>
  109. <entry><methodname>storeItem()</methodname></entry>
  110. <entry>No</entry>
  111. <entry>None</entry>
  112. </row>
  113. <row>
  114. <entry>fetch_stream</entry>
  115. <entry>
  116. Indicates whether the response is stream, and not a string
  117. <note>
  118. <para>
  119. See the <classname>Zend_Service_Amazon_S3</classname>
  120. documentation for more about handling streamed responses)
  121. </para>
  122. </note>
  123. </entry>
  124. <entry><methodname>fetchItem()</methodname></entry>
  125. <entry>No</entry>
  126. <entry>False</entry>
  127. </row>
  128. <row>
  129. <entry>http_adapter</entry>
  130. <entry>HTTP adapter to use in all access operations</entry>
  131. <entry>Constructor</entry>
  132. <entry>No</entry>
  133. <entry><classname>Zend_Http_Client_Adapter_Socket</classname></entry>
  134. </row>
  135. </tbody>
  136. </tgroup>
  137. </table>
  138. <table frame='all' id="zend.cloud.storageservice.options.azure">
  139. <title>Zend_Cloud_StorageService_Adapter_WindowsAzure options</title>
  140. <tgroup cols="5">
  141. <thead>
  142. <row>
  143. <entry>Option key</entry>
  144. <entry>Description</entry>
  145. <entry>Used in</entry>
  146. <entry>Required</entry>
  147. <entry>Default</entry>
  148. </row>
  149. </thead>
  150. <tbody>
  151. <row>
  152. <entry>storage_accountname</entry>
  153. <entry>Windows Azure account name</entry>
  154. <entry>Constructor</entry>
  155. <entry>Yes</entry>
  156. <entry>None</entry>
  157. </row>
  158. <row>
  159. <entry>storage_accountkey</entry>
  160. <entry>Windows Azure account key</entry>
  161. <entry>Constructor</entry>
  162. <entry>Yes</entry>
  163. <entry>None</entry>
  164. </row>
  165. <row>
  166. <entry>storage_container</entry>
  167. <entry>Container to use for this storage object</entry>
  168. <entry>Constructor</entry>
  169. <entry>Yes</entry>
  170. <entry>None</entry>
  171. </row>
  172. <row>
  173. <entry>storage_host</entry>
  174. <entry>Windows Azure access host</entry>
  175. <entry>Constructor</entry>
  176. <entry>Yes</entry>
  177. <entry><varname>blob.core.windows.net</varname></entry>
  178. </row>
  179. <row>
  180. <entry>storage_proxy_host</entry>
  181. <entry>Proxy hostname</entry>
  182. <entry>Constructor</entry>
  183. <entry>No</entry>
  184. <entry>None</entry>
  185. </row>
  186. <row>
  187. <entry>storage_proxy_port</entry>
  188. <entry>Proxy port</entry>
  189. <entry>Constructor</entry>
  190. <entry>No</entry>
  191. <entry>8080</entry>
  192. </row>
  193. <row>
  194. <entry>storage_proxy_credentials</entry>
  195. <entry>Proxy credentials</entry>
  196. <entry>Constructor</entry>
  197. <entry>No</entry>
  198. <entry>None</entry>
  199. </row>
  200. <row>
  201. <entry>http_adapter</entry>
  202. <entry>HTTP adapter to use in all access operations</entry>
  203. <entry>Constructor</entry>
  204. <entry>No</entry>
  205. <entry><classname>Zend_Http_Client_Adapter_Socket</classname></entry>
  206. </row>
  207. <row>
  208. <entry>returntype</entry>
  209. <entry>
  210. How to return the results.
  211. <itemizedlist>
  212. <listitem>
  213. <para>For <methodname>fetchItem()</methodname>:</para>
  214. <variablelist>
  215. <varlistentry>
  216. <term><constant>RETURN_STRING</constant></term>
  217. <listitem><para>
  218. Return the data as strings.
  219. </para></listitem>
  220. </varlistentry>
  221. <varlistentry>
  222. <term><constant>RETURN_PATH</constant></term>
  223. <listitem>
  224. <para>
  225. save data on disk in temp file, return path
  226. name
  227. </para>
  228. </listitem>
  229. </varlistentry>
  230. <varlistentry>
  231. <term><constant>RETURN_STREAM</constant></term>
  232. <listitem>
  233. <para>Default: Return the data as stream</para>
  234. </listitem>
  235. </varlistentry>
  236. </variablelist>
  237. </listitem>
  238. <listitem>
  239. <para>For <methodname>listItems()</methodname>:</para>
  240. <variablelist>
  241. <varlistentry>
  242. <term><constant>RETURN_NAMES</constant></term>
  243. <listitem>
  244. <para>return the list of item names (default)</para>
  245. </listitem>
  246. </varlistentry>
  247. <varlistentry>
  248. <term><constant>RETURN_LIST</constant></term>
  249. <listitem>
  250. <para>return the list of WindowsAzure objects</para>
  251. </listitem>
  252. </varlistentry>
  253. </variablelist>
  254. </listitem>
  255. </itemizedlist>
  256. </entry>
  257. <entry>
  258. <methodname>fetchItem()</methodname>,
  259. <methodname>listItems()</methodname>
  260. </entry>
  261. <entry>No</entry>
  262. <entry>
  263. <constant>RETURN_STREAM</constant> for
  264. <methodname>fetchItem()</methodname>;
  265. <constant>RETURN_NAMES</constant> for
  266. <methodname>listItems()</methodname>
  267. </entry>
  268. </row>
  269. <row>
  270. <entry>return_path</entry>
  271. <entry>
  272. Return path. This is the URL that can be used to access the item
  273. after it has been uploaded.
  274. </entry>
  275. <entry><methodname>fetchItem()</methodname></entry>
  276. <entry>No</entry>
  277. <entry>System tmp directory</entry>
  278. </row>
  279. <row>
  280. <entry>return_openmode</entry>
  281. <entry>
  282. <methodname>fopen()</methodname> mode used to open the file for
  283. saving data
  284. </entry>
  285. <entry><methodname>fetchItem()</methodname></entry>
  286. <entry>No</entry>
  287. <entry>'r'</entry>
  288. </row>
  289. </tbody>
  290. </tgroup>
  291. </table>
  292. <table frame='all' id="zend.cloud.storageservice.options.filesystem">
  293. <title>Zend_Cloud_StorageService_Adapter_Filesystem options</title>
  294. <tgroup cols="5">
  295. <thead>
  296. <row>
  297. <entry>Option key</entry>
  298. <entry>Description</entry>
  299. <entry>Used in</entry>
  300. <entry>Required</entry>
  301. <entry>Default</entry>
  302. </row>
  303. </thead>
  304. <tbody>
  305. <row>
  306. <entry>local_directory</entry>
  307. <entry>Local directory where the files will be stored</entry>
  308. <entry>Constructor</entry>
  309. <entry>No</entry>
  310. <entry>System tmp directory</entry>
  311. </row>
  312. </tbody>
  313. </tgroup>
  314. </table>
  315. </sect3>
  316. </sect2>
  317. <sect2 id="zend.cloud.storageservice.concepts">
  318. <title>Basic concepts</title>
  319. <para>
  320. Different cloud storage services use their own unique terminology to refer to document
  321. storage concepts. The SimpleCloud API defines a number of common concepts that are
  322. shared among all major providers.
  323. </para>
  324. <para>
  325. The storage service identifies files by string keys, which may be URL paths or another
  326. service-specific identifier. The items can be stored and retrieved using this key. Each
  327. item can have <emphasis>metadata</emphasis> associated with it. These metadata carry
  328. service-specific information about the item, such as size, type, permissions, etc. as
  329. defined in the adapter for that provider.
  330. </para>
  331. </sect2>
  332. <sect2 id="zend.cloud.storageservice.exceptions">
  333. <title>Exceptions</title>
  334. <para>
  335. If some error occurs inside the storage service, a
  336. <classname>Zend_Cloud_StorageService_Exception</classname> is thrown. If the exception
  337. was caused by underlying service driver, you can use the
  338. <methodname>getClientException()</methodname> method to retrieve the original exception.
  339. </para>
  340. <para>
  341. Since different cloud providers implement different sets of services, some adapters do
  342. not implement certain features. In this case, the
  343. <classname>Zend_Cloud_OperationNotAvailableException</classname> exception is thrown.
  344. </para>
  345. </sect2>
  346. <sect2 id="zend.cloud.storageservice.store">
  347. <title>Store an item</title>
  348. <para>
  349. <methodname>storeItem()</methodname> method is used to upload or otherwise add files to
  350. the storage provider.
  351. </para>
  352. <example id="zend.cloud.storageservice.store.example">
  353. <title>Storing an item</title>
  354. <programlisting language="php"><![CDATA[
  355. $data = file_get_contents('/my/local/dir/picture.jpg');
  356. $returnedData = $storage->storeItem('/my/remote/path/picture.jpg', $data);
  357. ]]></programlisting>
  358. </example>
  359. <para>
  360. An optional third parameter describes service-specific options.
  361. </para>
  362. <example id="zend.cloud.storageservice.store-options.example">
  363. <title>Storing an item with options</title>
  364. <programlisting language="php"><![CDATA[
  365. $data = file_get_contents("/my/local/dir/picture.jpg");
  366. // Use S3 bucket: myBucket
  367. // Make this item publicly readable
  368. $returnedData = $storage->storeItem(
  369. '/my/remote/path/picture.jpg',
  370. $data,
  371. array(
  372. Zend_Cloud_StorageService_Adapter_S3::BUCKET_NAME => "myBucket",
  373. Zend_Cloud_StorageService_Adapter_S3::METADATA => array(
  374. Zend_Service_Amazon_S3::S3_ACL_HEADER => Zend_Service_Amazon_S3::S3_ACL_PUBLIC_READ,
  375. )
  376. )
  377. );
  378. ]]></programlisting>
  379. </example>
  380. <para>
  381. For service adapters that support streaming, data can also be a PHP stream (i.e. opened
  382. file).
  383. </para>
  384. </sect2>
  385. <sect2 id="zend.cloud.storageservice.fetch">
  386. <title>Fetch an item</title>
  387. <para>
  388. The <methodname>fetchItem()</methodname> operation retrieves an item from the storage.
  389. </para>
  390. <example id="zend.cloud.storageservice.fetch.example">
  391. <title>Fetching an item</title>
  392. <programlisting language="php"><![CDATA[
  393. $returnedData = $storage->fetchItem("/my/remote/path/picture.jpg");
  394. file_put_contents($localFilePath, $returnedData);
  395. ]]></programlisting>
  396. </example>
  397. </sect2>
  398. <sect2 id="zend.cloud.storageservice.delete">
  399. <title>Delete an item</title>
  400. <para>
  401. The <methodname>deleteItem()</methodname> operation removes an item from the storage
  402. service.
  403. </para>
  404. <example id="zend.cloud.storageservice.delete.example">
  405. <title>Deleting an item</title>
  406. <programlisting language="php"><![CDATA[
  407. $storage->deleteItem("/my/remote/path/picture.jpg");
  408. ]]></programlisting>
  409. </example>
  410. </sect2>
  411. <sect2 id="zend.cloud.storageservice.copy">
  412. <title>Copy an item</title>
  413. <para>
  414. The <methodname>copyItem()</methodname> operation creates a copy of the item in the
  415. storage.
  416. </para>
  417. <note>
  418. <para>
  419. Not all services support copying natively. If this is the case, the adapter will
  420. simulate the operation, fetching the item and storing it under the target path.
  421. </para>
  422. </note>
  423. <example id="zend.cloud.storageservice.copy.example">
  424. <title>Copying an item</title>
  425. <programlisting language="php"><![CDATA[
  426. $storage->copyItem(
  427. '/my/remote/path/picture.jpg',
  428. '/anothor/remote/dir/picturecopy.jpg'
  429. );
  430. ]]></programlisting>
  431. </example>
  432. </sect2>
  433. <sect2 id="zend.cloud.storageservice.move">
  434. <title>Move an item</title>
  435. <para>
  436. The <methodname>moveItem()</methodname> operation moves an item from one key (or
  437. directory) to another.
  438. </para>
  439. <note>
  440. <para>
  441. Not all services support moving natively. If this is the case the adapter will
  442. simulate the operation, fetching the item, storing it under the target path, then
  443. deleting the original file.
  444. </para>
  445. </note>
  446. <example id="zend.cloud.storageservice.move.example">
  447. <title>Moving an item</title>
  448. <programlisting language="php"><![CDATA[
  449. $storage->moveItem(
  450. '/my/remote/path/picture.jpg',
  451. '/anothor/remote/dir/newpicture.jpg'
  452. );
  453. ]]></programlisting>
  454. </example>
  455. </sect2>
  456. <sect2 id="zend.cloud.storageservice.rename">
  457. <title>Rename an item</title>
  458. <para>
  459. The <methodname>renameItem()</methodname> operation changes the item name. For some
  460. services, this operation may be equivalent to moving to its original directory with a
  461. new name.
  462. </para>
  463. <example id="zend.cloud.storageservice.rename.example">
  464. <title>Renaming an item</title>
  465. <programlisting language="php"><![CDATA[
  466. $storage->renameItem('/my/remote/path/picture.jpg', 'newpicture.jpg');
  467. ]]></programlisting>
  468. </example>
  469. </sect2>
  470. <sect2 id="zend.cloud.storageservice.list">
  471. <title>List items</title>
  472. <para>
  473. To list the items stored in the specified path, use the
  474. <methodname>listItems()</methodname> method. The method returns a list of names
  475. identifying matching remote items.
  476. </para>
  477. <example id="zend.cloud.storageservice.list.example">
  478. <title>List items</title>
  479. <programlisting language="php"><![CDATA[
  480. $objects = $storage->listItems('/my/remote/path/');
  481. foreach ($objects as $objname) {
  482. echo "Found: $objname\n";
  483. }
  484. ]]></programlisting>
  485. </example>
  486. </sect2>
  487. <sect2 id="zend.cloud.storageservice.fetch-metadata">
  488. <title>Fetching metadata</title>
  489. <para>
  490. Some services store a set of key-value pairs along with the item as metadata. Use the
  491. <methodname>fetchMetadata()</methodname> method to retrieve an item's metadata.
  492. </para>
  493. <example id="zend.cloud.storageservice.fetch-metadata.example">
  494. <title>Fetching metadata</title>
  495. <programlisting language="php"><![CDATA[
  496. $data = $storage->fetchMetadata('/my/remote/path/picture.jpg');
  497. foreach ($data as $key => $value) {
  498. echo "Metadata $key: $value\n";
  499. }
  500. ]]></programlisting>
  501. </example>
  502. </sect2>
  503. <sect2 id="zend.cloud.storageservice.store-metadata">
  504. <title>Store metadata</title>
  505. <para>
  506. Depending on the service, metadata can be supplied either when storing the item or with
  507. a separate request. In the latter case, use <methodname>storeMetadata()</methodname> to
  508. add or update this metadata.
  509. </para>
  510. <example id="zend.cloud.storageservice.store-metadata.example">
  511. <title>Storing metadata</title>
  512. <programlisting language="php"><![CDATA[
  513. $data = $storage->storeMetadata('/my/remote/path/picture.jpg', array(
  514. 'type' => 'JPEG',
  515. 'category' => 'Portrait',
  516. ));
  517. ]]></programlisting>
  518. </example>
  519. </sect2>
  520. <sect2 id="zend.cloud.storageservice.delete-metadata">
  521. <title>Delete metadata</title>
  522. <para>
  523. The <methodname>deleteMetadata()</methodname> method removes all user-supplied metadata
  524. from an item.
  525. </para>
  526. <note>
  527. <para>
  528. Not all services support removing metadata.
  529. </para>
  530. </note>
  531. <example id="zend.cloud.storageservice.delete-metadata.example">
  532. <title>Deleting metadata</title>
  533. <programlisting language="php"><![CDATA[
  534. $storage->deleteMetadata("/my/remote/path/picture.jpg");
  535. ]]></programlisting>
  536. </example>
  537. </sect2>
  538. <sect2 id="zend.cloud.storageservice.adapter">
  539. <title>Accessing concrete adapters</title>
  540. <para>
  541. Sometimes it is necessary to retrieve the concrete adapter for the service that the
  542. Storage API is working with. This can be achieved by using the
  543. <methodname>getAdapter()</methodname> method.
  544. </para>
  545. <note>
  546. <para>
  547. Accessing the underlying adapter breaks portability among services, so it should be
  548. reserved for exceptional circumstances only.
  549. </para>
  550. </note>
  551. <example id="zend.cloud.storageservice.adapter.example">
  552. <title>Using a concrete adapter</title>
  553. <programlisting language="php"><![CDATA[
  554. // the Simple Cloud Storage API doesn't support "clean bucket" operation
  555. // the concrete adapter can be used to access this feature
  556. $s3 = $storage->getClient();
  557. $s3->cleanBucket("oldBucket");
  558. ]]></programlisting>
  559. </example>
  560. </sect2>
  561. </sect1>