暂无描述

Object.php 15KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518
  1. <?php
  2. require_once __DIR__ . '/Common.php';
  3. use OSS\OssClient;
  4. use OSS\Core\OssException;
  5. $bucket = Common::getBucketName();
  6. $ossClient = Common::getOssClient();
  7. if (is_null($ossClient)) exit(1);
  8. //*******************************简单使用***************************************************************
  9. // 简单上传变量的内容到oss文件
  10. $result = $ossClient->putObject($bucket, "b.file", "hi, oss");
  11. Common::println("b.file is created");
  12. Common::println($result['x-oss-request-id']);
  13. Common::println($result['etag']);
  14. Common::println($result['content-md5']);
  15. Common::println($result['body']);
  16. // 上传本地文件
  17. $result = $ossClient->uploadFile($bucket, "c.file", __FILE__);
  18. Common::println("c.file is created");
  19. Common::println("b.file is created");
  20. Common::println($result['x-oss-request-id']);
  21. Common::println($result['etag']);
  22. Common::println($result['content-md5']);
  23. Common::println($result['body']);
  24. // 下载object到本地变量
  25. $content = $ossClient->getObject($bucket, "b.file");
  26. Common::println("b.file is fetched, the content is: " . $content);
  27. // 给object添加symlink
  28. $content = $ossClient->putSymlink($bucket, "test-symlink", "b.file");
  29. Common::println("test-symlink is created");
  30. Common::println($result['x-oss-request-id']);
  31. Common::println($result['etag']);
  32. // 获取symlink
  33. $content = $ossClient->getSymlink($bucket, "test-symlink");
  34. Common::println("test-symlink refer to : " . $content[OssClient::OSS_SYMLINK_TARGET]);
  35. // 下载object到本地文件
  36. $options = array(
  37. OssClient::OSS_FILE_DOWNLOAD => "./c.file.localcopy",
  38. );
  39. $ossClient->getObject($bucket, "c.file", $options);
  40. Common::println("b.file is fetched to the local file: c.file.localcopy");
  41. Common::println("b.file is created");
  42. // 拷贝object
  43. $result = $ossClient->copyObject($bucket, "c.file", $bucket, "c.file.copy");
  44. Common::println("lastModifiedTime: " . $result[0]);
  45. Common::println("ETag: " . $result[1]);
  46. // 判断object是否存在
  47. $doesExist = $ossClient->doesObjectExist($bucket, "c.file.copy");
  48. Common::println("file c.file.copy exist? " . ($doesExist ? "yes" : "no"));
  49. // 删除object
  50. $result = $ossClient->deleteObject($bucket, "c.file.copy");
  51. Common::println("c.file.copy is deleted");
  52. Common::println("b.file is created");
  53. Common::println($result['x-oss-request-id']);
  54. // 判断object是否存在
  55. $doesExist = $ossClient->doesObjectExist($bucket, "c.file.copy");
  56. Common::println("file c.file.copy exist? " . ($doesExist ? "yes" : "no"));
  57. // 批量删除object
  58. $result = $ossClient->deleteObjects($bucket, array("b.file", "c.file"));
  59. foreach($result as $object)
  60. Common::println($object);
  61. sleep(2);
  62. unlink("c.file.localcopy");
  63. //******************************* 完整用法参考下面函数 ****************************************************
  64. listObjects($ossClient, $bucket);
  65. listAllObjects($ossClient, $bucket);
  66. createObjectDir($ossClient, $bucket);
  67. putObject($ossClient, $bucket);
  68. uploadFile($ossClient, $bucket);
  69. getObject($ossClient, $bucket);
  70. getObjectToLocalFile($ossClient, $bucket);
  71. copyObject($ossClient, $bucket);
  72. modifyMetaForObject($ossClient, $bucket);
  73. getObjectMeta($ossClient, $bucket);
  74. deleteObject($ossClient, $bucket);
  75. deleteObjects($ossClient, $bucket);
  76. doesObjectExist($ossClient, $bucket);
  77. getSymlink($ossClient, $bucket);
  78. putSymlink($ossClient, $bucket);
  79. /**
  80. * 创建虚拟目录
  81. *
  82. * @param OssClient $ossClient OssClient实例
  83. * @param string $bucket 存储空间名称
  84. * @return null
  85. */
  86. function createObjectDir($ossClient, $bucket)
  87. {
  88. try {
  89. $ossClient->createObjectDir($bucket, "dir");
  90. } catch (OssException $e) {
  91. printf(__FUNCTION__ . ": FAILED\n");
  92. printf($e->getMessage() . "\n");
  93. return;
  94. }
  95. print(__FUNCTION__ . ": OK" . "\n");
  96. }
  97. /**
  98. * 把本地变量的内容到文件
  99. *
  100. * 简单上传,上传指定变量的内存值作为object的内容
  101. *
  102. * @param OssClient $ossClient OssClient实例
  103. * @param string $bucket 存储空间名称
  104. * @return null
  105. */
  106. function putObject($ossClient, $bucket)
  107. {
  108. $object = "oss-php-sdk-test/upload-test-object-name.txt";
  109. $content = file_get_contents(__FILE__);
  110. $options = array();
  111. try {
  112. $ossClient->putObject($bucket, $object, $content, $options);
  113. } catch (OssException $e) {
  114. printf(__FUNCTION__ . ": FAILED\n");
  115. printf($e->getMessage() . "\n");
  116. return;
  117. }
  118. print(__FUNCTION__ . ": OK" . "\n");
  119. }
  120. /**
  121. * 上传指定的本地文件内容
  122. *
  123. * @param OssClient $ossClient OssClient实例
  124. * @param string $bucket 存储空间名称
  125. * @return null
  126. */
  127. function uploadFile($ossClient, $bucket)
  128. {
  129. $object = "oss-php-sdk-test/upload-test-object-name.txt";
  130. $filePath = __FILE__;
  131. $options = array();
  132. try {
  133. $ossClient->uploadFile($bucket, $object, $filePath, $options);
  134. } catch (OssException $e) {
  135. printf(__FUNCTION__ . ": FAILED\n");
  136. printf($e->getMessage() . "\n");
  137. return;
  138. }
  139. print(__FUNCTION__ . ": OK" . "\n");
  140. }
  141. /**
  142. * 列出Bucket内所有目录和文件, 注意如果符合条件的文件数目超过设置的max-keys, 用户需要使用返回的nextMarker作为入参,通过
  143. * 循环调用ListObjects得到所有的文件,具体操作见下面的 listAllObjects 示例
  144. *
  145. * @param OssClient $ossClient OssClient实例
  146. * @param string $bucket 存储空间名称
  147. * @return null
  148. */
  149. function listObjects($ossClient, $bucket)
  150. {
  151. $prefix = 'oss-php-sdk-test/';
  152. $delimiter = '/';
  153. $nextMarker = '';
  154. $maxkeys = 1000;
  155. $options = array(
  156. 'delimiter' => $delimiter,
  157. 'prefix' => $prefix,
  158. 'max-keys' => $maxkeys,
  159. 'marker' => $nextMarker,
  160. );
  161. try {
  162. $listObjectInfo = $ossClient->listObjects($bucket, $options);
  163. } catch (OssException $e) {
  164. printf(__FUNCTION__ . ": FAILED\n");
  165. printf($e->getMessage() . "\n");
  166. return;
  167. }
  168. print(__FUNCTION__ . ": OK" . "\n");
  169. $objectList = $listObjectInfo->getObjectList(); // 文件列表
  170. $prefixList = $listObjectInfo->getPrefixList(); // 目录列表
  171. if (!empty($objectList)) {
  172. print("objectList:\n");
  173. foreach ($objectList as $objectInfo) {
  174. print($objectInfo->getKey() . "\n");
  175. }
  176. }
  177. if (!empty($prefixList)) {
  178. print("prefixList: \n");
  179. foreach ($prefixList as $prefixInfo) {
  180. print($prefixInfo->getPrefix() . "\n");
  181. }
  182. }
  183. }
  184. /**
  185. * 列出Bucket内所有目录和文件, 根据返回的nextMarker循环得到所有Objects
  186. *
  187. * @param OssClient $ossClient OssClient实例
  188. * @param string $bucket 存储空间名称
  189. * @return null
  190. */
  191. function listAllObjects($ossClient, $bucket)
  192. {
  193. //构造dir下的文件和虚拟目录
  194. for ($i = 0; $i < 100; $i += 1) {
  195. $ossClient->putObject($bucket, "dir/obj" . strval($i), "hi");
  196. $ossClient->createObjectDir($bucket, "dir/obj" . strval($i));
  197. }
  198. $prefix = 'dir/';
  199. $delimiter = '/';
  200. $nextMarker = '';
  201. $maxkeys = 30;
  202. while (true) {
  203. $options = array(
  204. 'delimiter' => $delimiter,
  205. 'prefix' => $prefix,
  206. 'max-keys' => $maxkeys,
  207. 'marker' => $nextMarker,
  208. );
  209. var_dump($options);
  210. try {
  211. $listObjectInfo = $ossClient->listObjects($bucket, $options);
  212. } catch (OssException $e) {
  213. printf(__FUNCTION__ . ": FAILED\n");
  214. printf($e->getMessage() . "\n");
  215. return;
  216. }
  217. // 得到nextMarker,从上一次listObjects读到的最后一个文件的下一个文件开始继续获取文件列表
  218. $nextMarker = $listObjectInfo->getNextMarker();
  219. $listObject = $listObjectInfo->getObjectList();
  220. $listPrefix = $listObjectInfo->getPrefixList();
  221. var_dump(count($listObject));
  222. var_dump(count($listPrefix));
  223. if ($nextMarker === '') {
  224. break;
  225. }
  226. }
  227. }
  228. /**
  229. * 获取object的内容
  230. *
  231. * @param OssClient $ossClient OssClient实例
  232. * @param string $bucket 存储空间名称
  233. * @return null
  234. */
  235. function getObject($ossClient, $bucket)
  236. {
  237. $object = "oss-php-sdk-test/upload-test-object-name.txt";
  238. $options = array();
  239. try {
  240. $content = $ossClient->getObject($bucket, $object, $options);
  241. } catch (OssException $e) {
  242. printf(__FUNCTION__ . ": FAILED\n");
  243. printf($e->getMessage() . "\n");
  244. return;
  245. }
  246. print(__FUNCTION__ . ": OK" . "\n");
  247. if (file_get_contents(__FILE__) === $content) {
  248. print(__FUNCTION__ . ": FileContent checked OK" . "\n");
  249. } else {
  250. print(__FUNCTION__ . ": FileContent checked FAILED" . "\n");
  251. }
  252. }
  253. /**
  254. * put symlink
  255. *
  256. * @param OssClient $ossClient OssClient实例
  257. * @param string $bucket 存储空间名称
  258. * @return null
  259. */
  260. function putSymlink($ossClient, $bucket)
  261. {
  262. $symlink = "test-samples-symlink";
  263. $object = "test-samples-object";
  264. try {
  265. $ossClient->putObject($bucket, $object, 'test-content');
  266. $ossClient->putSymlink($bucket, $symlink, $object);
  267. $content = $ossClient->getObject($bucket, $symlink);
  268. } catch (OssException $e) {
  269. printf(__FUNCTION__ . ": FAILED\n");
  270. printf($e->getMessage() . "\n");
  271. return;
  272. }
  273. print(__FUNCTION__ . ": OK" . "\n");
  274. if ($content == 'test-content') {
  275. print(__FUNCTION__ . ": putSymlink checked OK" . "\n");
  276. } else {
  277. print(__FUNCTION__ . ": putSymlink checked FAILED" . "\n");
  278. }
  279. }
  280. /**
  281. * 获取symlink
  282. *
  283. * @param OssClient $ossClient OssClient实例
  284. * @param string $bucket 存储空间名称
  285. * @return null
  286. */
  287. function getSymlink($ossClient, $bucket)
  288. {
  289. $symlink = "test-samples-symlink";
  290. $object = "test-samples-object";
  291. try {
  292. $ossClient->putObject($bucket, $object, 'test-content');
  293. $ossClient->putSymlink($bucket, $symlink, $object);
  294. $content = $ossClient->getSymlink($bucket, $symlink);
  295. } catch (OssException $e) {
  296. printf(__FUNCTION__ . ": FAILED\n");
  297. printf($e->getMessage() . "\n");
  298. return;
  299. }
  300. print(__FUNCTION__ . ": OK" . "\n");
  301. if ($content[OssClient::OSS_SYMLINK_TARGET]) {
  302. print(__FUNCTION__ . ": getSymlink checked OK" . "\n");
  303. } else {
  304. print(__FUNCTION__ . ": getSymlink checked FAILED" . "\n");
  305. }
  306. }
  307. /**
  308. * get_object_to_local_file
  309. *
  310. * 获取object
  311. * 将object下载到指定的文件
  312. *
  313. * @param OssClient $ossClient OssClient实例
  314. * @param string $bucket 存储空间名称
  315. * @return null
  316. */
  317. function getObjectToLocalFile($ossClient, $bucket)
  318. {
  319. $object = "oss-php-sdk-test/upload-test-object-name.txt";
  320. $localfile = "upload-test-object-name.txt";
  321. $options = array(
  322. OssClient::OSS_FILE_DOWNLOAD => $localfile,
  323. );
  324. try {
  325. $ossClient->getObject($bucket, $object, $options);
  326. } catch (OssException $e) {
  327. printf(__FUNCTION__ . ": FAILED\n");
  328. printf($e->getMessage() . "\n");
  329. return;
  330. }
  331. print(__FUNCTION__ . ": OK, please check localfile: 'upload-test-object-name.txt'" . "\n");
  332. if (file_get_contents($localfile) === file_get_contents(__FILE__)) {
  333. print(__FUNCTION__ . ": FileContent checked OK" . "\n");
  334. } else {
  335. print(__FUNCTION__ . ": FileContent checked FAILED" . "\n");
  336. }
  337. if (file_exists($localfile)) {
  338. unlink($localfile);
  339. }
  340. }
  341. /**
  342. * 拷贝object
  343. * 当目的object和源object完全相同时,表示修改object的meta信息
  344. *
  345. * @param OssClient $ossClient OssClient实例
  346. * @param string $bucket 存储空间名称
  347. * @return null
  348. */
  349. function copyObject($ossClient, $bucket)
  350. {
  351. $fromBucket = $bucket;
  352. $fromObject = "oss-php-sdk-test/upload-test-object-name.txt";
  353. $toBucket = $bucket;
  354. $toObject = $fromObject . '.copy';
  355. $options = array();
  356. try {
  357. $ossClient->copyObject($fromBucket, $fromObject, $toBucket, $toObject, $options);
  358. } catch (OssException $e) {
  359. printf(__FUNCTION__ . ": FAILED\n");
  360. printf($e->getMessage() . "\n");
  361. return;
  362. }
  363. print(__FUNCTION__ . ": OK" . "\n");
  364. }
  365. /**
  366. * 修改Object Meta
  367. * 利用copyObject接口的特性:当目的object和源object完全相同时,表示修改object的meta信息
  368. *
  369. * @param OssClient $ossClient OssClient实例
  370. * @param string $bucket 存储空间名称
  371. * @return null
  372. */
  373. function modifyMetaForObject($ossClient, $bucket)
  374. {
  375. $fromBucket = $bucket;
  376. $fromObject = "oss-php-sdk-test/upload-test-object-name.txt";
  377. $toBucket = $bucket;
  378. $toObject = $fromObject;
  379. $copyOptions = array(
  380. OssClient::OSS_HEADERS => array(
  381. 'Cache-Control' => 'max-age=60',
  382. 'Content-Disposition' => 'attachment; filename="xxxxxx"',
  383. ),
  384. );
  385. try {
  386. $ossClient->copyObject($fromBucket, $fromObject, $toBucket, $toObject, $copyOptions);
  387. } catch (OssException $e) {
  388. printf(__FUNCTION__ . ": FAILED\n");
  389. printf($e->getMessage() . "\n");
  390. return;
  391. }
  392. print(__FUNCTION__ . ": OK" . "\n");
  393. }
  394. /**
  395. * 获取object meta, 也就是getObjectMeta接口
  396. *
  397. * @param OssClient $ossClient OssClient实例
  398. * @param string $bucket 存储空间名称
  399. * @return null
  400. */
  401. function getObjectMeta($ossClient, $bucket)
  402. {
  403. $object = "oss-php-sdk-test/upload-test-object-name.txt";
  404. try {
  405. $objectMeta = $ossClient->getObjectMeta($bucket, $object);
  406. } catch (OssException $e) {
  407. printf(__FUNCTION__ . ": FAILED\n");
  408. printf($e->getMessage() . "\n");
  409. return;
  410. }
  411. print(__FUNCTION__ . ": OK" . "\n");
  412. if (isset($objectMeta[strtolower('Content-Disposition')]) &&
  413. 'attachment; filename="xxxxxx"' === $objectMeta[strtolower('Content-Disposition')]
  414. ) {
  415. print(__FUNCTION__ . ": ObjectMeta checked OK" . "\n");
  416. } else {
  417. print(__FUNCTION__ . ": ObjectMeta checked FAILED" . "\n");
  418. }
  419. }
  420. /**
  421. * 删除object
  422. *
  423. * @param OssClient $ossClient OssClient实例
  424. * @param string $bucket 存储空间名称
  425. * @return null
  426. */
  427. function deleteObject($ossClient, $bucket)
  428. {
  429. $object = "oss-php-sdk-test/upload-test-object-name.txt";
  430. try {
  431. $ossClient->deleteObject($bucket, $object);
  432. } catch (OssException $e) {
  433. printf(__FUNCTION__ . ": FAILED\n");
  434. printf($e->getMessage() . "\n");
  435. return;
  436. }
  437. print(__FUNCTION__ . ": OK" . "\n");
  438. }
  439. /**
  440. * 批量删除object
  441. *
  442. * @param OssClient $ossClient OssClient实例
  443. * @param string $bucket 存储空间名称
  444. * @return null
  445. */
  446. function deleteObjects($ossClient, $bucket)
  447. {
  448. $objects = array();
  449. $objects[] = "oss-php-sdk-test/upload-test-object-name.txt";
  450. $objects[] = "oss-php-sdk-test/upload-test-object-name.txt.copy";
  451. try {
  452. $ossClient->deleteObjects($bucket, $objects);
  453. } catch (OssException $e) {
  454. printf(__FUNCTION__ . ": FAILED\n");
  455. printf($e->getMessage() . "\n");
  456. return;
  457. }
  458. print(__FUNCTION__ . ": OK" . "\n");
  459. }
  460. /**
  461. * 判断object是否存在
  462. *
  463. * @param OssClient $ossClient OssClient实例
  464. * @param string $bucket 存储空间名称
  465. * @return null
  466. */
  467. function doesObjectExist($ossClient, $bucket)
  468. {
  469. $object = "oss-php-sdk-test/upload-test-object-name.txt";
  470. try {
  471. $exist = $ossClient->doesObjectExist($bucket, $object);
  472. } catch (OssException $e) {
  473. printf(__FUNCTION__ . ": FAILED\n");
  474. printf($e->getMessage() . "\n");
  475. return;
  476. }
  477. print(__FUNCTION__ . ": OK" . "\n");
  478. var_dump($exist);
  479. }