OssClientSignatureV4Test.php 50 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409
  1. <?php
  2. namespace OSS\Tests;
  3. use OSS\Core\OssException;
  4. use OSS\Credentials\StaticCredentialsProvider;
  5. use OSS\OssClient;
  6. require_once __DIR__ . DIRECTORY_SEPARATOR . 'TestOssClientBase.php';
  7. class OssClientSignatureV4Test extends TestOssClientBase
  8. {
  9. /**
  10. * @var OssClient
  11. */
  12. protected $stsOssClient;
  13. public function testBaseInterfaceForObject()
  14. {
  15. $object = "oss-php-sdk-test/upload-test-object-name.txt";
  16. try {
  17. $this->ossClient->putObject($this->bucket, $object, file_get_contents(__FILE__));
  18. } catch (OssException $e) {
  19. $this->assertTrue(false);
  20. }
  21. // test GetObjectMeta
  22. try {
  23. $res = $this->ossClient->getObjectMeta($this->bucket, $object);
  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-Encoding']));
  28. } catch (OssException $e) {
  29. $this->assertTrue(false);
  30. }
  31. $options = array(OssClient::OSS_HEADERS => array(OssClient::OSS_ACCEPT_ENCODING => 'deflate, gzip'));
  32. try {
  33. $res = $this->ossClient->getObjectMeta($this->bucket, $object, $options);
  34. $this->assertEquals('200', $res['info']['http_code']);
  35. $this->assertEquals('text/plain', $res['content-type']);
  36. $this->assertEquals('Accept-Encoding', $res['vary']);
  37. $this->assertFalse(isset($res['content-length']));
  38. $this->assertEquals('gzip', $res['content-encoding']);
  39. } catch (OssException $e) {
  40. $this->assertTrue(false);
  41. }
  42. $options = array(OssClient::OSS_HEADERS => array(OssClient::OSS_ACCEPT_ENCODING => 'deflate, gzip'));
  43. try {
  44. $res = $this->ossClient->getObject($this->bucket, $object, $options);
  45. $this->assertEquals(file_get_contents(__FILE__), $res);
  46. } catch (OssException $e) {
  47. $this->assertTrue(false);
  48. }
  49. try {
  50. $res = $this->ossClient->getObject($this->bucket, $object, array(OssClient::OSS_LAST_MODIFIED => "xx"));
  51. $this->assertEquals(file_get_contents(__FILE__), $res);
  52. } catch (OssException $e) {
  53. $this->assertEquals('"/ilegal.txt" object name is invalid', $e->getMessage());
  54. }
  55. try {
  56. $res = $this->ossClient->getObject($this->bucket, $object, array(OssClient::OSS_ETAG => "xx"));
  57. $this->assertEquals(file_get_contents(__FILE__), $res);
  58. } catch (OssException $e) {
  59. $this->assertEquals('"/ilegal.txt" object name is invalid', $e->getMessage());
  60. }
  61. $content = file_get_contents(__FILE__);
  62. $options = array(
  63. OssClient::OSS_LENGTH => strlen($content),
  64. OssClient::OSS_HEADERS => array(
  65. 'Expires' => 'Fri, 28 Feb 2020 05:38:42 GMT',
  66. 'Cache-Control' => 'no-cache',
  67. 'Content-Disposition' => 'attachment;filename=oss_download.log',
  68. 'Content-Language' => 'zh-CN',
  69. 'x-oss-server-side-encryption' => 'AES256',
  70. 'x-oss-meta-self-define-title' => 'user define meta info',
  71. ),
  72. );
  73. try {
  74. $this->ossClient->putObject($this->bucket, $object, $content, $options);
  75. } catch (OssException $e) {
  76. $this->assertFalse(true);
  77. }
  78. try {
  79. $result = $this->ossClient->deleteObjects($this->bucket, "stringtype", $options);
  80. $this->assertEquals('stringtype', $result[0]);
  81. } catch (OssException $e) {
  82. $this->assertEquals('objects must be array', $e->getMessage());
  83. }
  84. try {
  85. $this->ossClient->uploadFile($this->bucket, $object, "notexist.txt", $options);
  86. $this->assertFalse(true);
  87. } catch (OssException $e) {
  88. $this->assertEquals('notexist.txt file does not exist', $e->getMessage());
  89. }
  90. $content = file_get_contents(__FILE__);
  91. $options = array(
  92. OssClient::OSS_LENGTH => strlen($content),
  93. OssClient::OSS_HEADERS => array(
  94. 'Expires' => 'Fri, 28 Feb 2020 05:38:42 GMT',
  95. 'Cache-Control' => 'no-cache',
  96. 'Content-Disposition' => 'attachment;filename=oss_download.log',
  97. 'Content-Language' => 'zh-CN',
  98. 'x-oss-server-side-encryption' => 'AES256',
  99. 'x-oss-meta-self-define-title' => 'user define meta info',
  100. ),
  101. );
  102. try {
  103. $this->ossClient->putObject($this->bucket, $object, $content, $options);
  104. } catch (OssException $e) {
  105. $this->assertFalse(true);
  106. }
  107. /**
  108. * GetObject to the local variable and check for match
  109. */
  110. try {
  111. $content = $this->ossClient->getObject($this->bucket, $object);
  112. $this->assertEquals($content, file_get_contents(__FILE__));
  113. } catch (OssException $e) {
  114. $this->assertFalse(true);
  115. }
  116. /**
  117. * GetObject first five bytes
  118. */
  119. try {
  120. $options = array(OssClient::OSS_RANGE => '0-4');
  121. $content = $this->ossClient->getObject($this->bucket, $object, $options);
  122. $this->assertEquals($content, '<?php');
  123. } catch (OssException $e) {
  124. $this->assertFalse(true);
  125. }
  126. /**
  127. * Upload the local file to object
  128. */
  129. try {
  130. $this->ossClient->uploadFile($this->bucket, $object, __FILE__);
  131. } catch (OssException $e) {
  132. $this->assertFalse(true);
  133. }
  134. /**
  135. * Download the file to the local variable and check for match.
  136. */
  137. try {
  138. $content = $this->ossClient->getObject($this->bucket, $object);
  139. $this->assertEquals($content, file_get_contents(__FILE__));
  140. } catch (OssException $e) {
  141. $this->assertFalse(true);
  142. }
  143. /**
  144. * Download the file to the local file
  145. */
  146. $localfile = "upload-test-object-name.txt";
  147. $options = array(
  148. OssClient::OSS_FILE_DOWNLOAD => $localfile,
  149. );
  150. try {
  151. $this->ossClient->getObject($this->bucket, $object, $options);
  152. } catch (OssException $e) {
  153. $this->assertFalse(true);
  154. }
  155. $this->assertTrue(file_get_contents($localfile) === file_get_contents(__FILE__));
  156. if (file_exists($localfile)) {
  157. unlink($localfile);
  158. }
  159. /**
  160. * Download the file to the local file. no such key
  161. */
  162. $localfile = "upload-test-object-name-no-such-key.txt";
  163. $options = array(
  164. OssClient::OSS_FILE_DOWNLOAD => $localfile,
  165. );
  166. try {
  167. $this->ossClient->getObject($this->bucket, $object . "no-such-key", $options);
  168. $this->assertTrue(false);
  169. } catch (OssException $e) {
  170. $this->assertTrue(true);
  171. $this->assertFalse(file_exists($localfile));
  172. if (strpos($e, "The specified key does not exist") == false) {
  173. $this->assertTrue(true);
  174. }
  175. }
  176. /**
  177. * Download the file to the content. no such key
  178. */
  179. try {
  180. $result = $this->ossClient->getObject($this->bucket, $object . "no-such-key");
  181. $this->assertTrue(false);
  182. } catch (OssException $e) {
  183. $this->assertTrue(true);
  184. if (strpos($e, "The specified key does not exist") == false) {
  185. $this->assertTrue(true);
  186. }
  187. }
  188. /**
  189. * Copy object
  190. */
  191. $to_bucket = $this->bucket;
  192. $to_object = $object . '.copy';
  193. $options = array();
  194. try {
  195. $result = $this->ossClient->copyObject($this->bucket, $object, $to_bucket, $to_object, $options);
  196. $this->assertFalse(empty($result));
  197. $this->assertEquals(strlen("2016-11-21T03:46:58.000Z"), strlen($result[0]));
  198. $this->assertEquals(strlen("\"5B3C1A2E053D763E1B002CC607C5A0FE\""), strlen($result[1]));
  199. } catch (OssException $e) {
  200. $this->assertFalse(true);
  201. var_dump($e->getMessage());
  202. }
  203. /**
  204. * Check if the replication is the same
  205. */
  206. try {
  207. $content = $this->ossClient->getObject($this->bucket, $to_object);
  208. $this->assertEquals($content, file_get_contents(__FILE__));
  209. } catch (OssException $e) {
  210. $this->assertFalse(true);
  211. }
  212. /**
  213. * List the files in your bucket.
  214. */
  215. $prefix = '';
  216. $delimiter = '/';
  217. $next_marker = '';
  218. $maxkeys = 1000;
  219. $options = array(
  220. 'delimiter' => $delimiter,
  221. 'prefix' => $prefix,
  222. 'max-keys' => $maxkeys,
  223. 'marker' => $next_marker,
  224. );
  225. try {
  226. $listObjectInfo = $this->ossClient->listObjects($this->bucket, $options);
  227. $objectList = $listObjectInfo->getObjectList();
  228. $prefixList = $listObjectInfo->getPrefixList();
  229. $this->assertNotNull($objectList);
  230. $this->assertNotNull($prefixList);
  231. $this->assertTrue(is_array($objectList));
  232. $this->assertTrue(is_array($prefixList));
  233. } catch (OssException $e) {
  234. $this->assertTrue(false);
  235. }
  236. /**
  237. * Set the meta information for the file
  238. */
  239. $from_bucket = $this->bucket;
  240. $from_object = "oss-php-sdk-test/upload-test-object-name.txt";
  241. $to_bucket = $from_bucket;
  242. $to_object = $from_object;
  243. $copy_options = array(
  244. OssClient::OSS_HEADERS => array(
  245. 'Expires' => '2012-10-01 08:00:00',
  246. 'Content-Disposition' => 'attachment; filename="xxxxxx"',
  247. ),
  248. );
  249. try {
  250. $this->ossClient->copyObject($from_bucket, $from_object, $to_bucket, $to_object, $copy_options);
  251. } catch (OssException $e) {
  252. $this->assertFalse(true);
  253. }
  254. /**
  255. * Get the meta information for the file
  256. */
  257. $object = "oss-php-sdk-test/upload-test-object-name.txt";
  258. try {
  259. $objectMeta = $this->ossClient->getObjectMeta($this->bucket, $object);
  260. $this->assertEquals('attachment; filename="xxxxxx"', $objectMeta[strtolower('Content-Disposition')]);
  261. } catch (OssException $e) {
  262. $this->assertFalse(true);
  263. }
  264. /**
  265. * Delete single file
  266. */
  267. $object = "oss-php-sdk-test/upload-test-object-name.txt";
  268. try {
  269. $this->assertTrue($this->ossClient->doesObjectExist($this->bucket, $object));
  270. $this->ossClient->deleteObject($this->bucket, $object);
  271. $this->assertFalse($this->ossClient->doesObjectExist($this->bucket, $object));
  272. } catch (OssException $e) {
  273. $this->assertFalse(true);
  274. }
  275. /**
  276. * Delete multiple files
  277. */
  278. $object1 = "oss-php-sdk-test/upload-test-object-name.txt";
  279. $object2 = "oss-php-sdk-test/upload-test-object-name.txt.copy";
  280. $list = array($object1, $object2);
  281. try {
  282. $this->assertTrue($this->ossClient->doesObjectExist($this->bucket, $object2));
  283. $result = $this->ossClient->deleteObjects($this->bucket, $list);
  284. $this->assertEquals($list[0], $result[0]);
  285. $this->assertEquals($list[1], $result[1]);
  286. $result = $this->ossClient->deleteObjects($this->bucket, $list, array('quiet' => 'true'));
  287. $this->assertEquals(array(), $result);
  288. $this->assertFalse($this->ossClient->doesObjectExist($this->bucket, $object2));
  289. $this->ossClient->putObject($this->bucket, $object, $content);
  290. $this->assertTrue($this->ossClient->doesObjectExist($this->bucket, $object));
  291. $result = $this->ossClient->deleteObjects($this->bucket, $list, array('quiet' => true));
  292. $this->assertEquals(array(), $result);
  293. $this->assertFalse($this->ossClient->doesObjectExist($this->bucket, $object));
  294. } catch (OssException $e) {
  295. $this->assertFalse(true);
  296. }
  297. $content_array = array('Hello OSS', 'Hi OSS', 'OSS OK');
  298. /**
  299. * Append the upload string
  300. */
  301. try {
  302. $position = $this->ossClient->appendObject($this->bucket, $object, $content_array[0], 0);
  303. $this->assertEquals($position, strlen($content_array[0]));
  304. $position = $this->ossClient->appendObject($this->bucket, $object, $content_array[1], $position);
  305. $this->assertEquals($position, strlen($content_array[0]) + strlen($content_array[1]));
  306. $position = $this->ossClient->appendObject($this->bucket, $object, $content_array[2], $position, array(OssClient::OSS_LENGTH => strlen($content_array[2])));
  307. $this->assertEquals($position, strlen($content_array[0]) + strlen($content_array[1]) + strlen($content_array[2]));
  308. } catch (OssException $e) {
  309. print_r($e->getMessage());
  310. $this->assertFalse(true);
  311. }
  312. /**
  313. * Check if the content is the same
  314. */
  315. try {
  316. $content = $this->ossClient->getObject($this->bucket, $object);
  317. $this->assertEquals($content, implode($content_array));
  318. } catch (OssException $e) {
  319. $this->assertFalse(true);
  320. }
  321. /**
  322. * Delete test object
  323. */
  324. try {
  325. $this->ossClient->deleteObject($this->bucket, $object);
  326. } catch (OssException $e) {
  327. $this->assertFalse(true);
  328. }
  329. /**
  330. * Append the upload of invalid local files
  331. */
  332. try {
  333. $position = $this->ossClient->appendFile($this->bucket, $object, "invalid-file-path", 0);
  334. $this->assertTrue(false);
  335. } catch (OssException $e) {
  336. $this->assertTrue(true);
  337. }
  338. /**
  339. * Append the upload of local files
  340. */
  341. try {
  342. $position = $this->ossClient->appendFile($this->bucket, $object, __FILE__, 0);
  343. $this->assertEquals($position, sprintf('%u', filesize(__FILE__)));
  344. $position = $this->ossClient->appendFile($this->bucket, $object, __FILE__, $position);
  345. $this->assertEquals($position, sprintf('%u', filesize(__FILE__)) * 2);
  346. } catch (OssException $e) {
  347. $this->assertFalse(true);
  348. }
  349. /**
  350. * Check if the replication is the same
  351. */
  352. try {
  353. $content = $this->ossClient->getObject($this->bucket, $object);
  354. $this->assertEquals($content, file_get_contents(__FILE__) . file_get_contents(__FILE__));
  355. } catch (OssException $e) {
  356. $this->assertFalse(true);
  357. }
  358. /**
  359. * Delete test object
  360. */
  361. try {
  362. $this->ossClient->deleteObject($this->bucket, $object);
  363. } catch (OssException $e) {
  364. $this->assertFalse(true);
  365. }
  366. $options = array(
  367. OssClient::OSS_HEADERS => array(
  368. 'Expires' => '2012-10-01 08:00:00',
  369. 'Content-Disposition' => 'attachment; filename="xxxxxx"',
  370. ),
  371. );
  372. /**
  373. * Append upload with option
  374. */
  375. try {
  376. $position = $this->ossClient->appendObject($this->bucket, $object, "Hello OSS, ", 0, $options);
  377. $position = $this->ossClient->appendObject($this->bucket, $object, "Hi OSS.", $position);
  378. } catch (OssException $e) {
  379. $this->assertFalse(true);
  380. }
  381. /**
  382. * Get the meta information for the file
  383. */
  384. try {
  385. $objectMeta = $this->ossClient->getObjectMeta($this->bucket, $object);
  386. $this->assertEquals('attachment; filename="xxxxxx"', $objectMeta[strtolower('Content-Disposition')]);
  387. } catch (OssException $e) {
  388. $this->assertFalse(true);
  389. }
  390. /**
  391. * Delete test object
  392. */
  393. try {
  394. $this->ossClient->deleteObject($this->bucket, $object);
  395. } catch (OssException $e) {
  396. $this->assertFalse(true);
  397. }
  398. $options = array(OssClient::OSS_CHECK_MD5 => true);
  399. $content = file_get_contents(__FILE__);
  400. /**
  401. * Upload data to start MD5
  402. */
  403. try {
  404. $this->ossClient->putObject($this->bucket, $object, $content, $options);
  405. } catch (OssException $e) {
  406. $this->assertFalse(true);
  407. }
  408. /**
  409. * Check if the replication is the same
  410. */
  411. try {
  412. $content = $this->ossClient->getObject($this->bucket, $object);
  413. $this->assertEquals($content, file_get_contents(__FILE__));
  414. } catch (OssException $e) {
  415. $this->assertFalse(true);
  416. }
  417. /**
  418. * Upload file to start MD5
  419. */
  420. try {
  421. $this->ossClient->uploadFile($this->bucket, $object, __FILE__, $options);
  422. } catch (OssException $e) {
  423. $this->assertFalse(true);
  424. }
  425. /**
  426. * Check if the replication is the same
  427. */
  428. try {
  429. $content = $this->ossClient->getObject($this->bucket, $object);
  430. $this->assertEquals($content, file_get_contents(__FILE__));
  431. } catch (OssException $e) {
  432. $this->assertFalse(true);
  433. }
  434. /**
  435. * Delete test object
  436. */
  437. try {
  438. $this->ossClient->deleteObject($this->bucket, $object);
  439. } catch (OssException $e) {
  440. $this->assertFalse(true);
  441. }
  442. $object = "oss-php-sdk-test/append-test-object-name.txt";
  443. $content_array = array('Hello OSS', 'Hi OSS', 'OSS OK');
  444. $options = array(OssClient::OSS_CHECK_MD5 => true);
  445. /**
  446. * Append the upload string
  447. */
  448. try {
  449. $position = $this->ossClient->appendObject($this->bucket, $object, $content_array[0], 0, $options);
  450. $this->assertEquals($position, strlen($content_array[0]));
  451. $position = $this->ossClient->appendObject($this->bucket, $object, $content_array[1], $position, $options);
  452. $this->assertEquals($position, strlen($content_array[0]) + strlen($content_array[1]));
  453. $position = $this->ossClient->appendObject($this->bucket, $object, $content_array[2], $position, $options);
  454. $this->assertEquals($position, strlen($content_array[0]) + strlen($content_array[1]) + strlen($content_array[1]));
  455. } catch (OssException $e) {
  456. $this->assertFalse(true);
  457. }
  458. /**
  459. * Check if the content is the same
  460. */
  461. try {
  462. $content = $this->ossClient->getObject($this->bucket, $object);
  463. $this->assertEquals($content, implode($content_array));
  464. } catch (OssException $e) {
  465. $this->assertFalse(true);
  466. }
  467. /**
  468. * Delete test object
  469. */
  470. try {
  471. $this->ossClient->deleteObject($this->bucket, $object);
  472. } catch (OssException $e) {
  473. $this->assertFalse(true);
  474. }
  475. /**
  476. * Append upload of local files
  477. */
  478. try {
  479. $position = $this->ossClient->appendFile($this->bucket, $object, __FILE__, 0, $options);
  480. $this->assertEquals($position, sprintf('%u', filesize(__FILE__)));
  481. $position = $this->ossClient->appendFile($this->bucket, $object, __FILE__, $position, $options);
  482. $this->assertEquals($position, (sprintf('%u', filesize(__FILE__)) * 2));
  483. } catch (OssException $e) {
  484. $this->assertFalse(true);
  485. }
  486. /**
  487. * Check if the replication is the same
  488. */
  489. try {
  490. $content = $this->ossClient->getObject($this->bucket, $object);
  491. $this->assertEquals($content, file_get_contents(__FILE__) . file_get_contents(__FILE__));
  492. } catch (OssException $e) {
  493. $this->assertFalse(true);
  494. }
  495. /**
  496. * delete test object
  497. */
  498. try {
  499. $this->ossClient->deleteObject($this->bucket, $object);
  500. } catch (OssException $e) {
  501. $this->assertFalse(true);
  502. }
  503. $options = array(
  504. OssClient::OSS_HEADERS => array(
  505. "Content-Type" => "application/octet-stream",
  506. "name" => "aliyun",
  507. "email" => "aliyun@aliyun.com",
  508. ),
  509. OssClient::OSS_ADDITIONAL_HEADERS => array('name', 'email')
  510. );
  511. try {
  512. $this->ossClient->uploadFile($this->bucket, $object, __FILE__, $options);
  513. } catch (OssException $e) {
  514. print_r($e->getMessage());
  515. $this->assertFalse(true);
  516. }
  517. try {
  518. $content = $this->ossClient->getObject($this->bucket, $object, $options);
  519. } catch (OssException $e) {
  520. $this->assertFalse(true);
  521. }
  522. /**
  523. * delete test object
  524. */
  525. try {
  526. $this->ossClient->deleteObject($this->bucket, $object, $options);
  527. } catch (OssException $e) {
  528. $this->assertFalse(true);
  529. }
  530. }
  531. public function testObjectKeyWithQuestionMark()
  532. {
  533. /**
  534. * Upload the local variable to bucket
  535. */
  536. $object = "oss-php-sdk-test/??/upload-test-object-name???123??123??.txt";
  537. $content = file_get_contents(__FILE__);
  538. $options = array(
  539. OssClient::OSS_LENGTH => strlen($content),
  540. OssClient::OSS_HEADERS => array(
  541. 'Expires' => 'Fri, 28 Feb 2020 05:38:42 GMT',
  542. 'Cache-Control' => 'no-cache',
  543. 'Content-Disposition' => 'attachment;filename=oss_download.log',
  544. 'Content-Language' => 'zh-CN',
  545. 'x-oss-server-side-encryption' => 'AES256',
  546. 'x-oss-meta-self-define-title' => 'user define meta info',
  547. ),
  548. );
  549. try {
  550. $this->ossClient->putObject($this->bucket, $object, $content, $options);
  551. } catch (OssException $e) {
  552. $this->assertFalse(true);
  553. }
  554. try {
  555. $this->ossClient->putObject($this->bucket, $object, $content, $options);
  556. } catch (OssException $e) {
  557. $this->assertFalse(true);
  558. }
  559. /**
  560. * GetObject to the local variable and check for match
  561. */
  562. try {
  563. $content = $this->ossClient->getObject($this->bucket, $object);
  564. $this->assertEquals($content, file_get_contents(__FILE__));
  565. } catch (OssException $e) {
  566. $this->assertFalse(true);
  567. }
  568. /**
  569. * GetObject first five bytes
  570. */
  571. try {
  572. $options = array(OssClient::OSS_RANGE => '0-4');
  573. $content = $this->ossClient->getObject($this->bucket, $object, $options);
  574. $this->assertEquals($content, '<?php');
  575. } catch (OssException $e) {
  576. $this->assertFalse(true);
  577. }
  578. /**
  579. * Upload the local file to object
  580. */
  581. try {
  582. $this->ossClient->uploadFile($this->bucket, $object, __FILE__);
  583. } catch (OssException $e) {
  584. $this->assertFalse(true);
  585. }
  586. /**
  587. * Download the file to the local variable and check for match.
  588. */
  589. try {
  590. $content = $this->ossClient->getObject($this->bucket, $object);
  591. $this->assertEquals($content, file_get_contents(__FILE__));
  592. } catch (OssException $e) {
  593. $this->assertFalse(true);
  594. }
  595. /**
  596. * Copy object
  597. */
  598. $to_bucket = $this->bucket;
  599. $to_object = $object . '.copy';
  600. $options = array();
  601. try {
  602. $result = $this->ossClient->copyObject($this->bucket, $object, $to_bucket, $to_object, $options);
  603. $this->assertFalse(empty($result));
  604. $this->assertEquals(strlen("2016-11-21T03:46:58.000Z"), strlen($result[0]));
  605. $this->assertEquals(strlen("\"5B3C1A2E053D763E1B002CC607C5A0FE\""), strlen($result[1]));
  606. } catch (OssException $e) {
  607. $this->assertFalse(true);
  608. var_dump($e->getMessage());
  609. }
  610. /**
  611. * Check if the replication is the same
  612. */
  613. try {
  614. $content = $this->ossClient->getObject($this->bucket, $to_object);
  615. $this->assertEquals($content, file_get_contents(__FILE__));
  616. } catch (OssException $e) {
  617. $this->assertFalse(true);
  618. }
  619. try {
  620. $this->assertTrue($this->ossClient->doesObjectExist($this->bucket, $object));
  621. $this->ossClient->deleteObject($this->bucket, $object);
  622. $this->assertFalse($this->ossClient->doesObjectExist($this->bucket, $object));
  623. } catch (OssException $e) {
  624. $this->assertFalse(true);
  625. }
  626. }
  627. public function testBaseInterfaceForBucekt()
  628. {
  629. $this->ossClient->createBucket($this->bucket, OssClient::OSS_ACL_TYPE_PUBLIC_READ_WRITE);
  630. $bucketListInfo = $this->ossClient->listBuckets();
  631. $this->assertNotNull($bucketListInfo);
  632. $bucketList = $bucketListInfo->getBucketList();
  633. $this->assertTrue(is_array($bucketList));
  634. $this->assertGreaterThan(0, count($bucketList));
  635. $this->ossClient->putBucketAcl($this->bucket, OssClient::OSS_ACL_TYPE_PUBLIC_READ_WRITE);
  636. Common::waitMetaSync();
  637. $this->assertEquals($this->ossClient->getBucketAcl($this->bucket), OssClient::OSS_ACL_TYPE_PUBLIC_READ_WRITE);
  638. $this->assertTrue($this->ossClient->doesBucketExist($this->bucket));
  639. $this->assertFalse($this->ossClient->doesBucketExist($this->bucket . '-notexist'));
  640. $this->assertNotNull($this->ossClient->getBucketLocation($this->bucket));
  641. $res = $this->ossClient->getBucketMeta($this->bucket);
  642. $this->assertEquals('200', $res['info']['http_code']);
  643. $this->assertNotNull($res['x-oss-bucket-region']);
  644. }
  645. public function testBaseInterfaceForObjectWithSts()
  646. {
  647. $object = "oss-php-sdk-test/upload-test-object-name.txt";
  648. try {
  649. $this->stsOssClient->putObject($this->bucket, $object, file_get_contents(__FILE__));
  650. } catch (OssException $e) {
  651. $this->assertTrue(false);
  652. }
  653. // test GetObjectMeta
  654. try {
  655. $res = $this->stsOssClient->getObjectMeta($this->bucket, $object);
  656. $this->assertEquals('200', $res['info']['http_code']);
  657. $this->assertEquals('text/plain', $res['content-type']);
  658. $this->assertEquals('Accept-Encoding', $res['vary']);
  659. $this->assertTrue(isset($res['content-encoding']));
  660. } catch (OssException $e) {
  661. $this->assertTrue(false);
  662. }
  663. $options = array(OssClient::OSS_HEADERS => array(OssClient::OSS_ACCEPT_ENCODING => 'deflate, gzip'));
  664. try {
  665. $res = $this->stsOssClient->getObjectMeta($this->bucket, $object, $options);
  666. $this->assertEquals('200', $res['info']['http_code']);
  667. $this->assertEquals('text/plain', $res['content-type']);
  668. $this->assertEquals('Accept-Encoding', $res['vary']);
  669. $this->assertEquals('gzip', $res['content-encoding']);
  670. } catch (OssException $e) {
  671. $this->assertTrue(false);
  672. }
  673. $options = array(OssClient::OSS_HEADERS => array(OssClient::OSS_ACCEPT_ENCODING => 'deflate, gzip'));
  674. try {
  675. $res = $this->stsOssClient->getObject($this->bucket, $object, $options);
  676. $this->assertEquals(file_get_contents(__FILE__), $res);
  677. } catch (OssException $e) {
  678. $this->assertTrue(false);
  679. }
  680. try {
  681. $res = $this->stsOssClient->getObject($this->bucket, $object, array(OssClient::OSS_LAST_MODIFIED => "xx"));
  682. $this->assertEquals(file_get_contents(__FILE__), $res);
  683. } catch (OssException $e) {
  684. $this->assertEquals('"/ilegal.txt" object name is invalid', $e->getMessage());
  685. }
  686. try {
  687. $res = $this->stsOssClient->getObject($this->bucket, $object, array(OssClient::OSS_ETAG => "xx"));
  688. $this->assertEquals(file_get_contents(__FILE__), $res);
  689. } catch (OssException $e) {
  690. $this->assertEquals('"/ilegal.txt" object name is invalid', $e->getMessage());
  691. }
  692. $content = file_get_contents(__FILE__);
  693. $options = array(
  694. OssClient::OSS_LENGTH => strlen($content),
  695. OssClient::OSS_HEADERS => array(
  696. 'Expires' => 'Fri, 28 Feb 2020 05:38:42 GMT',
  697. 'Cache-Control' => 'no-cache',
  698. 'Content-Disposition' => 'attachment;filename=oss_download.log',
  699. 'Content-Language' => 'zh-CN',
  700. 'x-oss-server-side-encryption' => 'AES256',
  701. 'x-oss-meta-self-define-title' => 'user define meta info',
  702. ),
  703. );
  704. try {
  705. $this->stsOssClient->putObject($this->bucket, $object, $content, $options);
  706. } catch (OssException $e) {
  707. $this->assertFalse(true);
  708. }
  709. try {
  710. $this->stsOssClient->putObject($this->bucket, $object, $content, $options);
  711. } catch (OssException $e) {
  712. $this->assertFalse(true);
  713. }
  714. try {
  715. $result = $this->stsOssClient->deleteObjects($this->bucket, "stringtype", $options);
  716. $this->assertEquals('stringtype', $result[0]);
  717. } catch (OssException $e) {
  718. $this->assertEquals('objects must be array', $e->getMessage());
  719. }
  720. try {
  721. $result = $this->stsOssClient->deleteObjects($this->bucket, "stringtype", $options);
  722. $this->assertFalse(true);
  723. } catch (OssException $e) {
  724. $this->assertEquals('objects must be array', $e->getMessage());
  725. }
  726. try {
  727. $this->stsOssClient->uploadFile($this->bucket, $object, "notexist.txt", $options);
  728. $this->assertFalse(true);
  729. } catch (OssException $e) {
  730. $this->assertEquals('notexist.txt file does not exist', $e->getMessage());
  731. }
  732. $content = file_get_contents(__FILE__);
  733. $options = array(
  734. OssClient::OSS_LENGTH => strlen($content),
  735. OssClient::OSS_HEADERS => array(
  736. 'Expires' => 'Fri, 28 Feb 2020 05:38:42 GMT',
  737. 'Cache-Control' => 'no-cache',
  738. 'Content-Disposition' => 'attachment;filename=oss_download.log',
  739. 'Content-Language' => 'zh-CN',
  740. 'x-oss-server-side-encryption' => 'AES256',
  741. 'x-oss-meta-self-define-title' => 'user define meta info',
  742. ),
  743. );
  744. try {
  745. $this->stsOssClient->putObject($this->bucket, $object, $content, $options);
  746. } catch (OssException $e) {
  747. $this->assertFalse(true);
  748. }
  749. try {
  750. $this->stsOssClient->putObject($this->bucket, $object, $content, $options);
  751. } catch (OssException $e) {
  752. $this->assertFalse(true);
  753. }
  754. /**
  755. * GetObject to the local variable and check for match
  756. */
  757. try {
  758. $content = $this->stsOssClient->getObject($this->bucket, $object);
  759. $this->assertEquals($content, file_get_contents(__FILE__));
  760. } catch (OssException $e) {
  761. $this->assertFalse(true);
  762. }
  763. /**
  764. * GetObject first five bytes
  765. */
  766. try {
  767. $options = array(OssClient::OSS_RANGE => '0-4');
  768. $content = $this->stsOssClient->getObject($this->bucket, $object, $options);
  769. $this->assertEquals($content, '<?php');
  770. } catch (OssException $e) {
  771. $this->assertFalse(true);
  772. }
  773. /**
  774. * Upload the local file to object
  775. */
  776. try {
  777. $this->stsOssClient->uploadFile($this->bucket, $object, __FILE__);
  778. } catch (OssException $e) {
  779. $this->assertFalse(true);
  780. }
  781. /**
  782. * Download the file to the local variable and check for match.
  783. */
  784. try {
  785. $content = $this->stsOssClient->getObject($this->bucket, $object);
  786. $this->assertEquals($content, file_get_contents(__FILE__));
  787. } catch (OssException $e) {
  788. $this->assertFalse(true);
  789. }
  790. /**
  791. * Download the file to the local file
  792. */
  793. $localfile = "upload-test-object-name.txt";
  794. $options = array(
  795. OssClient::OSS_FILE_DOWNLOAD => $localfile,
  796. );
  797. try {
  798. $this->stsOssClient->getObject($this->bucket, $object, $options);
  799. } catch (OssException $e) {
  800. $this->assertFalse(true);
  801. }
  802. $this->assertTrue(file_get_contents($localfile) === file_get_contents(__FILE__));
  803. if (file_exists($localfile)) {
  804. unlink($localfile);
  805. }
  806. /**
  807. * Download the file to the local file. no such key
  808. */
  809. $localfile = "upload-test-object-name-no-such-key.txt";
  810. $options = array(
  811. OssClient::OSS_FILE_DOWNLOAD => $localfile,
  812. );
  813. try {
  814. $this->stsOssClient->getObject($this->bucket, $object . "no-such-key", $options);
  815. $this->assertTrue(false);
  816. } catch (OssException $e) {
  817. $this->assertTrue(true);
  818. $this->assertFalse(file_exists($localfile));
  819. if (strpos($e, "The specified key does not exist") == false) {
  820. $this->assertTrue(true);
  821. }
  822. }
  823. /**
  824. * Download the file to the content. no such key
  825. */
  826. try {
  827. $result = $this->stsOssClient->getObject($this->bucket, $object . "no-such-key");
  828. $this->assertTrue(false);
  829. } catch (OssException $e) {
  830. $this->assertTrue(true);
  831. if (strpos($e, "The specified key does not exist") == false) {
  832. $this->assertTrue(true);
  833. }
  834. }
  835. /**
  836. * Copy object
  837. */
  838. $to_bucket = $this->bucket;
  839. $to_object = $object . '.copy';
  840. $options = array();
  841. try {
  842. $result = $this->stsOssClient->copyObject($this->bucket, $object, $to_bucket, $to_object, $options);
  843. $this->assertFalse(empty($result));
  844. $this->assertEquals(strlen("2016-11-21T03:46:58.000Z"), strlen($result[0]));
  845. $this->assertEquals(strlen("\"5B3C1A2E053D763E1B002CC607C5A0FE\""), strlen($result[1]));
  846. } catch (OssException $e) {
  847. $this->assertFalse(true);
  848. var_dump($e->getMessage());
  849. }
  850. /**
  851. * Check if the replication is the same
  852. */
  853. try {
  854. $content = $this->stsOssClient->getObject($this->bucket, $to_object);
  855. $this->assertEquals($content, file_get_contents(__FILE__));
  856. } catch (OssException $e) {
  857. $this->assertFalse(true);
  858. }
  859. /**
  860. * List the files in your bucket.
  861. */
  862. $prefix = '';
  863. $delimiter = '/';
  864. $next_marker = '';
  865. $maxkeys = 1000;
  866. $options = array(
  867. 'delimiter' => $delimiter,
  868. 'prefix' => $prefix,
  869. 'max-keys' => $maxkeys,
  870. 'marker' => $next_marker,
  871. );
  872. try {
  873. $listObjectInfo = $this->stsOssClient->listObjects($this->bucket, $options);
  874. $objectList = $listObjectInfo->getObjectList();
  875. $prefixList = $listObjectInfo->getPrefixList();
  876. $this->assertNotNull($objectList);
  877. $this->assertNotNull($prefixList);
  878. $this->assertTrue(is_array($objectList));
  879. $this->assertTrue(is_array($prefixList));
  880. } catch (OssException $e) {
  881. $this->assertTrue(false);
  882. }
  883. /**
  884. * Set the meta information for the file
  885. */
  886. $from_bucket = $this->bucket;
  887. $from_object = "oss-php-sdk-test/upload-test-object-name.txt";
  888. $to_bucket = $from_bucket;
  889. $to_object = $from_object;
  890. $copy_options = array(
  891. OssClient::OSS_HEADERS => array(
  892. 'Expires' => '2012-10-01 08:00:00',
  893. 'Content-Disposition' => 'attachment; filename="xxxxxx"',
  894. ),
  895. );
  896. try {
  897. $this->stsOssClient->copyObject($from_bucket, $from_object, $to_bucket, $to_object, $copy_options);
  898. } catch (OssException $e) {
  899. $this->assertFalse(true);
  900. }
  901. /**
  902. * Get the meta information for the file
  903. */
  904. $object = "oss-php-sdk-test/upload-test-object-name.txt";
  905. try {
  906. $objectMeta = $this->stsOssClient->getObjectMeta($this->bucket, $object);
  907. $this->assertEquals('attachment; filename="xxxxxx"', $objectMeta[strtolower('Content-Disposition')]);
  908. } catch (OssException $e) {
  909. $this->assertFalse(true);
  910. }
  911. /**
  912. * Delete single file
  913. */
  914. $object = "oss-php-sdk-test/upload-test-object-name.txt";
  915. try {
  916. $this->assertTrue($this->stsOssClient->doesObjectExist($this->bucket, $object));
  917. $this->stsOssClient->deleteObject($this->bucket, $object);
  918. $this->assertFalse($this->stsOssClient->doesObjectExist($this->bucket, $object));
  919. } catch (OssException $e) {
  920. $this->assertFalse(true);
  921. }
  922. /**
  923. * Delete multiple files
  924. */
  925. $object1 = "oss-php-sdk-test/upload-test-object-name.txt";
  926. $object2 = "oss-php-sdk-test/upload-test-object-name.txt.copy";
  927. $list = array($object1, $object2);
  928. try {
  929. $this->assertTrue($this->stsOssClient->doesObjectExist($this->bucket, $object2));
  930. $result = $this->stsOssClient->deleteObjects($this->bucket, $list);
  931. $this->assertEquals($list[0], $result[0]);
  932. $this->assertEquals($list[1], $result[1]);
  933. $result = $this->stsOssClient->deleteObjects($this->bucket, $list, array('quiet' => 'true'));
  934. $this->assertEquals(array(), $result);
  935. $this->assertFalse($this->stsOssClient->doesObjectExist($this->bucket, $object2));
  936. $this->stsOssClient->putObject($this->bucket, $object, $content);
  937. $this->assertTrue($this->stsOssClient->doesObjectExist($this->bucket, $object));
  938. $result = $this->stsOssClient->deleteObjects($this->bucket, $list, array('quiet' => true));
  939. $this->assertEquals(array(), $result);
  940. $this->assertFalse($this->stsOssClient->doesObjectExist($this->bucket, $object));
  941. } catch (OssException $e) {
  942. $this->assertFalse(true);
  943. }
  944. $content_array = array('Hello OSS', 'Hi OSS', 'OSS OK');
  945. /**
  946. * Append the upload string
  947. */
  948. try {
  949. $position = $this->stsOssClient->appendObject($this->bucket, $object, $content_array[0], 0);
  950. $this->assertEquals($position, strlen($content_array[0]));
  951. $position = $this->stsOssClient->appendObject($this->bucket, $object, $content_array[1], $position);
  952. $this->assertEquals($position, strlen($content_array[0]) + strlen($content_array[1]));
  953. $position = $this->stsOssClient->appendObject($this->bucket, $object, $content_array[2], $position, array(OssClient::OSS_LENGTH => strlen($content_array[2])));
  954. $this->assertEquals($position, strlen($content_array[0]) + strlen($content_array[1]) + strlen($content_array[2]));
  955. } catch (OssException $e) {
  956. print_r($e->getMessage());
  957. $this->assertFalse(true);
  958. }
  959. /**
  960. * Check if the content is the same
  961. */
  962. try {
  963. $content = $this->stsOssClient->getObject($this->bucket, $object);
  964. $this->assertEquals($content, implode($content_array));
  965. } catch (OssException $e) {
  966. $this->assertFalse(true);
  967. }
  968. /**
  969. * Delete test object
  970. */
  971. try {
  972. $this->stsOssClient->deleteObject($this->bucket, $object);
  973. } catch (OssException $e) {
  974. $this->assertFalse(true);
  975. }
  976. /**
  977. * Append the upload of invalid local files
  978. */
  979. try {
  980. $position = $this->stsOssClient->appendFile($this->bucket, $object, "invalid-file-path", 0);
  981. $this->assertTrue(false);
  982. } catch (OssException $e) {
  983. $this->assertTrue(true);
  984. }
  985. /**
  986. * Append the upload of local files
  987. */
  988. try {
  989. $position = $this->stsOssClient->appendFile($this->bucket, $object, __FILE__, 0);
  990. $this->assertEquals($position, sprintf('%u', filesize(__FILE__)));
  991. $position = $this->stsOssClient->appendFile($this->bucket, $object, __FILE__, $position);
  992. $this->assertEquals($position, sprintf('%u', filesize(__FILE__)) * 2);
  993. } catch (OssException $e) {
  994. $this->assertFalse(true);
  995. }
  996. /**
  997. * Check if the replication is the same
  998. */
  999. try {
  1000. $content = $this->stsOssClient->getObject($this->bucket, $object);
  1001. $this->assertEquals($content, file_get_contents(__FILE__) . file_get_contents(__FILE__));
  1002. } catch (OssException $e) {
  1003. $this->assertFalse(true);
  1004. }
  1005. /**
  1006. * Delete test object
  1007. */
  1008. try {
  1009. $this->stsOssClient->deleteObject($this->bucket, $object);
  1010. } catch (OssException $e) {
  1011. $this->assertFalse(true);
  1012. }
  1013. $options = array(
  1014. OssClient::OSS_HEADERS => array(
  1015. 'Expires' => '2012-10-01 08:00:00',
  1016. 'Content-Disposition' => 'attachment; filename="xxxxxx"',
  1017. ),
  1018. );
  1019. /**
  1020. * Append upload with option
  1021. */
  1022. try {
  1023. $position = $this->stsOssClient->appendObject($this->bucket, $object, "Hello OSS, ", 0, $options);
  1024. $position = $this->stsOssClient->appendObject($this->bucket, $object, "Hi OSS.", $position);
  1025. } catch (OssException $e) {
  1026. $this->assertFalse(true);
  1027. }
  1028. /**
  1029. * Get the meta information for the file
  1030. */
  1031. try {
  1032. $objectMeta = $this->stsOssClient->getObjectMeta($this->bucket, $object);
  1033. $this->assertEquals('attachment; filename="xxxxxx"', $objectMeta[strtolower('Content-Disposition')]);
  1034. } catch (OssException $e) {
  1035. $this->assertFalse(true);
  1036. }
  1037. /**
  1038. * Delete test object
  1039. */
  1040. try {
  1041. $this->stsOssClient->deleteObject($this->bucket, $object);
  1042. } catch (OssException $e) {
  1043. $this->assertFalse(true);
  1044. }
  1045. $options = array(OssClient::OSS_CHECK_MD5 => true);
  1046. $content = file_get_contents(__FILE__);
  1047. /**
  1048. * Upload data to start MD5
  1049. */
  1050. try {
  1051. $this->stsOssClient->putObject($this->bucket, $object, $content, $options);
  1052. } catch (OssException $e) {
  1053. $this->assertFalse(true);
  1054. }
  1055. /**
  1056. * Check if the replication is the same
  1057. */
  1058. try {
  1059. $content = $this->stsOssClient->getObject($this->bucket, $object);
  1060. $this->assertEquals($content, file_get_contents(__FILE__));
  1061. } catch (OssException $e) {
  1062. $this->assertFalse(true);
  1063. }
  1064. /**
  1065. * Upload file to start MD5
  1066. */
  1067. try {
  1068. $this->stsOssClient->uploadFile($this->bucket, $object, __FILE__, $options);
  1069. } catch (OssException $e) {
  1070. $this->assertFalse(true);
  1071. }
  1072. /**
  1073. * Check if the replication is the same
  1074. */
  1075. try {
  1076. $content = $this->stsOssClient->getObject($this->bucket, $object);
  1077. $this->assertEquals($content, file_get_contents(__FILE__));
  1078. } catch (OssException $e) {
  1079. $this->assertFalse(true);
  1080. }
  1081. /**
  1082. * Delete test object
  1083. */
  1084. try {
  1085. $this->stsOssClient->deleteObject($this->bucket, $object);
  1086. } catch (OssException $e) {
  1087. $this->assertFalse(true);
  1088. }
  1089. $object = "oss-php-sdk-test/append-test-object-name.txt";
  1090. $content_array = array('Hello OSS', 'Hi OSS', 'OSS OK');
  1091. $options = array(OssClient::OSS_CHECK_MD5 => true);
  1092. /**
  1093. * Append the upload string
  1094. */
  1095. try {
  1096. $position = $this->stsOssClient->appendObject($this->bucket, $object, $content_array[0], 0, $options);
  1097. $this->assertEquals($position, strlen($content_array[0]));
  1098. $position = $this->stsOssClient->appendObject($this->bucket, $object, $content_array[1], $position, $options);
  1099. $this->assertEquals($position, strlen($content_array[0]) + strlen($content_array[1]));
  1100. $position = $this->stsOssClient->appendObject($this->bucket, $object, $content_array[2], $position, $options);
  1101. $this->assertEquals($position, strlen($content_array[0]) + strlen($content_array[1]) + strlen($content_array[1]));
  1102. } catch (OssException $e) {
  1103. $this->assertFalse(true);
  1104. }
  1105. /**
  1106. * Check if the content is the same
  1107. */
  1108. try {
  1109. $content = $this->stsOssClient->getObject($this->bucket, $object);
  1110. $this->assertEquals($content, implode($content_array));
  1111. } catch (OssException $e) {
  1112. $this->assertFalse(true);
  1113. }
  1114. /**
  1115. * Delete test object
  1116. */
  1117. try {
  1118. $this->stsOssClient->deleteObject($this->bucket, $object);
  1119. } catch (OssException $e) {
  1120. $this->assertFalse(true);
  1121. }
  1122. /**
  1123. * Append upload of local files
  1124. */
  1125. try {
  1126. $position = $this->stsOssClient->appendFile($this->bucket, $object, __FILE__, 0, $options);
  1127. $this->assertEquals($position, sprintf('%u', filesize(__FILE__)));
  1128. $position = $this->stsOssClient->appendFile($this->bucket, $object, __FILE__, $position, $options);
  1129. $this->assertEquals($position, sprintf('%u', filesize(__FILE__)) * 2);
  1130. } catch (OssException $e) {
  1131. $this->assertFalse(true);
  1132. }
  1133. /**
  1134. * Check if the replication is the same
  1135. */
  1136. try {
  1137. $content = $this->stsOssClient->getObject($this->bucket, $object);
  1138. $this->assertEquals($content, file_get_contents(__FILE__) . file_get_contents(__FILE__));
  1139. } catch (OssException $e) {
  1140. $this->assertFalse(true);
  1141. }
  1142. /**
  1143. * delete test object
  1144. */
  1145. try {
  1146. $this->stsOssClient->deleteObject($this->bucket, $object);
  1147. } catch (OssException $e) {
  1148. $this->assertFalse(true);
  1149. }
  1150. $options = array(
  1151. OssClient::OSS_HEADERS => array(
  1152. "Content-Type" => "application/octet-stream",
  1153. "name" => "aliyun",
  1154. "email" => "aliyun@aliyun.com",
  1155. ),
  1156. OssClient::OSS_ADDITIONAL_HEADERS => array('name', 'email')
  1157. );
  1158. try {
  1159. $this->stsOssClient->uploadFile($this->bucket, $object, __FILE__, $options);
  1160. } catch (OssException $e) {
  1161. $this->assertFalse(true);
  1162. }
  1163. try {
  1164. $content = $this->stsOssClient->getObject($this->bucket, $object, $options);
  1165. } catch (OssException $e) {
  1166. $this->assertFalse(true);
  1167. }
  1168. /**
  1169. * delete test object
  1170. */
  1171. try {
  1172. $this->stsOssClient->deleteObject($this->bucket, $object, $options);
  1173. } catch (OssException $e) {
  1174. $this->assertFalse(true);
  1175. }
  1176. }
  1177. public function testBaseInterfaceForBucektWithSts()
  1178. {
  1179. $options = array(
  1180. OssClient::OSS_HEADERS => array(
  1181. "Content-Type" => "application/octet-stream",
  1182. "name" => "aliyun",
  1183. "email" => "aliyun@aliyun.com",
  1184. ),
  1185. OssClient::OSS_ADDITIONAL_HEADERS => array('name', 'email')
  1186. );
  1187. $this->stsOssClient->createBucket($this->bucket, OssClient::OSS_ACL_TYPE_PUBLIC_READ_WRITE, $options);
  1188. $bucketListInfo = $this->stsOssClient->listBuckets($options);
  1189. $this->assertNotNull($bucketListInfo);
  1190. $bucketList = $bucketListInfo->getBucketList();
  1191. $this->assertTrue(is_array($bucketList));
  1192. $this->assertGreaterThan(0, count($bucketList));
  1193. $this->stsOssClient->putBucketAcl($this->bucket, OssClient::OSS_ACL_TYPE_PUBLIC_READ_WRITE, $options);
  1194. Common::waitMetaSync();
  1195. $this->assertEquals($this->stsOssClient->getBucketAcl($this->bucket), OssClient::OSS_ACL_TYPE_PUBLIC_READ_WRITE);
  1196. $this->assertTrue($this->stsOssClient->doesBucketExist($this->bucket));
  1197. $this->assertFalse($this->stsOssClient->doesBucketExist($this->bucket . '-notexist'));
  1198. $this->assertNotNull($this->stsOssClient->getBucketLocation($this->bucket));
  1199. $res = $this->stsOssClient->getBucketMeta($this->bucket, $options);
  1200. $this->assertEquals('200', $res['info']['http_code']);
  1201. $this->assertNotNull($res['x-oss-bucket-region']);
  1202. }
  1203. protected function setUp(): void
  1204. {
  1205. $config = array(
  1206. 'signatureVersion' => OssClient::OSS_SIGNATURE_VERSION_V4
  1207. );
  1208. $this->bucket = Common::getBucketName() . '-' . time();
  1209. $this->ossClient = Common::getOssClient($config);
  1210. $this->ossClient->createBucket($this->bucket);
  1211. Common::waitMetaSync();
  1212. $this->stsOssClient = Common::getStsOssClient($config);
  1213. Common::waitMetaSync();
  1214. }
  1215. protected function tearDown(): void
  1216. {
  1217. if (!$this->ossClient->doesBucketExist($this->bucket)) {
  1218. return;
  1219. }
  1220. $objects = $this->ossClient->listObjects(
  1221. $this->bucket, array('max-keys' => 1000, 'delimiter' => ''))->getObjectList();
  1222. $keys = array();
  1223. foreach ($objects as $obj) {
  1224. $keys[] = $obj->getKey();
  1225. }
  1226. if (count($keys) > 0) {
  1227. $this->ossClient->deleteObjects($this->bucket, $keys);
  1228. }
  1229. $uploads = $this->ossClient->listMultipartUploads($this->bucket)->getUploads();
  1230. foreach ($uploads as $up) {
  1231. $this->ossClient->abortMultipartUpload($this->bucket, $up->getKey(), $up->getUploadId());
  1232. }
  1233. $this->ossClient->deleteBucket($this->bucket);
  1234. }
  1235. }