OssClientObjectTest.php 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588
  1. <?php
  2. namespace OSS\Tests;
  3. use OSS\Core\OssException;
  4. use OSS\OssClient;
  5. require_once __DIR__ . DIRECTORY_SEPARATOR . 'TestOssClientBase.php';
  6. class OssClientObjectTest extends TestOssClientBase
  7. {
  8. public function testGetObjectMeta()
  9. {
  10. $object = "oss-php-sdk-test/upload-test-object-name.txt";
  11. try {
  12. $res = $this->ossClient->getObjectMeta($this->bucket, $object);
  13. $this->assertEquals('200', $res['info']['http_code']);
  14. $this->assertEquals('text/plain', $res['content-type']);
  15. $this->assertEquals('Accept-Encoding', $res['vary']);
  16. $this->assertTrue(isset($res['content-length']));
  17. $this->assertFalse(isset($res['content-encoding']));
  18. } catch (OssException $e) {
  19. $this->assertTrue(false);
  20. }
  21. $options = array(OssClient::OSS_HEADERS => array(OssClient::OSS_ACCEPT_ENCODING => 'deflate, gzip'));
  22. try {
  23. $res = $this->ossClient->getObjectMeta($this->bucket, $object, $options);
  24. $this->assertEquals('200', $res['info']['http_code']);
  25. $this->assertEquals('text/plain', $res['content-type']);
  26. $this->assertEquals('Accept-Encoding', $res['vary']);
  27. $this->assertFalse(isset($res['content-length']));
  28. $this->assertEquals('gzip', $res['content-encoding']);
  29. } catch (OssException $e) {
  30. $this->assertTrue(false);
  31. }
  32. }
  33. public function testGetObjectWithAcceptEncoding()
  34. {
  35. $object = "oss-php-sdk-test/upload-test-object-name.txt";
  36. $options = array(OssClient::OSS_HEADERS => array(OssClient::OSS_ACCEPT_ENCODING => 'deflate, gzip'));
  37. try {
  38. $res = $this->ossClient->getObject($this->bucket, $object, $options);
  39. $this->assertEquals(file_get_contents(__FILE__), $res);
  40. } catch (OssException $e) {
  41. $this->assertTrue(false);
  42. }
  43. }
  44. public function testGetObjectWithHeader()
  45. {
  46. $object = "oss-php-sdk-test/upload-test-object-name.txt";
  47. try {
  48. $res = $this->ossClient->getObject($this->bucket, $object, array(OssClient::OSS_LAST_MODIFIED => "xx"));
  49. $this->assertEquals(file_get_contents(__FILE__), $res);
  50. } catch (OssException $e) {
  51. $this->assertEquals('"/ilegal.txt" object name is invalid', $e->getMessage());
  52. }
  53. }
  54. public function testGetObjectWithIleggalEtag()
  55. {
  56. $object = "oss-php-sdk-test/upload-test-object-name.txt";
  57. try {
  58. $res = $this->ossClient->getObject($this->bucket, $object, array(OssClient::OSS_ETAG => "xx"));
  59. $this->assertEquals(file_get_contents(__FILE__), $res);
  60. } catch (OssException $e) {
  61. $this->assertEquals('"/ilegal.txt" object name is invalid', $e->getMessage());
  62. }
  63. }
  64. public function testObject()
  65. {
  66. /**
  67. * 上传本地变量到bucket
  68. */
  69. $object = "oss-php-sdk-test/upload-test-object-name.txt";
  70. $content = file_get_contents(__FILE__);
  71. $options = array(
  72. OssClient::OSS_LENGTH => strlen($content),
  73. OssClient::OSS_HEADERS => array(
  74. 'Expires' => 'Fri, 28 Feb 2020 05:38:42 GMT',
  75. 'Cache-Control' => 'no-cache',
  76. 'Content-Disposition' => 'attachment;filename=oss_download.log',
  77. 'Content-Encoding' => 'utf-8',
  78. 'Content-Language' => 'zh-CN',
  79. 'x-oss-server-side-encryption' => 'AES256',
  80. 'x-oss-meta-self-define-title' => 'user define meta info',
  81. ),
  82. );
  83. try {
  84. $this->ossClient->putObject($this->bucket, $object, $content, $options);
  85. } catch (OssException $e) {
  86. $this->assertFalse(true);
  87. }
  88. try {
  89. $this->ossClient->putObject($this->bucket, $object, $content, $options);
  90. } catch (OssException $e) {
  91. $this->assertFalse(true);
  92. }
  93. try {
  94. $result = $this->ossClient->deleteObjects($this->bucket, "stringtype", $options);
  95. $this->assertEquals('stringtype', $result[0]);
  96. } catch (OssException $e) {
  97. $this->assertEquals('objects must be array', $e->getMessage());
  98. }
  99. try {
  100. $result = $this->ossClient->deleteObjects($this->bucket, "stringtype", $options);
  101. $this->assertFalse(true);
  102. } catch (OssException $e) {
  103. $this->assertEquals('objects must be array', $e->getMessage());
  104. }
  105. try {
  106. $this->ossClient->uploadFile($this->bucket, $object, "notexist.txt", $options);
  107. $this->assertFalse(true);
  108. } catch (OssException $e) {
  109. $this->assertEquals('notexist.txt file does not exist', $e->getMessage());
  110. }
  111. /**
  112. * getObject到本地变量,检查是否match
  113. */
  114. try {
  115. $content = $this->ossClient->getObject($this->bucket, $object);
  116. $this->assertEquals($content, file_get_contents(__FILE__));
  117. } catch (OssException $e) {
  118. $this->assertFalse(true);
  119. }
  120. /**
  121. * getObject的前五个字节
  122. */
  123. try {
  124. $options = array(OssClient::OSS_RANGE => '0-4');
  125. $content = $this->ossClient->getObject($this->bucket, $object, $options);
  126. $this->assertEquals($content, '<?php');
  127. } catch (OssException $e) {
  128. $this->assertFalse(true);
  129. }
  130. /**
  131. * 上传本地文件到object
  132. */
  133. try {
  134. $this->ossClient->uploadFile($this->bucket, $object, __FILE__);
  135. } catch (OssException $e) {
  136. $this->assertFalse(true);
  137. }
  138. /**
  139. * 下载文件到本地变量,检查是否match
  140. */
  141. try {
  142. $content = $this->ossClient->getObject($this->bucket, $object);
  143. $this->assertEquals($content, file_get_contents(__FILE__));
  144. } catch (OssException $e) {
  145. $this->assertFalse(true);
  146. }
  147. /**
  148. * 下载文件到本地文件
  149. */
  150. $localfile = "upload-test-object-name.txt";
  151. $options = array(
  152. OssClient::OSS_FILE_DOWNLOAD => $localfile,
  153. );
  154. try {
  155. $this->ossClient->getObject($this->bucket, $object, $options);
  156. } catch (OssException $e) {
  157. $this->assertFalse(true);
  158. }
  159. $this->assertTrue(file_get_contents($localfile) === file_get_contents(__FILE__));
  160. if (file_exists($localfile)) {
  161. unlink($localfile);
  162. }
  163. /**
  164. * 下载文件到本地文件 no such key
  165. */
  166. $localfile = "upload-test-object-name-no-such-key.txt";
  167. $options = array(
  168. OssClient::OSS_FILE_DOWNLOAD => $localfile,
  169. );
  170. try {
  171. $this->ossClient->getObject($this->bucket, $object . "no-such-key", $options);
  172. $this->assertTrue(false);
  173. } catch (OssException $e) {
  174. $this->assertTrue(true);
  175. $this->assertFalse(file_exists($localfile));
  176. if (strpos($e, "The specified key does not exist") == false)
  177. {
  178. $this->assertTrue(true);
  179. }
  180. }
  181. /**
  182. * 下载文件到内容 no such key
  183. */
  184. try {
  185. $result = $this->ossClient->getObject($this->bucket, $object . "no-such-key");
  186. $this->assertTrue(false);
  187. } catch (OssException $e) {
  188. $this->assertTrue(true);
  189. if (strpos($e, "The specified key does not exist") == false)
  190. {
  191. $this->assertTrue(true);
  192. }
  193. }
  194. /**
  195. * 复制object
  196. */
  197. $to_bucket = $this->bucket;
  198. $to_object = $object . '.copy';
  199. $options = array();
  200. try {
  201. $result = $this->ossClient->copyObject($this->bucket, $object, $to_bucket, $to_object, $options);
  202. $this->assertFalse(empty($result));
  203. $this->assertEquals(strlen("2016-11-21T03:46:58.000Z"), strlen($result[0]));
  204. $this->assertEquals(strlen("\"5B3C1A2E053D763E1B002CC607C5A0FE\""), strlen($result[1]));
  205. } catch (OssException $e) {
  206. $this->assertFalse(true);
  207. var_dump($e->getMessage());
  208. }
  209. /**
  210. * 检查复制的是否相同
  211. */
  212. try {
  213. $content = $this->ossClient->getObject($this->bucket, $to_object);
  214. $this->assertEquals($content, file_get_contents(__FILE__));
  215. } catch (OssException $e) {
  216. $this->assertFalse(true);
  217. }
  218. /**
  219. * 列出bucket内的文件列表
  220. */
  221. $prefix = '';
  222. $delimiter = '/';
  223. $next_marker = '';
  224. $maxkeys = 1000;
  225. $options = array(
  226. 'delimiter' => $delimiter,
  227. 'prefix' => $prefix,
  228. 'max-keys' => $maxkeys,
  229. 'marker' => $next_marker,
  230. );
  231. try {
  232. $listObjectInfo = $this->ossClient->listObjects($this->bucket, $options);
  233. $objectList = $listObjectInfo->getObjectList();
  234. $prefixList = $listObjectInfo->getPrefixList();
  235. $this->assertNotNull($objectList);
  236. $this->assertNotNull($prefixList);
  237. $this->assertTrue(is_array($objectList));
  238. $this->assertTrue(is_array($prefixList));
  239. } catch (OssException $e) {
  240. $this->assertTrue(false);
  241. }
  242. /**
  243. * 设置文件的meta信息
  244. */
  245. $from_bucket = $this->bucket;
  246. $from_object = "oss-php-sdk-test/upload-test-object-name.txt";
  247. $to_bucket = $from_bucket;
  248. $to_object = $from_object;
  249. $copy_options = array(
  250. OssClient::OSS_HEADERS => array(
  251. 'Expires' => '2012-10-01 08:00:00',
  252. 'Content-Disposition' => 'attachment; filename="xxxxxx"',
  253. ),
  254. );
  255. try {
  256. $this->ossClient->copyObject($from_bucket, $from_object, $to_bucket, $to_object, $copy_options);
  257. } catch (OssException $e) {
  258. $this->assertFalse(true);
  259. }
  260. /**
  261. * 获取文件的meta信息
  262. */
  263. $object = "oss-php-sdk-test/upload-test-object-name.txt";
  264. try {
  265. $objectMeta = $this->ossClient->getObjectMeta($this->bucket, $object);
  266. $this->assertEquals('attachment; filename="xxxxxx"', $objectMeta[strtolower('Content-Disposition')]);
  267. } catch (OssException $e) {
  268. $this->assertFalse(true);
  269. }
  270. /**
  271. * 删除单个文件
  272. */
  273. $object = "oss-php-sdk-test/upload-test-object-name.txt";
  274. try {
  275. $this->assertTrue($this->ossClient->doesObjectExist($this->bucket, $object));
  276. $this->ossClient->deleteObject($this->bucket, $object);
  277. $this->assertFalse($this->ossClient->doesObjectExist($this->bucket, $object));
  278. } catch (OssException $e) {
  279. $this->assertFalse(true);
  280. }
  281. /**
  282. * 删除多个个文件
  283. */
  284. $object1 = "oss-php-sdk-test/upload-test-object-name.txt";
  285. $object2 = "oss-php-sdk-test/upload-test-object-name.txt.copy";
  286. $list = array($object1, $object2);
  287. try {
  288. $this->assertTrue($this->ossClient->doesObjectExist($this->bucket, $object2));
  289. $result = $this->ossClient->deleteObjects($this->bucket, $list);
  290. $this->assertEquals($list[1], $result[0]);
  291. $this->assertEquals($list[0], $result[1]);
  292. $result = $this->ossClient->deleteObjects($this->bucket, $list, array('quiet' => 'true'));
  293. $this->assertEquals(array(), $result);
  294. $this->assertFalse($this->ossClient->doesObjectExist($this->bucket, $object2));
  295. } catch (OssException $e) {
  296. $this->assertFalse(true);
  297. }
  298. }
  299. public function testAppendObject()
  300. {
  301. $object = "oss-php-sdk-test/append-test-object-name.txt";
  302. $content_array = array('Hello OSS', 'Hi OSS', 'OSS OK');
  303. /**
  304. * 追加上传字符串
  305. */
  306. try {
  307. $position = $this->ossClient->appendObject($this->bucket, $object, $content_array[0], 0);
  308. $this->assertEquals($position, strlen($content_array[0]));
  309. $position = $this->ossClient->appendObject($this->bucket, $object, $content_array[1], $position);
  310. $this->assertEquals($position, strlen($content_array[0]) + strlen($content_array[1]));
  311. $position = $this->ossClient->appendObject($this->bucket, $object, $content_array[2], $position);
  312. $this->assertEquals($position, strlen($content_array[0]) + strlen($content_array[1]) + strlen($content_array[1]));
  313. } catch (OssException $e) {
  314. $this->assertFalse(true);
  315. }
  316. /**
  317. * 检查内容的是否相同
  318. */
  319. try {
  320. $content = $this->ossClient->getObject($this->bucket, $object);
  321. $this->assertEquals($content, implode($content_array));
  322. } catch (OssException $e) {
  323. $this->assertFalse(true);
  324. }
  325. /**
  326. * 删除测试object
  327. */
  328. try {
  329. $this->ossClient->deleteObject($this->bucket, $object);
  330. } catch (OssException $e) {
  331. $this->assertFalse(true);
  332. }
  333. /**
  334. * 追加上传本地文件
  335. */
  336. try {
  337. $position = $this->ossClient->appendFile($this->bucket, $object, __FILE__, 0);
  338. $this->assertEquals($position, filesize(__FILE__));
  339. $position = $this->ossClient->appendFile($this->bucket, $object, __FILE__, $position);
  340. $this->assertEquals($position, filesize(__FILE__) * 2);
  341. } catch (OssException $e) {
  342. $this->assertFalse(true);
  343. }
  344. /**
  345. * 检查复制的是否相同
  346. */
  347. try {
  348. $content = $this->ossClient->getObject($this->bucket, $object);
  349. $this->assertEquals($content, file_get_contents(__FILE__) . file_get_contents(__FILE__));
  350. } catch (OssException $e) {
  351. $this->assertFalse(true);
  352. }
  353. /**
  354. * 删除测试object
  355. */
  356. try {
  357. $this->ossClient->deleteObject($this->bucket, $object);
  358. } catch (OssException $e) {
  359. $this->assertFalse(true);
  360. }
  361. $options = array(
  362. OssClient::OSS_HEADERS => array(
  363. 'Expires' => '2012-10-01 08:00:00',
  364. 'Content-Disposition' => 'attachment; filename="xxxxxx"',
  365. ),
  366. );
  367. /**
  368. * 带option的追加上传
  369. */
  370. try {
  371. $position = $this->ossClient->appendObject($this->bucket, $object, "Hello OSS, ", 0, $options);
  372. $position = $this->ossClient->appendObject($this->bucket, $object, "Hi OSS.", $position);
  373. } catch (OssException $e) {
  374. $this->assertFalse(true);
  375. }
  376. /**
  377. * 获取文件的meta信息
  378. */
  379. try {
  380. $objectMeta = $this->ossClient->getObjectMeta($this->bucket, $object);
  381. $this->assertEquals('attachment; filename="xxxxxx"', $objectMeta[strtolower('Content-Disposition')]);
  382. } catch (OssException $e) {
  383. $this->assertFalse(true);
  384. }
  385. /**
  386. * 删除测试object
  387. */
  388. try {
  389. $this->ossClient->deleteObject($this->bucket, $object);
  390. } catch (OssException $e) {
  391. $this->assertFalse(true);
  392. }
  393. }
  394. public function testPutIllelObject()
  395. {
  396. $object = "/ilegal.txt";
  397. try {
  398. $this->ossClient->putObject($this->bucket, $object, "hi", null);
  399. $this->assertFalse(true);
  400. } catch (OssException $e) {
  401. $this->assertEquals('"/ilegal.txt" object name is invalid', $e->getMessage());
  402. }
  403. }
  404. public function testCheckMD5()
  405. {
  406. $object = "oss-php-sdk-test/upload-test-object-name.txt";
  407. $content = file_get_contents(__FILE__);
  408. $options = array(OssClient::OSS_CHECK_MD5 => true);
  409. /**
  410. * 上传数据开启MD5
  411. */
  412. try {
  413. $this->ossClient->putObject($this->bucket, $object, $content, $options);
  414. } catch (OssException $e) {
  415. $this->assertFalse(true);
  416. }
  417. /**
  418. * 检查复制的是否相同
  419. */
  420. try {
  421. $content = $this->ossClient->getObject($this->bucket, $object);
  422. $this->assertEquals($content, file_get_contents(__FILE__));
  423. } catch (OssException $e) {
  424. $this->assertFalse(true);
  425. }
  426. /**
  427. * 上传文件开启MD5
  428. */
  429. try {
  430. $this->ossClient->uploadFile($this->bucket, $object, __FILE__, $options);
  431. } catch (OssException $e) {
  432. $this->assertFalse(true);
  433. }
  434. /**
  435. * 检查复制的是否相同
  436. */
  437. try {
  438. $content = $this->ossClient->getObject($this->bucket, $object);
  439. $this->assertEquals($content, file_get_contents(__FILE__));
  440. } catch (OssException $e) {
  441. $this->assertFalse(true);
  442. }
  443. /**
  444. * 删除测试object
  445. */
  446. try {
  447. $this->ossClient->deleteObject($this->bucket, $object);
  448. } catch (OssException $e) {
  449. $this->assertFalse(true);
  450. }
  451. $object = "oss-php-sdk-test/append-test-object-name.txt";
  452. $content_array = array('Hello OSS', 'Hi OSS', 'OSS OK');
  453. $options = array(OssClient::OSS_CHECK_MD5 => true);
  454. /**
  455. * 追加上传字符串
  456. */
  457. try {
  458. $position = $this->ossClient->appendObject($this->bucket, $object, $content_array[0], 0, $options);
  459. $this->assertEquals($position, strlen($content_array[0]));
  460. $position = $this->ossClient->appendObject($this->bucket, $object, $content_array[1], $position, $options);
  461. $this->assertEquals($position, strlen($content_array[0]) + strlen($content_array[1]));
  462. $position = $this->ossClient->appendObject($this->bucket, $object, $content_array[2], $position, $options);
  463. $this->assertEquals($position, strlen($content_array[0]) + strlen($content_array[1]) + strlen($content_array[1]));
  464. } catch (OssException $e) {
  465. $this->assertFalse(true);
  466. }
  467. /**
  468. * 检查内容的是否相同
  469. */
  470. try {
  471. $content = $this->ossClient->getObject($this->bucket, $object);
  472. $this->assertEquals($content, implode($content_array));
  473. } catch (OssException $e) {
  474. $this->assertFalse(true);
  475. }
  476. /**
  477. * 删除测试object
  478. */
  479. try {
  480. $this->ossClient->deleteObject($this->bucket, $object);
  481. } catch (OssException $e) {
  482. $this->assertFalse(true);
  483. }
  484. /**
  485. * 追加上传本地文件
  486. */
  487. try {
  488. $position = $this->ossClient->appendFile($this->bucket, $object, __FILE__, 0, $options);
  489. $this->assertEquals($position, filesize(__FILE__));
  490. $position = $this->ossClient->appendFile($this->bucket, $object, __FILE__, $position, $options);
  491. $this->assertEquals($position, filesize(__FILE__) * 2);
  492. } catch (OssException $e) {
  493. $this->assertFalse(true);
  494. }
  495. /**
  496. * 检查复制的是否相同
  497. */
  498. try {
  499. $content = $this->ossClient->getObject($this->bucket, $object);
  500. $this->assertEquals($content, file_get_contents(__FILE__) . file_get_contents(__FILE__));
  501. } catch (OssException $e) {
  502. $this->assertFalse(true);
  503. }
  504. /**
  505. * 删除测试object
  506. */
  507. try {
  508. $this->ossClient->deleteObject($this->bucket, $object);
  509. } catch (OssException $e) {
  510. $this->assertFalse(true);
  511. }
  512. }
  513. public function setUp()
  514. {
  515. parent::setUp();
  516. $this->ossClient->putObject($this->bucket, 'oss-php-sdk-test/upload-test-object-name.txt', file_get_contents(__FILE__));
  517. }
  518. }