Test.php 54 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682
  1. <?php
  2. namespace Qcloud\Cos\Tests;
  3. use Qcloud\Cos\Client;
  4. use Qcloud\Cos\Exception\ServiceResponseException;
  5. class COSTest extends \PHPUnit_Framework_TestCase
  6. {
  7. const SYNC_TIME = 5;
  8. private $cosClient;
  9. private $bucket;
  10. private $region;
  11. protected function setUp()
  12. {
  13. $this->bucket = getenv('COS_BUCKET');
  14. $this->region = getenv('COS_REGION');
  15. $this->bucket2 = "tmp".$this->bucket;
  16. $this->cosClient = new Client(array('region' => $this->region,
  17. 'credentials' => array(
  18. 'secretId' => getenv('COS_KEY'),
  19. 'secretKey' => getenv('COS_SECRET'))));
  20. try {
  21. $this->cosClient->createBucket(['Bucket' => $this->bucket]);
  22. } catch(\Exception $e) {
  23. }
  24. }
  25. protected function tearDown() {
  26. }
  27. function generateRandomString($length = 10) {
  28. $characters = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
  29. $randomString = '';
  30. for ($i = 0; $i < $length; $i++) {
  31. $randomString .= $characters[rand(0, strlen($characters) - 1)];
  32. }
  33. return $randomString;
  34. }
  35. function generateRandomFile($size = 10, $filename = 'random-file') {
  36. exec("dd if=/dev/urandom of=". $filename. " bs=1 count=". (string)$size);
  37. }
  38. /**********************************
  39. * TestBucket
  40. **********************************/
  41. /*
  42. * put bucket,bucket已经存在
  43. * BucketAlreadyOwnedByYou
  44. * 409
  45. */
  46. public function testCreateExistingBucket()
  47. {
  48. try {
  49. $this->cosClient->createBucket(['Bucket' => $this->bucket]);
  50. } catch (ServiceResponseException $e) {
  51. $this->assertTrue($e->getExceptionCode() === 'BucketAlreadyOwnedByYou' && $e->getStatusCode() === 409);
  52. }
  53. }
  54. /*
  55. * put bucket, 创建所有region的bucket
  56. * 409
  57. */
  58. public function testValidRegionBucket()
  59. {
  60. $regionlist = array('cn-east','ap-shanghai',
  61. 'cn-south','ap-guangzhou',
  62. 'cn-north','ap-beijing-1',
  63. 'cn-southwest','ap-chengdu',
  64. 'sg','ap-singapore',
  65. 'tj','ap-beijing-1',
  66. 'bj','ap-beijing',
  67. 'sh','ap-shanghai',
  68. 'gz','ap-guangzhou',
  69. 'cd','ap-chengdu',
  70. 'sgp','ap-singapore');
  71. foreach ($regionlist as$region) {
  72. try {
  73. $this->cosClient = new Client(array('region' => $region,
  74. 'credentials' => array(
  75. 'appId' => getenv('COS_APPID'),
  76. 'secretId' => getenv('COS_KEY'),
  77. 'secretKey' => getenv('COS_SECRET'))));
  78. $this->cosClient->createBucket(['Bucket' => $this->bucket]);
  79. } catch (ServiceResponseException $e) {
  80. $this->assertEquals([$e->getStatusCode()], [409]);
  81. }
  82. }
  83. }
  84. /*
  85. * put bucket, 不合法的region名
  86. * 409
  87. */
  88. public function testInvalidRegionBucket()
  89. {
  90. $regionlist = array('cn-east-2','ap-shanghai-3');
  91. foreach ($regionlist as$region) {
  92. try {
  93. $this->cosClient = new Client(array('region' => $region,
  94. 'credentials' => array(
  95. 'appId' => getenv('COS_APPID'),
  96. 'secretId' => getenv('COS_KEY'),
  97. 'secretKey' => getenv('COS_SECRET'))));
  98. $this->cosClient->createBucket(['Bucket' => $this->bucket]);
  99. } catch (ServiceResponseException $e) {
  100. $this->assertFalse(TRUE);
  101. } catch (\GuzzleHttp\Exception\ConnectException $e) {
  102. $this->assertTrue(TRUE);
  103. }
  104. }
  105. }
  106. /*
  107. * get Service
  108. * 200
  109. */
  110. public function testGetService()
  111. {
  112. try {
  113. $this->cosClient->ListBuckets();
  114. } catch (ServiceResponseException $e) {
  115. print $e;
  116. $this->assertFalse(TRUE);
  117. }
  118. }
  119. /*
  120. * put bucket,bucket名称非法
  121. * InvalidBucketName
  122. * 400
  123. */
  124. public function testCreateInvalidBucket()
  125. {
  126. try {
  127. $this->cosClient->createBucket(array('Bucket' => 'qwe_123' . $this->bucket));
  128. } catch (ServiceResponseException $e) {
  129. print $e;
  130. $this->assertTrue($e->getExceptionCode() === 'InvalidBucketName' && $e->getStatusCode() === 400);
  131. }
  132. }
  133. /*
  134. * put bucket,设置bucket公公权限为private
  135. * 200
  136. */
  137. public function testCreatePrivateBucket()
  138. {
  139. try {
  140. $this->cosClient->createBucket(
  141. array(
  142. 'Bucket' => $this->bucket2,
  143. 'ACL'=>'private'
  144. ));
  145. sleep(COSTest::SYNC_TIME);
  146. TestHelper::nuke($this->bucket2);
  147. } catch (ServiceResponseException $e) {
  148. print $e;
  149. $this->assertFalse(TRUE);
  150. }
  151. }
  152. /*
  153. * put bucket,设置bucket公公权限为public-read
  154. * 200
  155. */
  156. public function testCreatePublicReadBucket()
  157. {
  158. try {
  159. TestHelper::nuke($this->bucket2);
  160. sleep(COSTest::SYNC_TIME);
  161. $this->cosClient->createBucket(
  162. array(
  163. 'Bucket' => $this->bucket2,
  164. 'ACL'=>'public-read'
  165. )
  166. );
  167. sleep(COSTest::SYNC_TIME);
  168. TestHelper::nuke($this->bucket2);
  169. } catch (ServiceResponseException $e) {
  170. print $e;
  171. $this->assertFalse(TRUE);
  172. }
  173. }
  174. /*
  175. * put bucket,公共权限非法
  176. * InvalidArgument
  177. * 400
  178. */
  179. public function testCreateInvalidACLBucket()
  180. {
  181. try {
  182. TestHelper::nuke($this->bucket2);
  183. sleep(COSTest::SYNC_TIME);
  184. $this->cosClient->createBucket(
  185. array(
  186. 'Bucket' => $this->bucket2,
  187. 'ACL'=>'public'
  188. )
  189. );
  190. sleep(COSTest::SYNC_TIME);
  191. TestHelper::nuke($this->bucket2);
  192. } catch (ServiceResponseException $e) {
  193. $this->assertTrue($e->getExceptionCode() === 'InvalidArgument' && $e->getStatusCode() === 400);
  194. }
  195. }
  196. /*
  197. * put bucket acl,设置bucket公共权限为private
  198. * 200
  199. */
  200. public function testPutBucketAclPrivate()
  201. {
  202. try {
  203. $this->cosClient->PutBucketAcl(
  204. array(
  205. 'Bucket' => $this->bucket,
  206. 'ACL'=>'private'
  207. )
  208. );
  209. } catch (ServiceResponseException $e) {
  210. print $e;
  211. $this->assertFalse(TRUE);
  212. }
  213. }
  214. /*
  215. * put bucket acl,设置bucket公共权限为public-read
  216. * 200
  217. */
  218. public function testPutBucketAclPublicRead()
  219. {
  220. try {
  221. $this->cosClient->PutBucketAcl(
  222. array(
  223. 'Bucket' => $this->bucket,
  224. 'ACL'=>'public-read'
  225. )
  226. );
  227. } catch (ServiceResponseException $e) {
  228. print $e;
  229. $this->assertFalse(TRUE);
  230. }
  231. }
  232. /*
  233. * put bucket acl,公共权限非法
  234. * InvalidArgument
  235. * 400
  236. */
  237. public function testPutBucketAclInvalid()
  238. {
  239. try {
  240. $this->cosClient->PutBucketAcl(
  241. array(
  242. 'Bucket' => $this->bucket,
  243. 'ACL'=>'public'
  244. )
  245. );
  246. } catch (ServiceResponseException $e) {
  247. $this->assertTrue($e->getExceptionCode() === 'InvalidArgument' && $e->getStatusCode() === 400);
  248. }
  249. }
  250. /*
  251. * put bucket acl,设置bucket账号权限为grant-read
  252. * 200
  253. */
  254. public function testPutBucketAclReadToUser()
  255. {
  256. try {
  257. $this->cosClient->PutBucketAcl(
  258. array(
  259. 'Bucket' => $this->bucket,
  260. 'GrantRead' => 'id="qcs::cam::uin/2779643970:uin/2779643970"'
  261. )
  262. );
  263. } catch (ServiceResponseException $e) {
  264. print $e;
  265. $this->assertFalse(TRUE);
  266. }
  267. }
  268. /*
  269. * put bucket acl,设置bucket账号权限为grant-write
  270. * 200
  271. */
  272. public function testPutBucketAclWriteToUser()
  273. {
  274. try {
  275. $this->cosClient->PutBucketAcl(
  276. array(
  277. 'Bucket' => $this->bucket,
  278. 'GrantWrite' => 'id="qcs::cam::uin/2779643970:uin/2779643970"'
  279. )
  280. );
  281. } catch (ServiceResponseException $e) {
  282. print $e;
  283. $this->assertFalse(TRUE);
  284. }
  285. }
  286. /*
  287. * put bucket acl,设置bucket账号权限为grant-full-control
  288. * 200
  289. */
  290. public function testPutBucketAclFullToUser()
  291. {
  292. try {
  293. $this->cosClient->PutBucketAcl(
  294. array(
  295. 'Bucket' => $this->bucket,
  296. 'GrantFullControl' => 'id="qcs::cam::uin/2779643970:uin/2779643970"'
  297. )
  298. );
  299. } catch (ServiceResponseException $e) {
  300. print $e;
  301. $this->assertFalse(TRUE);
  302. }
  303. }
  304. /*
  305. * put bucket acl,设置bucket账号权限,同时授权给多个账户
  306. * 200
  307. */
  308. public function testPutBucketAclToUsers()
  309. {
  310. try {
  311. $this->cosClient->PutBucketAcl(
  312. array(
  313. 'Bucket' => $this->bucket,
  314. 'GrantFullControl' => 'id="qcs::cam::uin/2779643970:uin/2779643970",id="qcs::cam::uin/2779643970:uin/2779643970",id="qcs::cam::uin/2779643970:uin/2779643970"'
  315. )
  316. );
  317. } catch (ServiceResponseException $e) {
  318. print $e;
  319. $this->assertFalse(TRUE);
  320. }
  321. }
  322. /*
  323. * put bucket acl,设置bucket账号权限,授权给子账号
  324. * 200
  325. */
  326. public function testPutBucketAclToSubuser()
  327. {
  328. try {
  329. $this->cosClient->PutBucketAcl(
  330. array(
  331. 'Bucket' => $this->bucket,
  332. 'GrantFullControl' => 'id="qcs::cam::uin/2779643970:uin/2779643970"'
  333. )
  334. );
  335. } catch (ServiceResponseException $e) {
  336. print $e;
  337. $this->assertFalse(TRUE);
  338. }
  339. }
  340. /*
  341. * put bucket acl,设置bucket账号权限,同时指定read、write和fullcontrol
  342. * 200
  343. */
  344. public function testPutBucketAclReadWriteFull()
  345. {
  346. try {
  347. $this->cosClient->PutBucketAcl(
  348. array(
  349. 'Bucket' => $this->bucket,
  350. 'GrantRead' => 'id="qcs::cam::uin/123:uin/123"',
  351. 'GrantWrite' => 'id="qcs::cam::uin/2779643970:uin/2779643970"',
  352. 'GrantFullControl' => 'id="qcs::cam::uin/2779643970:uin/2779643970"'
  353. )
  354. );
  355. } catch (ServiceResponseException $e) {
  356. print $e;
  357. $this->assertFalse(TRUE);
  358. }
  359. }
  360. /*
  361. * put bucket acl,设置bucket账号权限,grant值非法
  362. * InvalidArgument
  363. * 400
  364. */
  365. public function testPutBucketAclInvalidGrant()
  366. {
  367. try {
  368. $this->cosClient->PutBucketAcl(
  369. array(
  370. 'Bucket' => $this->bucket,
  371. 'GrantFullControl' => 'id="qcs::camuin/321023:uin/2779643970"'
  372. )
  373. );
  374. } catch (ServiceResponseException $e) {
  375. $this->assertTrue($e->getExceptionCode() === 'InvalidArgument' && $e->getStatusCode() === 400);
  376. }
  377. }
  378. /*
  379. * put bucket acl,设置bucket账号权限,通过body方式授权
  380. * 200
  381. */
  382. public function testPutBucketAclByBody()
  383. {
  384. try {
  385. $this->cosClient->PutBucketAcl(
  386. array(
  387. 'Bucket' => $this->bucket,
  388. 'Grants' => array(
  389. array(
  390. 'Grantee' => array(
  391. 'DisplayName' => 'qcs::cam::uin/2779643970:uin/2779643970',
  392. 'ID' => 'qcs::cam::uin/2779643970:uin/2779643970',
  393. 'Type' => 'CanonicalUser',
  394. ),
  395. 'Permission' => 'FULL_CONTROL',
  396. ),
  397. ),
  398. 'Owner' => array(
  399. 'DisplayName' => 'qcs::cam::uin/2779643970:uin/2779643970',
  400. 'ID' => 'qcs::cam::uin/2779643970:uin/2779643970',
  401. )
  402. )
  403. );
  404. } catch (ServiceResponseException $e) {
  405. print $e;
  406. $this->assertFalse(TRUE);
  407. }
  408. }
  409. /*
  410. * put bucket acl,设置bucket账号权限,通过body方式授权给anyone
  411. * 200
  412. */
  413. public function testPutBucketAclByBodyToAnyone()
  414. {
  415. try {
  416. $this->cosClient->PutBucketAcl(
  417. array(
  418. 'Bucket' => $this->bucket,
  419. 'Grants' => array(
  420. array(
  421. 'Grantee' => array(
  422. 'DisplayName' => 'qcs::cam::anyone:anyone',
  423. 'ID' => 'qcs::cam::anyone:anyone',
  424. 'Type' => 'CanonicalUser',
  425. ),
  426. 'Permission' => 'FULL_CONTROL',
  427. ),
  428. ),
  429. 'Owner' => array(
  430. 'DisplayName' => 'qcs::cam::uin/2779643970:uin/2779643970',
  431. 'ID' => 'qcs::cam::uin/2779643970:uin/2779643970',
  432. )
  433. )
  434. );
  435. } catch (ServiceResponseException $e) {
  436. print $e;
  437. $this->assertFalse(TRUE);
  438. }
  439. }
  440. /*
  441. * put bucket acl,bucket不存在
  442. * NoSuchBucket
  443. * 404
  444. */
  445. public function testPutBucketAclBucketNonexisted()
  446. {
  447. try {
  448. TestHelper::nuke($this->bucket2);
  449. sleep(COSTest::SYNC_TIME);
  450. $this->cosClient->PutBucketAcl(
  451. array(
  452. 'Bucket' => $this->bucket2,
  453. 'GrantFullControl' => 'id="qcs::cam::uin/321023:uin/2779643970"'
  454. )
  455. );
  456. } catch (ServiceResponseException $e) {
  457. $this->assertTrue($e->getExceptionCode() === 'NoSuchBucket' && $e->getStatusCode() === 404);
  458. }
  459. }
  460. /*
  461. * put bucket acl,覆盖设置
  462. * x200
  463. */
  464. public function testPutBucketAclCover()
  465. {
  466. try {
  467. $this->cosClient->PutBucketAcl(array(
  468. 'Bucket' => $this->bucket,
  469. 'GrantFullControl' => 'id="qcs::cam::uin/2779643970:uin/2779643970"',
  470. 'GrantRead' => 'id="qcs::cam::uin/2779643970:uin/2779643970"',
  471. 'GrantWrite' => 'id="qcs::cam::uin/2779643970:uin/2779643970"'));
  472. $this->cosClient->PutBucketAcl(array(
  473. 'Bucket' => $this->bucket,
  474. 'GrantWrite' => 'id="qcs::cam::uin/2779643970:uin/2779643970"'));
  475. } catch (ServiceResponseException $e) {
  476. print $e;
  477. $this->assertFalse(TRUE);
  478. }
  479. }
  480. /*
  481. * 正常head bucket
  482. * 200
  483. */
  484. public function testHeadBucket()
  485. {
  486. try {
  487. $this->cosClient->HeadBucket(array(
  488. 'Bucket' => $this->bucket));
  489. } catch (ServiceResponseException $e) {
  490. print $e;
  491. $this->assertFalse(TRUE);
  492. }
  493. }
  494. /*
  495. * head bucket,bucket不存在
  496. * NoSuchBucket
  497. * 404
  498. */
  499. public function testHeadBucketNonexisted()
  500. {
  501. try {
  502. TestHelper::nuke($this->bucket2);
  503. sleep(COSTest::SYNC_TIME);
  504. $this->cosClient->HeadBucket(array(
  505. 'Bucket' => $this->bucket2));
  506. } catch (ServiceResponseException $e) {
  507. $this->assertTrue($e->getExceptionCode() === 'NoSuchBucket' && $e->getStatusCode() === 404);
  508. }
  509. }
  510. /*
  511. * get bucket,bucket为空
  512. * 200
  513. */
  514. public function testGetBucketEmpty()
  515. {
  516. try {
  517. $this->cosClient->ListObjects(array(
  518. 'Bucket' => $this->bucket));
  519. } catch (ServiceResponseException $e) {
  520. print $e;
  521. $this->assertFalse(TRUE);
  522. }
  523. }
  524. /*
  525. * get bucket,bucket不存在
  526. * NoSuchBucket
  527. * 404
  528. */
  529. public function testGetBucketNonexisted()
  530. {
  531. try {
  532. TestHelper::nuke($this->bucket2);
  533. sleep(COSTest::SYNC_TIME);
  534. $this->cosClient->ListObjects(
  535. array(
  536. 'Bucket' => $this->bucket2
  537. )
  538. );
  539. } catch (ServiceResponseException $e) {
  540. $this->assertTrue($e->getExceptionCode() === 'NoSuchBucket' && $e->getStatusCode() === 404);
  541. }
  542. }
  543. /*
  544. * put bucket cors,cors规则包含多条
  545. * 200
  546. */
  547. public function testPutBucketCors()
  548. {
  549. try {
  550. $this->cosClient->putBucketCors(
  551. array(
  552. 'Bucket' => $this->bucket,
  553. 'CORSRules' => array(
  554. array(
  555. 'ID' => '1234',
  556. 'AllowedHeaders' => array('*',),
  557. 'AllowedMethods' => array('PUT',),
  558. 'AllowedOrigins' => array('*',),
  559. 'ExposeHeaders' => array('*',),
  560. 'MaxAgeSeconds' => 1,
  561. ),
  562. array(
  563. 'ID' => '12345',
  564. 'AllowedHeaders' => array('*',),
  565. 'AllowedMethods' => array('GET',),
  566. 'AllowedOrigins' => array('*',),
  567. 'ExposeHeaders' => array('*',),
  568. 'MaxAgeSeconds' => 1,
  569. ),
  570. ),
  571. )
  572. );
  573. } catch (ServiceResponseException $e) {
  574. print $e;
  575. $this->assertFalse(TRUE);
  576. }
  577. }
  578. /*
  579. * 正常get bucket cors
  580. * 200
  581. */
  582. public function testGetBucketCors()
  583. {
  584. try {
  585. $this->cosClient->putBucketCors(
  586. array(
  587. 'Bucket' => $this->bucket,
  588. 'CORSRules' => array(
  589. array(
  590. 'ID' => '1234',
  591. 'AllowedHeaders' => array('*',),
  592. 'AllowedMethods' => array('PUT',),
  593. 'AllowedOrigins' => array('*',),
  594. 'ExposeHeaders' => array('*',),
  595. 'MaxAgeSeconds' => 1,
  596. ),
  597. array(
  598. 'ID' => '12345',
  599. 'AllowedHeaders' => array('*',),
  600. 'AllowedMethods' => array('GET',),
  601. 'AllowedOrigins' => array('*',),
  602. 'ExposeHeaders' => array('*',),
  603. 'MaxAgeSeconds' => 1,
  604. ),
  605. ),
  606. )
  607. );
  608. $this->cosClient->getBucketCors(
  609. array(
  610. 'Bucket' => $this->bucket
  611. )
  612. );
  613. } catch (ServiceResponseException $e) {
  614. print $e;
  615. $this->assertFalse(TRUE);
  616. }
  617. }
  618. /*
  619. * bucket未设置cors规则,发送get bucket cors
  620. * NoSuchCORSConfiguration
  621. * 404
  622. */
  623. public function testGetBucketCorsNull()
  624. {
  625. try {
  626. $this->cosClient->getBucketCors(
  627. array(
  628. 'Bucket' => $this->bucket
  629. )
  630. );
  631. } catch (ServiceResponseException $e) {
  632. $this->assertTrue($e->getExceptionCode() === 'NoSuchCORSConfiguration' && $e->getStatusCode() === 404);
  633. }
  634. }
  635. /*
  636. * 正常get bucket lifecycle
  637. * 200
  638. */
  639. public function testGetBucketLifecycle()
  640. {
  641. try {
  642. $result = $this->cosClient->putBucketLifecycle(
  643. array(
  644. 'Bucket' => $this->bucket,
  645. 'Rules' => array(
  646. array(
  647. 'Status' => 'Enabled',
  648. 'Filter' => array(
  649. 'Tag' => array(
  650. 'Key' => 'datalevel',
  651. 'Value' => 'backup'
  652. )
  653. ),
  654. 'Transitions' => array(
  655. array(
  656. # 30天后转换为Standard_IA
  657. 'Days' => 30,
  658. 'StorageClass' => 'Standard_IA'),
  659. array(
  660. # 365天后转换为Archive
  661. 'Days' => 365,
  662. 'StorageClass' => 'Archive')
  663. ),
  664. 'Expiration' => array(
  665. # 3650天后过期删除
  666. 'Days' => 3650,
  667. )
  668. )
  669. )
  670. )
  671. );
  672. $result = $this->cosClient->getBucketLifecycle(array(
  673. 'Bucket' => $this->bucket,
  674. ));
  675. } catch (ServiceResponseException $e) {
  676. print $e;
  677. $this->assertFalse(TRUE);
  678. }
  679. }
  680. /*
  681. * 正常delete bucket lifecycle
  682. * 200
  683. */
  684. public function testDeleteBucketLifecycle()
  685. {
  686. try {
  687. $result = $this->cosClient->putBucketLifecycle(
  688. array(
  689. 'Bucket' => $this->bucket,
  690. 'Rules' => array(
  691. array(
  692. 'Status' => 'Enabled',
  693. 'Filter' => array(
  694. 'Tag' => array(
  695. 'Key' => 'datalevel',
  696. 'Value' => 'backup'
  697. )
  698. ),
  699. 'Transitions' => array(
  700. array(
  701. # 30天后转换为Standard_IA
  702. 'Days' => 30,
  703. 'StorageClass' => 'Standard_IA'),
  704. array(
  705. # 365天后转换为Archive
  706. 'Days' => 365,
  707. 'StorageClass' => 'Archive')
  708. ),
  709. 'Expiration' => array(
  710. # 3650天后过期删除
  711. 'Days' => 3650,
  712. )
  713. )
  714. )
  715. )
  716. );
  717. $result = $this->cosClient->deleteBucketLifecycle(array(
  718. // Bucket is required
  719. 'Bucket' => $this->bucket,
  720. ));
  721. } catch (ServiceResponseException $e) {
  722. print $e;
  723. $this->assertFalse(TRUE);
  724. }
  725. }
  726. /*
  727. * put bucket lifecycle,请求body中不指定filter
  728. * 200
  729. */
  730. public function testPutBucketLifecycleNonFilter()
  731. {
  732. try {
  733. $result = $this->cosClient->putBucketLifecycle(
  734. array(
  735. 'Bucket' => $this->bucket,
  736. 'Rules' => array(
  737. array(
  738. 'Expiration' => array(
  739. 'Days' => 1000,
  740. ),
  741. 'ID' => 'id1',
  742. 'Status' => 'Enabled',
  743. 'Transitions' => array(
  744. array(
  745. 'Days' => 100,
  746. 'StorageClass' => 'Standard_IA'),
  747. ),
  748. ),
  749. )
  750. )
  751. );
  752. } catch (ServiceResponseException $e) {
  753. $this->assertTrue($e->getExceptionCode() === 'NoSuchBucket' && $e->getStatusCode() === 404);
  754. }
  755. }
  756. /*
  757. * put bucket,bucket名称带有-
  758. * 200
  759. */
  760. public function testPutBucket2()
  761. {
  762. try {
  763. try{
  764. $this->cosClient->deleteBucket(array('Bucket' => '12345-'.$this->bucket));
  765. } catch (\Exception $e) {
  766. }
  767. $this->cosClient->createBucket(array('Bucket' => '12345-'.$this->bucket));
  768. sleep(COSTest::SYNC_TIME);
  769. $this->cosClient->deleteBucket(array('Bucket' => '12345-'.$this->bucket));
  770. } catch (ServiceResponseException $e) {
  771. print $e;
  772. $this->assertFalse(TRUE);
  773. }
  774. }
  775. /*
  776. * put bucket,bucket名称带有两个-
  777. * 200
  778. */
  779. public function testPutBucket3()
  780. {
  781. try {
  782. $this->cosClient->createBucket(array('Bucket' => '12-333-4445' . $this->bucket));
  783. $this->cosClient->deleteBucket(array('Bucket' => '12-333-4445' . $this->bucket));
  784. } catch (ServiceResponseException $e) {
  785. print $e;
  786. $this->assertFalse(TRUE);
  787. }
  788. }
  789. /*
  790. * 正常get bucket location
  791. * 200
  792. */
  793. public function testGetBucketLocation()
  794. {
  795. try {
  796. $this->cosClient->getBucketLocation(array('Bucket' => $this->bucket));
  797. } catch (ServiceResponseException $e) {
  798. print $e;
  799. $this->assertFalse(TRUE);
  800. }
  801. }
  802. /*
  803. * bucket不存在,发送get bucket location请求
  804. * NoSuchBucket
  805. * 404
  806. */
  807. public function testGetBucketLocationNonExisted()
  808. {
  809. try {
  810. TestHelper::nuke($this->bucket2);
  811. sleep(COSTest::SYNC_TIME);
  812. $this->cosClient->getBucketLocation(array('Bucket' => $this->bucket2));
  813. } catch (ServiceResponseException $e) {
  814. // echo($e->getExceptionCode());
  815. // echo($e->getStatusCode());
  816. $this->assertTrue($e->getExceptionCode() === 'NoSuchBucket' && $e->getStatusCode() === 404);
  817. }
  818. }
  819. /**********************************
  820. * TestObject
  821. **********************************/
  822. /*
  823. * put object, 从本地上传文件
  824. * 200
  825. */
  826. public function testPutObjectLocalObject() {
  827. try {
  828. $key = '你好.txt';
  829. $body = $this->generateRandomString(1024+1023);
  830. $md5 = base64_encode(md5($body, true));
  831. $local_test_key = "local_test_file";
  832. $f = fopen($local_test_key, "wb");
  833. fwrite($f, $body);
  834. fclose($f);
  835. $this->cosClient->putObject(['Bucket' => $this->bucket,
  836. 'Key' => $key,
  837. 'Body' => fopen($local_test_key, "rb")]);
  838. $rt = $this->cosClient->getObject(['Bucket'=>$this->bucket, 'Key'=>$key]);
  839. $download_md5 = base64_encode(md5($rt['Body'], true));
  840. $this->assertEquals($md5, $download_md5);
  841. } catch (ServiceResponseException $e) {
  842. print $e;
  843. $this->assertFalse(TRUE);
  844. }
  845. }
  846. /*
  847. * upload, 从本地上传
  848. * 200
  849. */
  850. public function testUploadLocalObject() {
  851. try {
  852. $key = '你好.txt';
  853. $body = $this->generateRandomString(1024+1023);
  854. $md5 = base64_encode(md5($body, true));
  855. $local_test_key = "local_test_file";
  856. $f = fopen($local_test_key, "wb");
  857. fwrite($f, $body);
  858. fclose($f);
  859. $this->cosClient->upload($bucket=$this->bucket,
  860. $key=$key,
  861. $body=fopen($local_test_key, "rb"),
  862. $options=['PartSize'=>1024 * 1024 + 1]);
  863. $rt = $this->cosClient->getObject(['Bucket'=>$this->bucket, 'Key'=>$key]);
  864. $download_md5 = base64_encode(md5($rt['Body'], true));
  865. $this->assertEquals($md5, $download_md5);
  866. } catch (ServiceResponseException $e) {
  867. print $e;
  868. $this->assertFalse(TRUE);
  869. }
  870. }
  871. /*
  872. * put object,请求头部携带服务端加密参数
  873. * 200
  874. */
  875. public function testPutObjectEncryption()
  876. {
  877. try {
  878. $this->cosClient->putObject(
  879. array(
  880. 'Bucket' => $this->bucket,
  881. 'Key' => '11//32//43',
  882. 'Body' => 'Hello World!',
  883. 'ServerSideEncryption' => 'AES256'
  884. )
  885. );
  886. } catch (ServiceResponseException $e) {
  887. print $e;
  888. $this->assertFalse(TRUE);
  889. }
  890. }
  891. /*
  892. * 上传文件Bucket不存在
  893. * NoSuchBucket
  894. * 404
  895. */
  896. public function testPutObjectIntoNonexistedBucket() {
  897. try {
  898. TestHelper::nuke($this->bucket2);
  899. sleep(COSTest::SYNC_TIME);
  900. $this->cosClient->putObject(
  901. array(
  902. 'Bucket' => $this->bucket, 'Key' => 'hello.txt', 'Body' => 'Hello World'
  903. )
  904. );
  905. } catch (ServiceResponseException $e) {
  906. $this->assertTrue($e->getExceptionCode() === 'NoSuchBucket');
  907. $this->assertTrue($e->getStatusCode() === 404);
  908. }
  909. }
  910. /*
  911. * 上传小文件
  912. * 200
  913. */
  914. public function testUploadSmallObject() {
  915. try {
  916. $this->cosClient->upload($this->bucket, '你好.txt', 'Hello World');
  917. } catch (ServiceResponseException $e) {
  918. print $e;
  919. $this->assertFalse(TRUE);
  920. }
  921. }
  922. /*
  923. * 上传空文件
  924. * 200
  925. */
  926. public function testPutObjectEmpty() {
  927. try {
  928. $this->cosClient->upload($this->bucket, '你好.txt', '');
  929. } catch (ServiceResponseException $e) {
  930. print $e;
  931. $this->assertFalse(TRUE);
  932. }
  933. }
  934. /*
  935. * 上传已存在的文件
  936. * 200
  937. */
  938. public function testPutObjectExisted() {
  939. try {
  940. $this->cosClient->upload($this->bucket, '你好.txt', '1234124');
  941. $this->cosClient->upload($this->bucket, '你好.txt', '请二位qwe');
  942. } catch (ServiceResponseException $e) {
  943. print $e;
  944. $this->assertFalse(TRUE);
  945. }
  946. }
  947. /*
  948. * put object,请求头部携带自定义头部x-cos-meta-
  949. * 200
  950. */
  951. public function testPutObjectMeta() {
  952. try {
  953. $key = '你好.txt';
  954. $meta = array(
  955. 'test' => str_repeat('a', 1 * 1024),
  956. 'test-meta' => 'qwe-23ds-ad-xcz.asd.*qweqw'
  957. );
  958. $this->cosClient->putObject(array(
  959. 'Bucket' => $this->bucket,
  960. 'Key' => '你好.txt',
  961. 'Body' => '1234124',
  962. 'Metadata' => $meta
  963. ));
  964. $rt = $this->cosClient->headObject(['Bucket'=>$this->bucket, 'Key'=>$key]);
  965. $this->assertEquals($rt['Metadata'], $meta);
  966. } catch (ServiceResponseException $e) {
  967. print $e;
  968. $this->assertFalse(TRUE);
  969. }
  970. }
  971. /*
  972. * upload large object,请求头部携带自定义头部x-cos-meta-
  973. * 200
  974. */
  975. public function testUploadLargeObjectMeta() {
  976. try {
  977. $key = '你好.txt';
  978. $meta = array(
  979. 'test' => str_repeat('a', 1 * 1024),
  980. 'test-meta' => 'qwe-23ds-ad-xcz.asd.*qweqw'
  981. );
  982. $body = $this->generateRandomString(2*1024*1024+1023);
  983. $this->cosClient->upload($bucket=$this->bucket,
  984. $key=$key,
  985. $body=$body,
  986. $options=['PartSize'=>1024 * 1024 + 1, 'Metadata'=>$meta]);
  987. $rt = $this->cosClient->headObject(['Bucket'=>$this->bucket, 'Key'=>$key]);
  988. $this->assertEquals($rt['Metadata'], $meta);
  989. } catch (ServiceResponseException $e) {
  990. print $e;
  991. $this->assertFalse(TRUE);
  992. }
  993. }
  994. /*
  995. * put object,请求头部携带自定义头部x-cos-meta-
  996. * KeyTooLong
  997. * 400
  998. */
  999. public function testPutObjectMeta2K() {
  1000. try {
  1001. $this->cosClient->putObject(array(
  1002. 'Bucket' => $this->bucket,
  1003. 'Key' => '你好.txt',
  1004. 'Body' => '1234124',
  1005. 'Metadata' => array(
  1006. 'lew' => str_repeat('a', 3 * 1024),
  1007. )));
  1008. } catch (ServiceResponseException $e) {
  1009. $this->assertEquals(
  1010. [$e->getStatusCode(), $e->getExceptionCode()],
  1011. [400, 'KeyTooLong']
  1012. );
  1013. print $e;
  1014. }
  1015. }
  1016. /*
  1017. * 上传复杂文件名的文件
  1018. * 200
  1019. */
  1020. public function testUploadComplexObject() {
  1021. try {
  1022. $key = '→↓←→↖↗↙↘! \"#$%&\'()*+,-./0123456789:;<=>@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\]^_`abcdefghijklmnopqrstuvwxyz{|}~';
  1023. $this->cosClient->upload($this->bucket, $key, 'Hello World');
  1024. $this->cosClient->headObject(array(
  1025. 'Bucket' => $this->bucket,
  1026. 'Key' => $key
  1027. ));
  1028. } catch (ServiceResponseException $e) {
  1029. print $e;
  1030. $this->assertFalse(TRUE);
  1031. }
  1032. }
  1033. /*
  1034. * 上传大文件
  1035. * 200
  1036. */
  1037. public function testUploadLargeObject() {
  1038. try {
  1039. $key = '你好.txt';
  1040. $body = $this->generateRandomString(2*1024*1024+1023);
  1041. $md5 = base64_encode(md5($body, true));
  1042. $this->cosClient->upload($bucket=$this->bucket,
  1043. $key=$key,
  1044. $body=$body,
  1045. $options=['PartSize'=>1024 * 1024 + 1]);
  1046. $rt = $this->cosClient->getObject(['Bucket'=>$this->bucket, 'Key'=>$key]);
  1047. $download_md5 = base64_encode(md5($rt['Body'], true));
  1048. $this->assertEquals($md5, $download_md5);
  1049. } catch (ServiceResponseException $e) {
  1050. print $e;
  1051. $this->assertFalse(TRUE);
  1052. }
  1053. }
  1054. /*
  1055. * 断点重传
  1056. * 200
  1057. */
  1058. public function testResumeUpload() {
  1059. try {
  1060. $key = '你好.txt';
  1061. $body = $this->generateRandomString(3*1024*1024+1023);
  1062. $partSize = 1024 * 1024 + 1;
  1063. $md5 = base64_encode(md5($body, true));
  1064. $rt = $this->cosClient->CreateMultipartUpload(['Bucket' => $this->bucket,
  1065. 'Key' => $key]);
  1066. $uploadId = $rt['UploadId'];
  1067. $this->cosClient->uploadPart(['Bucket' => $this->bucket,
  1068. 'Key' => $key,
  1069. 'Body' => substr($body, 0, $partSize),
  1070. 'UploadId' => $uploadId,
  1071. 'PartNumber' => 1]);
  1072. $rt = $this->cosClient->ListParts(['Bucket' => $this->bucket,
  1073. 'Key' => $key,
  1074. 'UploadId' => $uploadId]);
  1075. $this->assertEquals(count($rt['Parts']), 1);
  1076. $this->cosClient->resumeUpload($bucket=$this->bucket,
  1077. $key=$key,
  1078. $body=$body,
  1079. $uploadId=$uploadId,
  1080. $options=['PartSize'=>$partSize]);
  1081. $rt = $this->cosClient->getObject(['Bucket'=>$this->bucket, 'Key'=>$key]);
  1082. $download_md5 = base64_encode(md5($rt['Body'], true));
  1083. $this->assertEquals($md5, $download_md5);
  1084. } catch (ServiceResponseException $e) {
  1085. print $e;
  1086. $this->assertFalse(TRUE);
  1087. }
  1088. }
  1089. /*
  1090. * 下载文件
  1091. * 200
  1092. */
  1093. public function testGetObject() {
  1094. try {
  1095. $this->cosClient->upload($this->bucket, '你好.txt', 'Hello World');
  1096. $this->cosClient->getObject(array(
  1097. 'Bucket' => $this->bucket,
  1098. 'Key' => '你好.txt',));
  1099. } catch (ServiceResponseException $e) {
  1100. print $e;
  1101. $this->assertFalse(TRUE);
  1102. }
  1103. }
  1104. /*
  1105. * get object,object名称包含特殊字符
  1106. * 200
  1107. */
  1108. public function testGetObjectSpecialName() {
  1109. try {
  1110. $this->cosClient->upload($this->bucket, '你好<>!@#^%^&*&(&^!@#@!.txt', 'Hello World');
  1111. $this->cosClient->getObject(array(
  1112. 'Bucket' => $this->bucket,
  1113. 'Key' => '你好<>!@#^%^&*&(&^!@#@!.txt',));
  1114. } catch (ServiceResponseException $e) {
  1115. print $e;
  1116. $this->assertFalse(TRUE);
  1117. }
  1118. }
  1119. /*
  1120. * get object,请求头部带if-match,参数值为true
  1121. * 200
  1122. */
  1123. public function testGetObjectIfMatchTrue() {
  1124. try {
  1125. $this->cosClient->upload($this->bucket, '你好.txt', 'Hello World');
  1126. $this->cosClient->getObject(array(
  1127. 'Bucket' => $this->bucket,
  1128. 'Key' => '你好.txt',
  1129. 'IfMatch' => '"b10a8db164e0754105b7a99be72e3fe5"'));
  1130. } catch (ServiceResponseException $e) {
  1131. print $e;
  1132. $this->assertFalse(TRUE);
  1133. }
  1134. }
  1135. /*
  1136. * get object,请求头部带if-match,参数值为false
  1137. * PreconditionFailed
  1138. * 412
  1139. */
  1140. public function testGetObjectIfMatchFalse() {
  1141. try {
  1142. $this->cosClient->upload($this->bucket, '你好.txt', 'Hello World');
  1143. $this->cosClient->getObject(array(
  1144. 'Bucket' => $this->bucket,
  1145. 'Key' => '你好.txt',
  1146. 'IfMatch' => '""'));
  1147. } catch (ServiceResponseException $e) {
  1148. $this->assertEquals(
  1149. [$e->getStatusCode(), $e->getExceptionCode()],
  1150. [412, 'PreconditionFailed']
  1151. );
  1152. print $e;
  1153. }
  1154. }
  1155. /*
  1156. * get object,请求头部带if-none-match,参数值为true
  1157. * 200
  1158. */
  1159. public function testGetObjectIfNoneMatchTrue() {
  1160. try {
  1161. $this->cosClient->upload($this->bucket, '你好.txt', 'Hello World');
  1162. $rt = $this->cosClient->getObject(array(
  1163. 'Bucket' => $this->bucket,
  1164. 'Key' => '你好.txt',
  1165. 'IfNoneMatch' => '"b10a8db164e0754105b7a99be72e3fe5"'));
  1166. } catch (ServiceResponseException $e) {
  1167. print $e;
  1168. $this->assertFalse(TRUE);
  1169. }
  1170. }
  1171. /*
  1172. * get object,请求头部带if-none-match,参数值为false
  1173. * PreconditionFailed
  1174. * 412
  1175. */
  1176. public function testGetObjectIfNoneMatchFalse() {
  1177. try {
  1178. $this->cosClient->upload($this->bucket, '你好.txt', 'Hello World');
  1179. $this->cosClient->getObject(array(
  1180. 'Bucket' => $this->bucket,
  1181. 'Key' => '你好.txt',
  1182. 'IfNoneMatch' => '""'));
  1183. } catch (ServiceResponseException $e) {
  1184. print $e;
  1185. $this->assertFalse(TRUE);
  1186. }
  1187. }
  1188. /*
  1189. * 获取文件url
  1190. * 200
  1191. */
  1192. public function testGetObjectUrl() {
  1193. try{
  1194. $this->cosClient->getObjectUrl($this->bucket, 'hello.txt', '+10 minutes');
  1195. } catch (ServiceResponseException $e) {
  1196. print $e;
  1197. $this->assertFalse(TRUE);
  1198. }
  1199. }
  1200. /*
  1201. * 复制小文件
  1202. * 200
  1203. */
  1204. public function testCopySmallObject() {
  1205. try{
  1206. $this->cosClient->upload($this->bucket, '你好.txt', 'Hello World');
  1207. $this->cosClient->copy($bucket=$this->bucket,
  1208. $key='hi.txt',
  1209. $copySource = ['Bucket'=>$this->bucket,
  1210. 'Region'=>$this->region,
  1211. 'Key'=>'你好.txt']);
  1212. } catch (ServiceResponseException $e) {
  1213. print $e;
  1214. $this->assertFalse(TRUE);
  1215. }
  1216. }
  1217. /*
  1218. * 复制大文件
  1219. * 200
  1220. */
  1221. public function testCopyLargeObject() {
  1222. try{
  1223. $src_key = '你好.txt';
  1224. $dst_key = 'hi.txt';
  1225. $body = $this->generateRandomString(2*1024*1024+333);
  1226. $md5 = base64_encode(md5($body, true));
  1227. $this->cosClient->upload($bucket=$this->bucket,
  1228. $key=$src_key,
  1229. $body=$body,
  1230. $options=['PartSize'=>1024 * 1024 + 1]);
  1231. $this->cosClient->copy($bucket=$this->bucket,
  1232. $key=$dst_key,
  1233. $copySource = ['Bucket'=>$this->bucket,
  1234. 'Region'=>$this->region,
  1235. 'Key'=>$src_key],
  1236. $options=['PartSize'=>1024 * 1024 + 1]);
  1237. $rt = $this->cosClient->getObject(['Bucket'=>$this->bucket, 'Key'=>$dst_key]);
  1238. $download_md5 = base64_encode(md5($rt['Body'], true));
  1239. $this->assertEquals($md5, $download_md5);
  1240. } catch (ServiceResponseException $e) {
  1241. print $e;
  1242. $this->assertFalse(TRUE);
  1243. }
  1244. }
  1245. /*
  1246. * 设置objectacl
  1247. * 200
  1248. */
  1249. public function testPutObjectACL() {
  1250. try {
  1251. $this->cosClient->upload($this->bucket, '11', 'hello.txt');
  1252. $this->cosClient->PutObjectAcl(
  1253. array(
  1254. 'Bucket' => $this->bucket,
  1255. 'Key' => '11',
  1256. 'Grants' => array(
  1257. array(
  1258. 'Grantee' => array(
  1259. 'DisplayName' => 'qcs::cam::uin/2779643970:uin/2779643970',
  1260. 'ID' => 'qcs::cam::uin/2779643970:uin/2779643970',
  1261. 'Type' => 'CanonicalUser',
  1262. ),
  1263. 'Permission' => 'FULL_CONTROL',
  1264. ),
  1265. // ... repeated
  1266. ),
  1267. 'Owner' => array(
  1268. 'DisplayName' => 'qcs::cam::uin/2779643970:uin/2779643970',
  1269. 'ID' => 'qcs::cam::uin/2779643970:uin/2779643970',
  1270. )
  1271. )
  1272. );
  1273. } catch (ServiceResponseException $e) {
  1274. print $e;
  1275. $this->assertFalse(TRUE);
  1276. }
  1277. }
  1278. /*
  1279. * 获取objectacl
  1280. * 200
  1281. */
  1282. public function testGetObjectACL()
  1283. {
  1284. try {
  1285. $this->cosClient->upload($this->bucket, '11', 'hello.txt');
  1286. $this->cosClient->PutObjectAcl(
  1287. array(
  1288. 'Bucket' => $this->bucket,
  1289. 'Key' => '11',
  1290. 'Grants' => array(
  1291. array(
  1292. 'Grantee' => array(
  1293. 'DisplayName' => 'qcs::cam::uin/2779643970:uin/2779643970',
  1294. 'ID' => 'qcs::cam::uin/2779643970:uin/2779643970',
  1295. 'Type' => 'CanonicalUser',
  1296. ),
  1297. 'Permission' => 'FULL_CONTROL',
  1298. ),
  1299. ),
  1300. 'Owner' => array(
  1301. 'DisplayName' => 'qcs::cam::uin/2779643970:uin/2779643970',
  1302. 'ID' => 'qcs::cam::uin/2779643970:uin/2779643970',
  1303. )
  1304. )
  1305. );
  1306. } catch (ServiceResponseException $e) {
  1307. print $e;
  1308. $this->assertFalse(TRUE);
  1309. }
  1310. }
  1311. /*
  1312. * put object acl,设置object公共权限为private
  1313. * 200
  1314. */
  1315. public function testPutObjectAclPrivate()
  1316. {
  1317. try {
  1318. $this->cosClient->putObject(array('Bucket' => $this->bucket,'Key' => '你好.txt', 'Body' => '123'));
  1319. $this->cosClient->PutObjectAcl(
  1320. array(
  1321. 'Bucket' => $this->bucket,
  1322. 'Key' => '你好.txt',
  1323. 'ACL'=>'private'
  1324. )
  1325. );
  1326. } catch (ServiceResponseException $e) {
  1327. print $e;
  1328. $this->assertFalse(TRUE);
  1329. }
  1330. }
  1331. /*
  1332. * put object acl,设置object公共权限为public-read
  1333. * 200
  1334. */
  1335. public function testPutObjectAclPublicRead()
  1336. {
  1337. try {
  1338. $this->cosClient->putObject(array('Bucket' => $this->bucket,'Key' => '你好.txt', 'Body' => '123'));
  1339. $this->cosClient->PutObjectAcl(
  1340. array(
  1341. 'Bucket' => $this->bucket,
  1342. 'Key' => '你好.txt',
  1343. 'ACL'=>'public-read'
  1344. )
  1345. );
  1346. } catch (ServiceResponseException $e) {
  1347. print $e;
  1348. $this->assertFalse(TRUE);
  1349. }
  1350. }
  1351. /*
  1352. * put object acl,公共权限非法
  1353. * InvalidArgument
  1354. * 400
  1355. */
  1356. public function testPutObjectAclInvalid()
  1357. {
  1358. try {
  1359. $this->cosClient->putObject(array('Bucket' => $this->bucket,'Key' => '你好.txt', 'Body' => '123'));
  1360. $this->cosClient->PutObjectAcl(
  1361. array(
  1362. 'Bucket' => $this->bucket,
  1363. 'Key' => '你好.txt',
  1364. 'ACL'=>'public'
  1365. )
  1366. );
  1367. } catch (ServiceResponseException $e) {
  1368. $this->assertTrue($e->getExceptionCode() === 'InvalidArgument' && $e->getStatusCode() === 400);
  1369. }
  1370. }
  1371. /*
  1372. * put object acl,设置object账号权限为grant-read
  1373. * 200
  1374. */
  1375. public function testPutObjectAclReadToUser()
  1376. {
  1377. try {
  1378. $this->cosClient->putObject(array('Bucket' => $this->bucket,'Key' => '你好.txt', 'Body' => '123'));
  1379. $this->cosClient->PutObjectAcl(
  1380. array(
  1381. 'Bucket' => $this->bucket,
  1382. 'Key' => '你好.txt',
  1383. 'GrantRead' => 'id="qcs::cam::uin/2779643970:uin/2779643970"'
  1384. )
  1385. );
  1386. } catch (ServiceResponseException $e) {
  1387. print $e;
  1388. $this->assertFalse(TRUE);
  1389. }
  1390. }
  1391. /*
  1392. * put object acl,设置object账号权限为grant-full-control
  1393. * 200
  1394. */
  1395. public function testPutObjectAclFullToUser()
  1396. {
  1397. try {
  1398. $this->cosClient->putObject(array('Bucket' => $this->bucket,'Key' => '你好.txt', 'Body' => '123'));
  1399. $this->cosClient->PutObjectAcl(
  1400. array(
  1401. 'Bucket' => $this->bucket,
  1402. 'Key' => '你好.txt',
  1403. 'GrantFullControl' => 'id="qcs::cam::uin/2779643970:uin/2779643970"'
  1404. )
  1405. );
  1406. } catch (ServiceResponseException $e) {
  1407. print $e;
  1408. $this->assertFalse(TRUE);
  1409. }
  1410. }
  1411. /*
  1412. * put object acl,设置object账号权限,同时授权给多个账户
  1413. * 200
  1414. */
  1415. public function testPutObjectAclToUsers()
  1416. {
  1417. try {
  1418. $this->cosClient->putObject(array('Bucket' => $this->bucket,'Key' => '你好.txt', 'Body' => '123'));
  1419. $this->cosClient->PutObjectAcl(
  1420. array(
  1421. 'Bucket' => $this->bucket,
  1422. 'Key' => '你好.txt',
  1423. 'GrantFullControl' => 'id="qcs::cam::uin/2779643970:uin/2779643970",id="qcs::cam::uin/2779643970:uin/2779643970",id="qcs::cam::uin/2779643970:uin/2779643970"'
  1424. )
  1425. );
  1426. } catch (ServiceResponseException $e) {
  1427. print $e;
  1428. $this->assertFalse(TRUE);
  1429. }
  1430. }
  1431. /*
  1432. * put object acl,设置object账号权限,授权给子账号
  1433. * 200
  1434. */
  1435. public function testPutObjectAclToSubuser()
  1436. {
  1437. try {
  1438. $this->cosClient->putObject(array('Bucket' => $this->bucket,'Key' => '你好.txt', 'Body' => '123'));
  1439. $this->cosClient->PutObjectAcl(
  1440. array(
  1441. 'Bucket' => $this->bucket,
  1442. 'Key' => '你好.txt',
  1443. 'GrantFullControl' => 'id="qcs::cam::uin/2779643970:uin/2779643970"'
  1444. )
  1445. );
  1446. } catch (ServiceResponseException $e) {
  1447. print $e;
  1448. $this->assertFalse(TRUE);
  1449. }
  1450. }
  1451. /*
  1452. * put object acl,设置object账号权限,grant值非法
  1453. * InvalidArgument
  1454. * 400
  1455. */
  1456. public function testPutObjectAclInvalidGrant()
  1457. {
  1458. try {
  1459. $this->cosClient->putObject(array('Bucket' => $this->bucket,'Key' => '你好.txt', 'Body' => '123'));
  1460. $this->cosClient->PutObjectAcl(
  1461. array(
  1462. 'Bucket' => $this->bucket,
  1463. 'Key' => '你好.txt',
  1464. 'GrantFullControl' => 'id="qcs::camuin/321023:uin/2779643970"'
  1465. )
  1466. );
  1467. } catch (ServiceResponseException $e) {
  1468. $this->assertTrue($e->getExceptionCode() === 'InvalidArgument' && $e->getStatusCode() === 400);
  1469. }
  1470. }
  1471. /*
  1472. * put object acl,设置object账号权限,通过body方式授权
  1473. * 200
  1474. */
  1475. public function testPutObjectAclByBody()
  1476. {
  1477. try {
  1478. $this->cosClient->putObject(array('Bucket' => $this->bucket,'Key' => '你好.txt', 'Body' => '123'));
  1479. $this->cosClient->PutObjectAcl(
  1480. array(
  1481. 'Bucket' => $this->bucket,
  1482. 'Key' => '你好.txt',
  1483. 'Grants' => array(
  1484. array(
  1485. 'Grantee' => array(
  1486. 'DisplayName' => 'qcs::cam::uin/2779643970:uin/2779643970',
  1487. 'ID' => 'qcs::cam::uin/2779643970:uin/2779643970',
  1488. 'Type' => 'CanonicalUser',
  1489. ),
  1490. 'Permission' => 'FULL_CONTROL',
  1491. ),
  1492. // ... repeated
  1493. ),
  1494. 'Owner' => array(
  1495. 'DisplayName' => 'qcs::cam::uin/2779643970:uin/2779643970',
  1496. 'ID' => 'qcs::cam::uin/2779643970:uin/2779643970',
  1497. )
  1498. )
  1499. );
  1500. } catch (ServiceResponseException $e) {
  1501. print $e;
  1502. $this->assertFalse(TRUE);
  1503. }
  1504. }
  1505. /*
  1506. * put object acl,设置object账号权限,通过body方式授权给anyone
  1507. * 200
  1508. */
  1509. public function testPutObjectAclByBodyToAnyone()
  1510. {
  1511. try {
  1512. $this->cosClient->putObject(array('Bucket' => $this->bucket,'Key' => '你好.txt', 'Body' => '123'));
  1513. $this->cosClient->putObjectAcl(
  1514. array(
  1515. 'Bucket' => $this->bucket,
  1516. 'Key' => '你好.txt',
  1517. 'Grants' => array(
  1518. array(
  1519. 'Grantee' => array(
  1520. 'DisplayName' => 'qcs::cam::anyone:anyone',
  1521. 'ID' => 'qcs::cam::anyone:anyone',
  1522. 'Type' => 'CanonicalUser',
  1523. ),
  1524. 'Permission' => 'FULL_CONTROL',
  1525. ),
  1526. // ... repeated
  1527. ),
  1528. 'Owner' => array(
  1529. 'DisplayName' => 'qcs::cam::uin/2779643970:uin/2779643970',
  1530. 'ID' => 'qcs::cam::uin/2779643970:uin/2779643970',
  1531. )
  1532. )
  1533. );
  1534. } catch (ServiceResponseException $e) {
  1535. print $e;
  1536. $this->assertFalse(TRUE);
  1537. }
  1538. }
  1539. /*
  1540. * selectobject,select检索数据
  1541. * 200
  1542. */
  1543. public function testSelectObjectContent()
  1544. {
  1545. $key = '你好.txt';
  1546. try {
  1547. $body = "appid,bucket,region
  1548. 12500001,22weqwe,sh
  1549. 12500002,we2qwe,sh
  1550. 12500003,weq3we,sh
  1551. 12500004,weqw4e,sh
  1552. 3278522,azxc,gz
  1553. 4343,ewqew,tj";
  1554. $this->cosClient->putObject(array('Bucket' => $this->bucket,'Key' => $key, 'Body' => $body));
  1555. $result = $this->cosClient->selectObjectContent(array(
  1556. 'Bucket' => $this->bucket, //格式:BucketName-APPID
  1557. 'Key' => $key,
  1558. 'Expression' => 'Select * from COSObject s',
  1559. 'ExpressionType' => 'SQL',
  1560. 'InputSerialization' => array(
  1561. 'CompressionType' => 'None',
  1562. 'CSV' => array(
  1563. 'FileHeaderInfo' => 'USE',
  1564. 'RecordDelimiter' => '\n',
  1565. 'FieldDelimiter' => ',',
  1566. 'QuoteEscapeCharacter' => '"',
  1567. 'Comments' => '#',
  1568. 'AllowQuotedRecordDelimiter' => 'FALSE'
  1569. )
  1570. ),
  1571. 'OutputSerialization' => array(
  1572. 'CSV' => array(
  1573. 'QuoteField' => 'ASNEEDED',
  1574. 'RecordDelimiter' => '\n',
  1575. 'FieldDelimiter' => ',',
  1576. 'QuoteCharacter' => '"',
  1577. 'QuoteEscapeCharacter' => '"'
  1578. )
  1579. ),
  1580. 'RequestProgress' => array(
  1581. 'Enabled' => 'FALSE'
  1582. )
  1583. ));
  1584. foreach ($result['Data'] as $data) {
  1585. }
  1586. } catch (\Exception $e) {
  1587. print ($e);
  1588. $this->assertFalse(TRUE);
  1589. }
  1590. }
  1591. }