Object.php 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304
  1. <?php
  2. require_once __DIR__ . '/Common.php';
  3. use NOS\NosClient;
  4. use NOS\Core\NosException;
  5. $bucket = Common::getTestBucketName();
  6. $nosClient = Common::getNosClient();
  7. if (is_null($nosClient)) exit(1);
  8. //----------------------------------------------基本使用--------------------------------------------------------
  9. $nosClient->putObject($bucket, "b.file", "hi, nos");
  10. Common::println("b.file is created");
  11. $nosClient->uploadFile($bucket, "c.file", __FILE__);
  12. Common::println("c.file is created");
  13. $content = $nosClient->getObject($bucket, "b.file");
  14. Common::println("b.file is fetched, the content is: " . $content);
  15. $options = array(
  16. NosClient::NOS_FILE_DOWNLOAD => "./c.file.localcopy",
  17. );
  18. $nosClient->getObject($bucket, "c.file", $options);
  19. Common::println("b.file is fetched to the local file: c.file.localcopy");
  20. $nosClient->copyObject($bucket, "c.file", $bucket, "c.file.copy");
  21. Common::println("c.file is copied to c.file.copy");
  22. $doesExist = $nosClient->doesObjectExist($bucket, "c.file.copy");
  23. Common::println("file c.file.copy exist? " . ($doesExist ? "yes" : "no"));
  24. $nosClient->deleteObject($bucket, "c.file.copy");
  25. Common::println("c.file.copy is deleted");
  26. $doesExist = $nosClient->doesObjectExist($bucket, "c.file.copy");
  27. Common::println("file c.file.copy exist? " . ($doesExist ? "yes" : "no"));
  28. $nosClient->deleteObjects($bucket, array("b.file", "c.file"));
  29. Common::println("b.file, c.file are deleted");
  30. sleep(2);
  31. unlink("c.file.localcopy");
  32. //-----------------------------------------具体的使用方法请参考以下文档---------------------------------------------
  33. listObjects($nosClient, $bucket);
  34. listAllObjects($nosClient, $bucket);
  35. putObject($nosClient, $bucket);
  36. uploadFile($nosClient, $bucket);
  37. getObject($nosClient, $bucket);
  38. getObjectToLocalFile($nosClient, $bucket);
  39. copyObject($nosClient, $bucket);
  40. getObjectMeta($nosClient, $bucket);
  41. deleteObject($nosClient, $bucket);
  42. deleteObjects($nosClient, $bucket);
  43. doesObjectExist($nosClient, $bucket);
  44. /**
  45. *
  46. * @param unknown $nosClient
  47. * @param unknown $bucket
  48. */
  49. function putObject($nosClient, $bucket)
  50. {
  51. $object = "nos-php-sdk-test/upload-test-object-name.txt";
  52. $content = file_get_contents(__FILE__);
  53. $options = array();
  54. try {
  55. $nosClient->putObject($bucket, $object, $content, $options);
  56. } catch (NosException $e) {
  57. printf(__FUNCTION__ . ": FAILED\n");
  58. printf($e->getMessage() . "\n");
  59. return;
  60. }
  61. print(__FUNCTION__ . ": OK" . "\n");
  62. }
  63. /**
  64. *
  65. * @param unknown $nosClient
  66. * @param unknown $bucket
  67. */
  68. function uploadFile($nosClient, $bucket)
  69. {
  70. $object = "nos-php-sdk-test/upload-test-object-name.txt";
  71. $filePath = __FILE__;
  72. $options = array();
  73. try {
  74. $options[NosClient::NOS_HEADERS]['Cache-Control'] = 'max-age=60';
  75. $options[NosClient::NOS_HEADERS]['Content-Disposition'] = 'attachment; filename="xxxxxx"';
  76. $nosClient->uploadFile($bucket, $object, $filePath, $options);
  77. } catch (NosException $e) {
  78. printf(__FUNCTION__ . ": FAILED\n");
  79. printf($e->getMessage() . "\n");
  80. return;
  81. }
  82. print(__FUNCTION__ . ": OK" . "\n");
  83. }
  84. /**
  85. *
  86. * @param unknown $nosClient
  87. * @param unknown $bucket
  88. */
  89. function listObjects($nosClient, $bucket)
  90. {
  91. $prefix = 'nos-php-sdk-test/';
  92. $delimiter = '/';
  93. $nextMarker = '';
  94. $maxkeys = 1000;
  95. $options = array(
  96. 'delimiter' => $delimiter,
  97. 'prefix' => $prefix,
  98. 'max-keys' => $maxkeys,
  99. 'marker' => $nextMarker,
  100. );
  101. try {
  102. $listObjectInfo = $nosClient->listObjects($bucket, $options);
  103. } catch (NosException $e) {
  104. printf(__FUNCTION__ . ": FAILED\n");
  105. printf($e->getMessage() . "\n");
  106. return;
  107. }
  108. print(__FUNCTION__ . ": OK" . "\n");
  109. $objectList = $listObjectInfo->getObjectList(); // 文件列表
  110. if (!empty($objectList)) {
  111. print("objectList:\n");
  112. foreach ($objectList as $objectInfo) {
  113. print($objectInfo->getKey() . "\n");
  114. }
  115. }
  116. }
  117. function listAllObjects($nosClient, $bucket)
  118. {
  119. //构造dir下的文件和虚拟目录
  120. for ($i = 0; $i < 100; $i += 1) {
  121. $nosClient->putObject($bucket, "dir/obj" . strval($i), "hi");
  122. }
  123. $prefix = 'dir/';
  124. $delimiter = '/';
  125. $nextMarker = '';
  126. $maxkeys = 30;
  127. while (true) {
  128. $options = array(
  129. 'delimiter' => $delimiter,
  130. 'prefix' => $prefix,
  131. 'max-keys' => $maxkeys,
  132. 'marker' => $nextMarker,
  133. );
  134. var_dump($options);
  135. try {
  136. $listObjectInfo = $nosClient->listObjects($bucket, $options);
  137. } catch (NosException $e) {
  138. printf(__FUNCTION__ . ": FAILED\n");
  139. printf($e->getMessage() . "\n");
  140. return;
  141. }
  142. // 得到nextMarker,从上一次listObjects读到的最后一个文件的下一个文件开始继续获取文件列表
  143. $nextMarker = $listObjectInfo->getNextMarker();
  144. $listObject = $listObjectInfo->getObjectList();
  145. var_dump(count($listObject));
  146. if ($nextMarker === '') {
  147. break;
  148. }
  149. }
  150. }
  151. function getObject($nosClient, $bucket)
  152. {
  153. $object = "nos-php-sdk-test/upload-test-object-name.txt";
  154. $options = array();
  155. try {
  156. $content = $nosClient->getObject($bucket, $object, $options);
  157. } catch (NosException $e) {
  158. printf(__FUNCTION__ . ": FAILED\n");
  159. printf($e->getMessage() . "\n");
  160. return;
  161. }
  162. print(__FUNCTION__ . ": OK" . "\n");
  163. if (file_get_contents(__FILE__) === $content) {
  164. print(__FUNCTION__ . ": FileContent checked OK" . "\n");
  165. } else {
  166. print(__FUNCTION__ . ": FileContent checked FAILED" . "\n");
  167. }
  168. }
  169. function getObjectToLocalFile($nosClient, $bucket)
  170. {
  171. $object = "nos-php-sdk-test/upload-test-object-name.txt";
  172. $localfile = "upload-test-object-name.txt";
  173. $options = array(
  174. NosClient::NOS_FILE_DOWNLOAD => $localfile,
  175. );
  176. try {
  177. $nosClient->getObject($bucket, $object, $options);
  178. } catch (NosException $e) {
  179. printf(__FUNCTION__ . ": FAILED\n");
  180. printf($e->getMessage() . "\n");
  181. return;
  182. }
  183. print(__FUNCTION__ . ": OK, please check localfile: 'upload-test-object-name.txt'" . "\n");
  184. if (file_get_contents($localfile) === file_get_contents(__FILE__)) {
  185. print(__FUNCTION__ . ": FileContent checked OK" . "\n");
  186. } else {
  187. print(__FUNCTION__ . ": FileContent checked FAILED" . "\n");
  188. }
  189. if (file_exists($localfile)) {
  190. unlink($localfile);
  191. }
  192. }
  193. function copyObject($nosClient, $bucket)
  194. {
  195. $fromBucket = $bucket;
  196. $fromObject = "nos-php-sdk-test/upload-test-object-name.txt";
  197. $toBucket = $bucket;
  198. $toObject = $fromObject . '.copy';
  199. $options = array();
  200. try {
  201. $nosClient->copyObject($fromBucket, $fromObject, $toBucket, $toObject, $options);
  202. } catch (NosException $e) {
  203. printf(__FUNCTION__ . ": FAILED\n");
  204. printf($e->getMessage() . "\n");
  205. return;
  206. }
  207. print(__FUNCTION__ . ": OK" . "\n");
  208. }
  209. function getObjectMeta($nosClient, $bucket)
  210. {
  211. $object = "nos-php-sdk-test/upload-test-object-name.txt";
  212. try {
  213. $objectMeta = $nosClient->getObjectMeta($bucket, $object);
  214. } catch (NosException $e) {
  215. printf(__FUNCTION__ . ": FAILED\n");
  216. printf($e->getMessage() . "\n");
  217. return;
  218. }
  219. print(__FUNCTION__ . ": OK" . "\n");
  220. if (isset($objectMeta[strtolower('Content-Disposition')]) &&
  221. 'attachment; filename="xxxxxx"' === $objectMeta[strtolower('Content-Disposition')]
  222. ) {
  223. print(__FUNCTION__ . ": ObjectMeta checked OK" . "\n");
  224. } else {
  225. print(__FUNCTION__ . ": ObjectMeta checked FAILED" . "\n");
  226. }
  227. }
  228. function deleteObject($nosClient, $bucket)
  229. {
  230. $object = "nos-php-sdk-test/upload-test-object-name.txt";
  231. try {
  232. $nosClient->deleteObject($bucket, $object);
  233. } catch (NosException $e) {
  234. printf(__FUNCTION__ . ": FAILED\n");
  235. printf($e->getMessage() . "\n");
  236. return;
  237. }
  238. print(__FUNCTION__ . ": OK" . "\n");
  239. }
  240. function deleteObjects($nosClient, $bucket)
  241. {
  242. $objects = array();
  243. $objects[] = "nos-php-sdk-test/upload-test-object-name.txt";
  244. $objects[] = "nos-php-sdk-test/upload-test-object-name.txt.copy";
  245. try {
  246. $nosClient->deleteObjects($bucket, $objects);
  247. } catch (NosException $e) {
  248. printf(__FUNCTION__ . ": FAILED\n");
  249. printf($e->getMessage() . "\n");
  250. return;
  251. }
  252. print(__FUNCTION__ . ": OK" . "\n");
  253. }
  254. function doesObjectExist($nosClient, $bucket)
  255. {
  256. $object = "nos-php-sdk-test/upload-test-object-name.txt";
  257. try {
  258. $exist = $nosClient->doesObjectExist($bucket, $object);
  259. } catch (NosException $e) {
  260. printf(__FUNCTION__ . ": FAILED\n");
  261. printf($e->getMessage() . "\n");
  262. return;
  263. }
  264. print(__FUNCTION__ . ": OK" . "\n");
  265. var_dump($exist);
  266. }