ObsPhpDemo.php 49 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406
  1. <?php
  2. /**
  3. * Copyright 2019 Huawei Technologies Co.,Ltd.
  4. * Licensed under the Apache License, Version 2.0 (the "License"); you may not use
  5. * this file except in compliance with the License. You may obtain a copy of the
  6. * License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software distributed
  11. * under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
  12. * CONDITIONS OF ANY KIND, either express or implied. See the License for the
  13. * specific language governing permissions and limitations under the License.
  14. *
  15. */
  16. if (file_exists('vendor/autoload.php')) {
  17. require 'vendor/autoload.php';
  18. } else {
  19. require '../vendor/autoload.php'; // sample env
  20. }
  21. if (file_exists('obs-autoloader.php')) {
  22. require 'obs-autoloader.php';
  23. } else {
  24. require '../obs-autoloader.php'; // sample env
  25. }
  26. use Obs\ObsClient;
  27. use Obs\ObsException;
  28. use function GuzzleHttp\json_encode;
  29. $ak = '*** Provide your Access Key ***';
  30. $sk = '*** Provide your Secret Key ***';
  31. $endpoint = 'https://your-endpoint:443';
  32. $obsClient = ObsClient::factory(array (
  33. 'key' => $ak,
  34. 'secret' => $sk,
  35. 'endpoint' => $endpoint,
  36. ));
  37. $obsClient->initLog(array (
  38. 'FilePath' => './logs',
  39. 'FileName' => 'eSDK-OBS-PHP.log',
  40. 'MaxFiles' => 10,
  41. 'Level' => WARN
  42. ));
  43. $bucketName = 'bucket000';
  44. $objectKey = 'test';
  45. // create bucket
  46. function CreateBucket() {
  47. global $obsClient;
  48. global $bucketName;
  49. echo "create bucket start...\n";
  50. try {
  51. $resp = $obsClient->createBucket(array (
  52. 'Bucket' => $bucketName,
  53. 'ACL' => ObsClient::AclPrivate,
  54. 'LocationConstraint' => '',
  55. 'StorageClass' => ObsClient::StorageClassWarm
  56. ));
  57. printf("HttpStatusCode:%s\n", $resp ['HttpStatusCode']);
  58. printf("Location:%s\n", $resp ['Location']);
  59. printf("RequestId:%s\n", $resp ['RequestId']);
  60. } catch ( ObsException $e ) {
  61. echo $e;
  62. }
  63. }
  64. // list buckets
  65. function ListBuckets() {
  66. global $obsClient;
  67. echo "list bucket start...\n";
  68. try {
  69. $resp = $obsClient->listBuckets();
  70. printf("HttpStatusCode:%s\n", $resp ['HttpStatusCode']);
  71. printf("RequestId:%s\n", $resp ['RequestId']);
  72. $i = 0;
  73. foreach ( $resp ['Buckets'] as $bucket ) {
  74. printf("Buckets[$i][Name]:%s,Buckets[$i][CreationDate]:%s\n", $bucket ['Name'], $bucket ['CreationDate']);
  75. $i ++;
  76. }
  77. printf("Owner[ID]:%s\n", $resp ['Owner'] ['ID']);
  78. } catch ( ObsException $e ) {
  79. echo $e;
  80. }
  81. }
  82. // delete bucket
  83. function DeleteBucket() {
  84. global $obsClient;
  85. global $bucketName;
  86. echo "delete bucket start...\n";
  87. try {
  88. $resp = $obsClient->deleteBucket(array (
  89. 'Bucket' => $bucketName
  90. ));
  91. printf("HttpStatusCode:%s\n", $resp ['HttpStatusCode']);
  92. printf("RequestId:%s\n", $resp ['RequestId']);
  93. } catch ( ObsException $e ) {
  94. echo $e;
  95. }
  96. }
  97. // list objects
  98. function ListObjects() {
  99. global $obsClient;
  100. global $bucketName;
  101. echo "list objects start...\n";
  102. try {
  103. $resp = $obsClient->listObjects(array (
  104. 'Bucket' => $bucketName,
  105. 'Delimiter' => '',
  106. 'Marker' => '',
  107. 'MaxKeys' => '',
  108. 'Prefix' => ''
  109. ));
  110. printf("HttpStatusCode:%s\n", $resp ['HttpStatusCode']);
  111. printf("RequestId:%s\n", $resp ['RequestId']);
  112. printf("IsTruncated:%d,Marker:%s,NextMarker:%s,Name:%s\n", $resp ['IsTruncated'], $resp ['Marker'], $resp ['NextMarker'], $resp ['Name']);
  113. printf("Prefix:%s,Delimiter:%s,MaxKeys:%d\n", $resp ['Prefix'], $resp ['Delimiter'], $resp ['MaxKeys']);
  114. $i = 0;
  115. foreach ( $resp ['CommonPrefixes'] as $CommonPrefixe ) {
  116. printf("CommonPrefixes[$i][Prefix]:%s\n", $CommonPrefixe ['Prefix']);
  117. $i ++;
  118. }
  119. $i = 0;
  120. foreach ( $resp ['Contents'] as $content ) {
  121. printf("Contents[$i][ETag]:%s,Contents[$i][Size]:%d,Contents[$i][StorageClass]:%s\n", $content ['ETag'], $content ['Size'], $content ['StorageClass']);
  122. printf("Contents[$i][Key]:%s,Contents[$i][LastModified]:%s\n", $content ['Key'], $content ['LastModified']);
  123. printf("Contents[$i][Owner][ID]:%s\n", $content ['Owner'] ['ID']);
  124. $i ++;
  125. }
  126. } catch ( ObsException $e ) {
  127. echo $e;
  128. }
  129. }
  130. // list versions
  131. function ListVersions() {
  132. global $obsClient;
  133. global $bucketName;
  134. echo "list versions start...\n";
  135. try {
  136. $resp = $obsClient->listVersions(array (
  137. 'Bucket' => $bucketName,
  138. 'Delimiter' => '',
  139. 'KeyMarker' => '',
  140. 'MaxKeys' => '',
  141. 'Prefix' => '',
  142. 'VersionIdMarker' => ''
  143. ));
  144. printf("HttpStatusCode:%s\n", $resp ['HttpStatusCode']);
  145. printf("RequestId:%s\n", $resp ['RequestId']);
  146. printf("IsTruncated:%d,KeyMarker:%s,VersionIdMarker:%s,NextKeyMarker:%s\n", $resp ['IsTruncated'], $resp ['KeyMarker'], $resp ['VersionIdMarker'], $resp ['NextKeyMarker']);
  147. printf("NextVersionIdMarker:%s,Name:%s,Prefix:%s,Delimiter:%s,MaxKeys:%s\n", $resp ['NextVersionIdMarker'], $resp ['Name'], $resp ['Prefix'], $resp ['Delimiter'], $resp ['MaxKeys']);
  148. $i = 0;
  149. foreach ( $resp ['CommonPrefixes'] as $CommonPrefixe ) {
  150. printf("CommonPrefixes[$i][Prefix]:%s\n", $CommonPrefixe ['Prefix']);
  151. $i ++;
  152. }
  153. $i = 0;
  154. foreach ( $resp ['Versions'] as $version ) {
  155. printf("Versions[$i][ETag]:%s,Versions[$i][Size]:%d,Versions[$i][StorageClass]:%s\n", $version ['ETag'], $version ['Size'], $version ['StorageClass']);
  156. printf("Versions[$i][Key]:%s,Versions[$i][VersionId]:%s,Versions[$i][IsLatest]:%d,Versions[$i][LastModified]:%s\n", $version ['Key'], $version ['VersionId'], $version ['IsLatest'], $version ['LastModified']);
  157. printf("Versions[$i][Owner][ID]:%s\n", $version ['Owner'] ['ID']);
  158. $i ++;
  159. }
  160. $i = 0;
  161. foreach ( $resp ['DeleteMarkers'] as $deleteMarker ) {
  162. printf("DeleteMarkers[$i][Key]:%s,DeleteMarkers[$i][VersionId]:%s,DeleteMarkers[$i][IsLatest]:%d,DeleteMarkers[$i][LastModified]:%s\n", $deleteMarker ['Key'], $deleteMarker ['VersionId'], $deleteMarker ['IsLatest'], $deleteMarker ['LastModified']);
  163. printf("DeleteMarkers[$i][Owner][ID]:%s\n", $deleteMarker ['Owner'] ['ID']);
  164. $i ++;
  165. }
  166. } catch ( ObsException $e ) {
  167. echo $e;
  168. }
  169. }
  170. // head bucket
  171. function HeadBucket() {
  172. global $obsClient;
  173. global $bucketName;
  174. echo "head bucket start...\n";
  175. try {
  176. $resp = $obsClient->headBucket(array (
  177. 'Bucket' => $bucketName
  178. ));
  179. printf("HttpStatusCode:%s\n", $resp ['HttpStatusCode']);
  180. printf("RequestId:%s\n", $resp ['RequestId']);
  181. } catch ( ObsException $e ) {
  182. echo $e;
  183. }
  184. }
  185. // get bucket metadata
  186. function GetBucketMetadata() {
  187. global $obsClient;
  188. global $bucketName;
  189. echo "get bucket metatdata start...\n";
  190. try {
  191. $resp = $obsClient->getBucketMetadata(array (
  192. "Bucket" => $bucketName,
  193. "Origin" => "www.example.com",
  194. "RequestHeader" => "header1"
  195. ));
  196. printf("HttpStatusCode:%s\n", $resp ['HttpStatusCode']);
  197. printf("StorageClass:%s\n", $resp ["StorageClass"]);
  198. printf("AllowOrigin:%s\n", $resp ["AllowOrigin"]);
  199. printf("MaxAgeSeconds:%s\n", $resp ["MaxAgeSeconds"]);
  200. printf("ExposeHeader:%s\n", $resp ["ExposeHeader"]);
  201. printf("AllowHeader:%s\n", $resp ["AllowHeader"]);
  202. printf("AllowMethod:%s\n", $resp ["AllowMethod"]);
  203. } catch ( ObsException $e ) {
  204. echo $e;
  205. }
  206. }
  207. // get bucket location
  208. function GetBucketLocation() {
  209. global $obsClient;
  210. global $bucketName;
  211. echo "get bucket location start...\n";
  212. try {
  213. $resp = $obsClient->getBucketLocation(array (
  214. 'Bucket' => $bucketName
  215. ));
  216. printf("HttpStatusCode:%s\n", $resp ['HttpStatusCode']);
  217. printf("RequestId:%s\n", $resp ['RequestId']);
  218. printf("Location:%s\n", $resp ['Location']);
  219. } catch ( ObsException $e ) {
  220. echo $e;
  221. }
  222. }
  223. // get bucket storageinfo
  224. function GetBucketStorageInfo() {
  225. global $obsClient;
  226. global $bucketName;
  227. echo "get bucket storage info start...\n";
  228. try {
  229. $resp = $obsClient->getBucketStorageInfo(array (
  230. 'Bucket' => $bucketName
  231. ));
  232. printf("HttpStatusCode:%s\n", $resp ['HttpStatusCode']);
  233. printf("RequestId:%s\n", $resp ['RequestId']);
  234. printf("Size:%d,ObjectNumber:%d\n", $resp ['Size'], $resp ['ObjectNumber']);
  235. } catch ( ObsException $e ) {
  236. echo $e;
  237. }
  238. }
  239. // set bucket quota
  240. function SetBucketQuota() {
  241. global $obsClient;
  242. global $bucketName;
  243. echo "set bucket quota start...\n";
  244. try {
  245. $resp = $obsClient->setBucketQuota(array (
  246. 'Bucket' => $bucketName,
  247. 'StorageQuota' => 1048576
  248. ));
  249. printf("HttpStatusCode:%s\n", $resp ['HttpStatusCode']);
  250. printf("RequestId:%s\n", $resp ['RequestId']);
  251. } catch ( ObsException $e ) {
  252. echo $e;
  253. }
  254. }
  255. // get bucket quota
  256. function GetBucketQuota() {
  257. global $obsClient;
  258. global $bucketName;
  259. echo "get bucket quota start...\n";
  260. try {
  261. $resp = $obsClient->getBucketQuota(array (
  262. 'Bucket' => $bucketName
  263. ));
  264. printf("HttpStatusCode:%s\n", $resp ['HttpStatusCode']);
  265. printf("RequestId:%s\n", $resp ['RequestId']);
  266. printf("StorageQuota:%s\n", $resp ['StorageQuota']);
  267. } catch ( ObsException $e ) {
  268. echo $e;
  269. }
  270. }
  271. // set bucket storage policy
  272. function SetBucketStoragePolicy() {
  273. global $obsClient;
  274. global $bucketName;
  275. echo "set bucket storage policy start...\n";
  276. try {
  277. $resp = $obsClient->setBucketStoragePolicy(array (
  278. 'Bucket' => $bucketName,
  279. 'StorageClass' => ObsClient::StorageClassCold
  280. ));
  281. printf("HttpStatusCode:%s\n", $resp ['HttpStatusCode']);
  282. printf("RequestId:%s\n", $resp ['RequestId']);
  283. } catch ( ObsException $e ) {
  284. echo $e;
  285. }
  286. }
  287. // get bucket storage policy
  288. function GetBucketStoragePolicy() {
  289. global $obsClient;
  290. global $bucketName;
  291. echo "get bucket storage policy start...\n";
  292. try {
  293. $resp = $obsClient->getBucketStoragePolicy(array (
  294. 'Bucket' => $bucketName
  295. ));
  296. printf("HttpStatusCode:%s\n", $resp ['HttpStatusCode']);
  297. printf("RequestId:%s\n", $resp ['RequestId']);
  298. printf("StorageClass:%s\n", $resp ['StorageClass']);
  299. } catch ( ObsException $e ) {
  300. echo $e;
  301. }
  302. }
  303. // set bucket acl
  304. function SetBucketAcl() {
  305. global $obsClient;
  306. global $bucketName;
  307. echo "set bucket ACL start...\n";
  308. try {
  309. $resp = $obsClient->setBucketAcl(array (
  310. 'Bucket' => $bucketName,
  311. 'ACL' => '',
  312. 'Owner' => array (
  313. 'ID' => 'ownerid'
  314. ),
  315. 'Grants' => array (
  316. 0 => array (
  317. 'Grantee' => array (
  318. 'ID' => 'userid'
  319. ),
  320. 'Permission' => ObsClient::PermissionRead,
  321. 'Delivered' => true
  322. ),
  323. 1 => array (
  324. 'Grantee' => array (
  325. 'URI' => ObsClient::AllUsers
  326. ),
  327. 'Permission' => ObsClient::PermissionWrite,
  328. 'Delivered' => true
  329. )
  330. )
  331. ));
  332. printf("HttpStatusCode:%s\n", $resp ['HttpStatusCode']);
  333. printf("RequestId:%s\n", $resp ['RequestId']);
  334. } catch ( ObsException $e ) {
  335. echo $e;
  336. }
  337. }
  338. // get bucket acl
  339. function GetBucketAcl() {
  340. global $obsClient;
  341. global $bucketName;
  342. echo "get bucket ACL start...\n";
  343. try {
  344. $resp = $obsClient->getBucketAcl(array (
  345. 'Bucket' => $bucketName
  346. ));
  347. printf("HttpStatusCode:%s\n", $resp ['HttpStatusCode']);
  348. printf("RequestId:%s\n", $resp ['RequestId']);
  349. printf("Owner[ID]:%s\n", $resp ['Owner'] ['ID'] );
  350. $i = 0;
  351. foreach ( $resp ['Grants'] as $grant ) {
  352. printf("Grants[$i][Grantee][ID]:%s,Grants[$i][Grantee][URI]:%s\n",$grant ['Grantee'] ['ID'], $grant ['Grantee'] ['URI']);
  353. printf("Grants[$i][Permission]:%s\n", $grant ['Permission']);
  354. printf("Grants[$i][Delivered]:%s\n", $grant['Delivered'] ? 'true' : 'false');
  355. $i ++;
  356. }
  357. } catch ( ObsException $e ) {
  358. echo $e;
  359. }
  360. }
  361. // set bucket logging configuration
  362. function SetBucketLogging() {
  363. global $obsClient;
  364. global $bucketName;
  365. echo "set bucket logging configuration start...\n";
  366. try {
  367. $resp = $obsClient->setBucketLogging(array (
  368. 'Bucket' => $bucketName,
  369. 'Agency' => 'your agency',
  370. 'LoggingEnabled' => array (
  371. 'TargetBucket' => 'bucket003',
  372. 'TargetPrefix' => 'bucket.log',
  373. 'TargetGrants' => array (
  374. 0 => array (
  375. 'Grantee' => array (
  376. 'ID' => 'userid'
  377. ),
  378. 'Permission' => ObsClient::PermissionRead
  379. ),
  380. 1 => array (
  381. 'Grantee' => array (
  382. 'URI' => ObsClient::AllUsers,
  383. ),
  384. 'Permission' => ObsClient::PermissionRead
  385. )
  386. )
  387. )
  388. ));
  389. printf("HttpStatusCode:%s\n", $resp ['HttpStatusCode']);
  390. printf("RequestId:%s\n", $resp ['RequestId']);
  391. } catch ( ObsException $e ) {
  392. echo $e;
  393. }
  394. }
  395. // get bucket logging configuration
  396. function GetBucketLogging() {
  397. global $obsClient;
  398. global $bucketName;
  399. echo "get bucket logging configuration start...\n";
  400. try {
  401. $resp = $obsClient->getBucketLogging(array (
  402. 'Bucket' => $bucketName
  403. ));
  404. printf("HttpStatusCode:%s\n", $resp ['HttpStatusCode']);
  405. printf("RequestId:%s\n", $resp ['RequestId']);
  406. printf("Agency:%s\n", $resp ['Agency']);
  407. printf("LoggingEnabled[TargetBucket]:%s,LoggingEnabled[TargetPrefix]:%s\n", $resp ['LoggingEnabled'] ['TargetBucket'], $resp ['LoggingEnabled'] ['TargetPrefix']);
  408. if (is_array($resp ['LoggingEnabled'] ['TargetGrants'])) {
  409. $i = 0;
  410. foreach ( $resp ['LoggingEnabled'] ['TargetGrants'] as $grant ) {
  411. printf("LoggingEnabled[$i][TargetGrants][Permission]:%s\n", $grant ['Permission']);
  412. printf("LoggingEnabled[$i][TargetGrants][Grantee][ID]:%s,LoggingEnabled[$i][TargetGrants][Grantee][URI]:%s\n", $grant ['Grantee'] ['ID'], $grant ['Grantee'] ['URI']);
  413. $i ++;
  414. }
  415. }
  416. } catch ( ObsException $e ) {
  417. echo $e;
  418. }
  419. }
  420. // set bucket policy
  421. function SetBucketPolicy() {
  422. global $obsClient;
  423. global $bucketName;
  424. echo "set bucket policy start...\n";
  425. try {
  426. $resp = $obsClient->setBucketPolicy(array (
  427. 'Bucket' => $bucketName,
  428. 'Policy' => 'your policy'
  429. ));
  430. printf("HttpStatusCode:%s\n", $resp ['HttpStatusCode']);
  431. printf("RequestId:%s\n", $resp ['RequestId']);
  432. } catch ( ObsException $e ) {
  433. echo $e;
  434. }
  435. }
  436. // get bucket policy
  437. function GetBucketPolicy() {
  438. global $obsClient;
  439. global $bucketName;
  440. echo "get bucket policy start...\n";
  441. try {
  442. $resp = $obsClient->getBucketPolicy(array (
  443. 'Bucket' => $bucketName
  444. ));
  445. printf("HttpStatusCode:%s\n", $resp ['HttpStatusCode']);
  446. printf("Policy:%s\n", $resp ['Policy']);
  447. printf("RequestId:%s\n", $resp ['RequestId']);
  448. } catch ( ObsException $e ) {
  449. echo $e;
  450. }
  451. }
  452. // delete bucket policy
  453. function DeleteBucketPolicy() {
  454. global $obsClient;
  455. global $bucketName;
  456. echo "delete bucket policy start...\n";
  457. try {
  458. $resp = $obsClient->deleteBucketPolicy(array (
  459. 'Bucket' => $bucketName
  460. ));
  461. printf("HttpStatusCode:%s\n", $resp ['HttpStatusCode']);
  462. printf("RequestId:%s\n", $resp ['RequestId']);
  463. } catch ( ObsException $e ) {
  464. echo $e;
  465. }
  466. }
  467. // set bucket lifycycle configuration
  468. function SetBucketLifecycle() {
  469. global $obsClient;
  470. global $bucketName;
  471. echo "set bucket lifecycle configuration start...\n";
  472. try {
  473. $resp = $obsClient->setBucketLifecycle(array (
  474. 'Bucket' => $bucketName,
  475. 'Rules' => array (
  476. 0 => array (
  477. 'ID' => '',
  478. 'Prefix' => 'ok',
  479. 'Status' => 'Enabled',
  480. 'Transitions' => array (
  481. 0 => array (
  482. 'StorageClass' => ObsClient::StorageClassWarm,
  483. 'Date' => '2019-02-01T00:00:00Z'
  484. ),
  485. 1 => array (
  486. 'StorageClass' => ObsClient::StorageClassCold,
  487. 'Date' => '2019-03-01T00:00:00Z'
  488. )
  489. ),
  490. 'Expiration' => array (
  491. 'Date' => '2019-04-01T00:00:00Z'
  492. ),
  493. 'NoncurrentVersionTransitions' => array (
  494. 0 => array (
  495. 'StorageClass' => ObsClient::StorageClassWarm,
  496. 'NoncurrentDays' => 30
  497. ),
  498. 1 => array (
  499. 'StorageClass' => ObsClient::StorageClassCold,
  500. 'NoncurrentDays' => 60
  501. )
  502. ),
  503. // 'Expiration'=>array('Days'=>100),
  504. 'NoncurrentVersionExpiration' => array (
  505. 'NoncurrentDays' => 60
  506. )
  507. )
  508. )
  509. ));
  510. printf("HttpStatusCode:%s\n", $resp ['HttpStatusCode']);
  511. printf("RequestId:%s\n", $resp ['RequestId']);
  512. } catch ( ObsException $e ) {
  513. echo $e;
  514. }
  515. }
  516. // get bucket lifycycle configuration
  517. function GetBucketLifecycle() {
  518. global $obsClient;
  519. global $bucketName;
  520. echo "get bucket lifecycle configuration start...\n";
  521. try {
  522. $resp = $obsClient->getBucketLifecycle(array (
  523. 'Bucket' => $bucketName
  524. ));
  525. $i = 0;
  526. foreach ( $resp ['Rules'] as $rule ) {
  527. foreach ( $rule ['Transitions'] as $index => $transition ) {
  528. printf("Rules[$i][Transitions][$index][Date]:%s,Rules[$i][Transitions][$index][StorageClass]:%s\n", $transition ['Date'], $transition ['StorageClass']);
  529. }
  530. printf("Rules[$i][Expiration][Date]:%s,Rules[$i][Expiration][Days]:%d\n", $rule ['Expiration'] ['Date'], $rule ['Expiration'] ['Days']);
  531. printf("Rules[$i][NoncurrentVersionExpiration][NoncurrentDays]:%s\n", $rule ['NoncurrentVersionExpiration'] ['NoncurrentDays']);
  532. foreach ( $rule ['NoncurrentVersionTransitions'] as $index => $noncurrentVersionTransition ) {
  533. printf("Rules[$i][NoncurrentVersionTransitions][$index][NoncurrentDays]:%d,Rules[$i][NoncurrentVersionTransitions][$index][StorageClass]:%s\n", $noncurrentVersionTransition ['NoncurrentDays'], $noncurrentVersionTransition ['StorageClass']);
  534. }
  535. printf("Rules[$i][ID]:%s,Rules[$i][Prefix]:%s,Rules[$i][Status]:%s\n", $rule ['ID'], $rule ['Prefix'], $rule ['Status']);
  536. $i ++;
  537. }
  538. printf("HttpStatusCode:%s\n", $resp ['HttpStatusCode']);
  539. printf("RequestId:%s\n", $resp ['RequestId']);
  540. } catch ( ObsException $e ) {
  541. echo $e;
  542. }
  543. }
  544. // delete bucket lifycycle configuration
  545. function DeleteBucketLifecycle() {
  546. global $obsClient;
  547. global $bucketName;
  548. echo "delete bucket lifecycle configuration start...\n";
  549. try {
  550. $resp = $obsClient->deleteBucketLifecycle(array (
  551. 'Bucket' => $bucketName
  552. ));
  553. printf("HttpStatusCode:%s\n", $resp ['HttpStatusCode']);
  554. printf("RequestId:%s\n", $resp ['RequestId']);
  555. } catch ( ObsException $e ) {
  556. echo $e;
  557. }
  558. }
  559. // set bucket website configuration
  560. function SetBucketWebsite() {
  561. global $obsClient;
  562. global $bucketName;
  563. echo "set bucket website configuration start...\n";
  564. try {
  565. $resp = $obsClient->setBucketWebsite(array (
  566. 'Bucket' => $bucketName,
  567. // 'RedirectAllRequestsTo'=>array('HostName'=>'obs.hostname','Protocol'=>'http'),
  568. 'IndexDocument' => array (
  569. 'Suffix' => 'index.html'
  570. ),
  571. 'ErrorDocument' => array (
  572. 'Key' => 'error.html'
  573. ),
  574. 'RoutingRules' => array (
  575. 0 => array (
  576. 'Condition' => array (
  577. 'KeyPrefixEquals' => 'docs/',
  578. 'HttpErrorCodeReturnedEquals' => 404
  579. ),
  580. 'Redirect' => array (
  581. 'ReplaceKeyPrefixWith' => 'documents/',
  582. 'HostName' => 'obs.hostname',
  583. 'Protocol' => 'http',
  584. 'HttpRedirectCode' => 308
  585. )
  586. )
  587. )
  588. ));
  589. printf("HttpStatusCode:%s\n", $resp ['HttpStatusCode']);
  590. printf("RequestId:%s\n", $resp ['RequestId']);
  591. } catch ( ObsException $e ) {
  592. echo $e;
  593. }
  594. }
  595. // get bucket website configuration
  596. function GetBucketWebsite() {
  597. global $obsClient;
  598. global $bucketName;
  599. echo "get bucket website configuration start...\n";
  600. try {
  601. $resp = $obsClient->GetBucketWebsite(array (
  602. 'Bucket' => $bucketName
  603. ));
  604. printf("HttpStatusCode:%s\n", $resp ['HttpStatusCode']);
  605. printf("RequestId:%s\n", $resp ['RequestId']);
  606. printf("RedirectAllRequestsTo[HostName]:%s,RedirectAllRequestsTo[Protocol]:%s\n", $resp ['RedirectAllRequestsTo'] ['HostName'], $resp ['RedirectAllRequestsTo'] ['Protocol']);
  607. printf("IndexDocument[Suffix]:%s\n", $resp ['IndexDocument'] ['Suffix']);
  608. printf("ErrorDocument[Key]:%s\n", $resp ['ErrorDocument'] ['Key']);
  609. $i = 0;
  610. foreach ( $resp ['RoutingRules'] as $rout ) {
  611. printf("RoutingRules[$i][Condition][HttpErrorCodeReturnedEquals]:%s,RoutingRules[$i][Condition][KeyPrefixEquals]:%s\n", $rout ['Condition'] ['HttpErrorCodeReturnedEquals'], $rout ['Condition'] ['KeyPrefixEquals']);
  612. printf("RoutingRules[$i][Redirect][Protocol]:%s,RoutingRules[$i][Redirect][HostName]:%s,RoutingRules[$i][Redirect][ReplaceKeyPrefixWith]:%s,RoutingRules[$i][Redirect][ReplaceKeyWith]:%s,RoutingRules[$i][Redirect][HttpRedirectCode]:%s\n", $rout ['Redirect'] ['Protocol'], $rout ['Redirect'] ['HostName'], $rout ['Redirect'] ['ReplaceKeyPrefixWith'], $rout ['Redirect'] ['ReplaceKeyWith'], $rout ['Redirect'] ['HttpRedirectCode']);
  613. $i ++;
  614. }
  615. } catch ( ObsException $e ) {
  616. echo $e;
  617. }
  618. }
  619. // delete bucket website configuration
  620. function DeleteBucketWebsite() {
  621. global $obsClient;
  622. global $bucketName;
  623. echo "delete bucket website configuration start...\n";
  624. try {
  625. $resp = $obsClient->deleteBucketWebsite(array (
  626. 'Bucket' => $bucketName
  627. ));
  628. printf("HttpStatusCode:%s\n", $resp ['HttpStatusCode']);
  629. printf("RequestId:%s\n", $resp ['RequestId']);
  630. } catch ( ObsException $e ) {
  631. echo $e;
  632. }
  633. }
  634. // set bucket versioning configuration
  635. function SetBucketVersioning() {
  636. global $obsClient;
  637. global $bucketName;
  638. echo "set bucket versioning configuration start...\n";
  639. try {
  640. $resp = $obsClient->setBucketVersioning(array (
  641. 'Bucket' => $bucketName,
  642. 'Status' => 'Suspended'
  643. ));
  644. printf("HttpStatusCode:%s\n", $resp ['HttpStatusCode']);
  645. printf("RequestId:%s\n", $resp ['RequestId']);
  646. } catch ( ObsException $e ) {
  647. echo $e;
  648. }
  649. }
  650. // get bucket versioning configuration
  651. function GetBucketVersioning() {
  652. global $obsClient;
  653. global $bucketName;
  654. echo "get bucket versioning configuration start...\n";
  655. try {
  656. $resp = $obsClient->getBucketVersioning(array (
  657. 'Bucket' => $bucketName
  658. ));
  659. printf("HttpStatusCode:%s\n", $resp ['HttpStatusCode']);
  660. printf("Status:%s\n", $resp ['Status']);
  661. printf("RequestId:%s\n", $resp ['RequestId']);
  662. } catch ( ObsException $e ) {
  663. echo $e;
  664. }
  665. }
  666. // set bucket cors
  667. function SetBucketCors() {
  668. global $obsClient;
  669. global $bucketName;
  670. echo "set bucket cors start...\n";
  671. try {
  672. $resp = $obsClient->setBucketCors(array (
  673. 'Bucket' => $bucketName,
  674. 'CorsRules' => array (
  675. 0 => array (
  676. 'ID' => '123456',
  677. 'AllowedMethod' => array (
  678. 0 => "PUT",
  679. 1 => "POST",
  680. 2 => "GET",
  681. 3 => "DELETE"
  682. ),
  683. 'AllowedOrigin' => array (
  684. 0 => "obs.hostname1"
  685. ),
  686. 'AllowedHeader' => array (
  687. 0 => "header-1",
  688. 1 => "header-2"
  689. )
  690. )
  691. )
  692. ));
  693. printf("HttpStatusCode:%s\n", $resp ['HttpStatusCode']);
  694. printf("RequestId:%s\n", $resp ['RequestId']);
  695. } catch ( ObsException $e ) {
  696. echo $e;
  697. }
  698. }
  699. // delete bucket cors
  700. function DeleteBucketCors() {
  701. global $obsClient;
  702. global $bucketName;
  703. echo "delete bucket cors start...\n";
  704. try {
  705. $resp = $obsClient->deleteBucketCors(array (
  706. 'Bucket' => $bucketName
  707. ));
  708. printf("HttpStatusCode:%s\n", $resp ['HttpStatusCode']);
  709. printf("RequestId:%s\n", $resp ['RequestId']);
  710. } catch ( ObsException $e ) {
  711. echo $e;
  712. }
  713. }
  714. // get bucket cors
  715. function GetBucketCors() {
  716. global $obsClient;
  717. global $bucketName;
  718. echo "get bucket cors start...\n";
  719. try {
  720. $resp = $obsClient->getBucketCors(array (
  721. 'Bucket' => $bucketName
  722. ));
  723. printf("HttpStatusCode:%s\n", $resp ['HttpStatusCode']);
  724. printf("RequestId:%s\n", $resp ['RequestId']);
  725. print_r($resp ['CorsRules']);
  726. } catch ( ObsException $e ) {
  727. echo $e;
  728. }
  729. }
  730. // options bucket
  731. function OptionsBucket() {
  732. global $obsClient;
  733. global $bucketName;
  734. echo "options bucket start...\n";
  735. try {
  736. $resp = $obsClient->optionsBucket(array (
  737. 'Bucket' => $bucketName,
  738. 'Origin' => 'obs.hostname1',
  739. 'AccessControlRequestMethods' => array (
  740. 0 => "PUT",
  741. 1 => "POST"
  742. ),
  743. 'AccessControlRequestHeaders' => array (
  744. 0 => "header-1",
  745. 1 => "header-2"
  746. )
  747. ));
  748. printf("HttpStatusCode:%s\n", $resp ['HttpStatusCode']);
  749. printf("RequestId:%s\n", $resp ['RequestId']);
  750. print_r($resp);
  751. } catch ( ObsException $e ) {
  752. echo $e;
  753. }
  754. }
  755. // set bucket tagging
  756. function SetBucketTagging() {
  757. global $obsClient;
  758. global $bucketName;
  759. echo "set bucket tagging start...\n";
  760. try {
  761. $resp = $obsClient->setBucketTagging(array (
  762. 'Bucket' => $bucketName,
  763. 'Tags' => array (
  764. 0 => array (
  765. 'Key' => 'testKey1',
  766. 'Value' => 'testValue1'
  767. ),
  768. 1 => array (
  769. 'Key' => 'testKey2',
  770. 'Value' => 'testValue2'
  771. )
  772. )
  773. ));
  774. printf("HttpStatusCode:%s\n", $resp ['HttpStatusCode']);
  775. printf("RequestId:%s\n", $resp ["RequestId"]);
  776. } catch ( ObsException $e ) {
  777. echo $e;
  778. }
  779. }
  780. // get bucket tagging
  781. function GetBucketTagging() {
  782. global $obsClient;
  783. global $bucketName;
  784. echo "get bucket tagging start...\n";
  785. try {
  786. $resp = $obsClient->getBucketTagging(array (
  787. 'Bucket' => $bucketName
  788. ));
  789. printf("HttpStatusCode:%s\n", $resp ['HttpStatusCode']);
  790. printf("RequestId:%s\n", $resp ["RequestId"]);
  791. foreach ( $resp ["Tags"] as $tag ) {
  792. printf("Tag[%s:%s]\n", $tag ["Key"], $tag ["Value"]);
  793. }
  794. } catch ( ObsException $e ) {
  795. echo $e;
  796. }
  797. }
  798. // delete bucket tagging
  799. function DeleteBucketTagging() {
  800. global $obsClient;
  801. global $bucketName;
  802. echo "delete bucket tagging start...\n";
  803. try {
  804. $resp = $obsClient->deleteBucketTagging(array (
  805. 'Bucket' => $bucketName
  806. ));
  807. printf("HttpStatusCode:%s\n", $resp ['HttpStatusCode']);
  808. printf("RequestId:%s\n", $resp ["RequestId"]);
  809. } catch ( ObsException $e ) {
  810. echo $e;
  811. }
  812. }
  813. // set bucket notification
  814. function SetBucketNotification() {
  815. global $obsClient;
  816. global $bucketName;
  817. echo "set bucket notification start...\n";
  818. try {
  819. $resp = $obsClient->setBucketNotification(array (
  820. 'Bucket' => $bucketName,
  821. 'TopicConfigurations' => array (
  822. 0 => array (
  823. 'ID' => '001',
  824. 'Topic' => 'your topic',
  825. 'Event' => array (
  826. 0 => 'ObjectCreated:*'
  827. ),
  828. 'Filter' => array (
  829. 0 => array (
  830. 'Name' => 'prefix',
  831. 'Value' => 'smn/'
  832. ),
  833. 1 => array (
  834. 'Name' => 'suffix',
  835. 'Value' => '.jpg'
  836. )
  837. )
  838. )
  839. )
  840. ));
  841. printf("HttpStatusCode:%s\n", $resp ['HttpStatusCode']);
  842. printf("RequestId:%s\n", $resp ["RequestId"]);
  843. } catch ( ObsException $e ) {
  844. echo $e;
  845. }
  846. }
  847. // get bucket notification
  848. function GetBucketNotification() {
  849. global $obsClient;
  850. global $bucketName;
  851. echo "get bucket notification start...\n";
  852. try {
  853. $resp = $obsClient->getBucketNotification(array (
  854. 'Bucket' => $bucketName
  855. ));
  856. printf("HttpStatusCode:%s\n", $resp ['HttpStatusCode']);
  857. printf("RequestId:%s\n", $resp ["RequestId"]);
  858. print_r($resp ['TopicConfigurations']);
  859. } catch ( ObsException $e ) {
  860. echo $e;
  861. }
  862. }
  863. // delete object
  864. function DeleteObject() {
  865. global $obsClient;
  866. global $bucketName;
  867. global $objectKey;
  868. echo "delete object start...\n";
  869. try {
  870. $resp = $obsClient->deleteObject(array (
  871. 'Bucket' => $bucketName,
  872. 'Key' => $objectKey,
  873. 'VersionId' => ''
  874. ));
  875. printf("HttpStatusCode:%s\n", $resp ['HttpStatusCode']);
  876. printf("RequestId:%s\n", $resp ['RequestId']);
  877. printf("DeleteMarker:%s,VersionId:%s\n", $resp ['DeleteMarker'], $resp ['VersionId']);
  878. } catch ( ObsException $e ) {
  879. echo $e;
  880. }
  881. }
  882. // options object
  883. function OptionsObject() {
  884. global $obsClient;
  885. global $bucketName;
  886. global $objectKey;
  887. echo "options bucket start...\n";
  888. try {
  889. $resp = $obsClient->optionsObject(array (
  890. 'Bucket' => $bucketName,
  891. 'Key' => $objectKey,
  892. 'Origin' => 'obs.hostname1',
  893. 'AccessControlRequestMethods' => array (
  894. "PUT",
  895. "GET"
  896. ),
  897. 'AccessControlRequestHeaders' => array (
  898. 0 => "header-1",
  899. 1 => "header-2"
  900. )
  901. ));
  902. printf("HttpStatusCode:%s\n", $resp ['HttpStatusCode']);
  903. printf("RequestId:%s\n", $resp ['RequestId']);
  904. print_r($resp);
  905. } catch ( ObsException $e ) {
  906. echo $e;
  907. }
  908. }
  909. // delete objects
  910. function DeleteObjects() {
  911. global $obsClient;
  912. global $bucketName;
  913. echo "delete objects start...\n";
  914. try {
  915. $resp = $obsClient->deleteObjects(array (
  916. 'Bucket' => $bucketName,
  917. 'Objects' => array (
  918. 0 => array (
  919. 'Key' => 'test'
  920. ),
  921. 1 => array (
  922. 'Key' => 'file.log'
  923. )
  924. ),
  925. 'Quiet' => false
  926. ));
  927. printf("HttpStatusCode:%s\n", $resp ['HttpStatusCode']);
  928. printf("RequestId:%s\n", $resp ['RequestId']);
  929. $i = 0;
  930. foreach ( $resp ['Errors'] as $error ) {
  931. printf("Errors[$i][Key]:%s,Errors[$i][VersionId]:%s锛孍rrors[$i][Code]:%s锛孍rrors[$i][Message]:%s\n", $error ['Key'], $error ['VersionId'], $error ['Code'], $error ['Message']);
  932. $i ++;
  933. }
  934. $i = 0;
  935. foreach ( $resp ['Deleteds'] as $delete ) {
  936. printf("Deleteds[$i][Key]:%s,Deleted[$i][VersionId]:%s锛孌eleted[$i][DeleteMarker]:%s锛孌eleted[$i][DeleteMarkerVersionId]:%s\n", $delete ['Key'], $delete ['VersionId'], $delete ['DeleteMarker'], $delete ['DeleteMarkerVersionId']);
  937. $i ++;
  938. }
  939. } catch ( ObsException $e ) {
  940. echo $e;
  941. }
  942. }
  943. // set object acl
  944. function SetObjectAcl() {
  945. global $obsClient;
  946. global $bucketName;
  947. global $objectKey;
  948. echo "set object ACL start...\n";
  949. try {
  950. $resp = $obsClient->setObjectAcl(array (
  951. 'Bucket' => $bucketName,
  952. 'Key' => $objectKey,
  953. 'Delivered' => true,
  954. 'Grants' => array (
  955. 0 => array (
  956. 'Grantee' => array (
  957. 'ID' => 'userid',
  958. ),
  959. 'Permission' => ObsClient::PermissionWrite
  960. ),
  961. 1 => array (
  962. 'Grantee' => array (
  963. 'URI' => ObsClient::AllUsers
  964. ),
  965. 'Permission' => ObsClient::PermissionRead
  966. )
  967. ),
  968. 'Owner' => array (
  969. 'ID' => 'ownerid'
  970. )
  971. ));
  972. printf("HttpStatusCode:%s\n", $resp ['HttpStatusCode']);
  973. printf("RequestId:%s\n", $resp ['RequestId']);
  974. } catch ( ObsException $e ) {
  975. echo $e;
  976. }
  977. }
  978. // get object acl
  979. function GetObjectAcl() {
  980. global $obsClient;
  981. global $bucketName;
  982. global $objectKey;
  983. echo "get bucket ACL start...\n";
  984. try {
  985. $resp = $obsClient->getObjectAcl(array (
  986. 'Bucket' => $bucketName,
  987. 'Key' => $objectKey
  988. ));
  989. printf("HttpStatusCode:%s\n", $resp ['HttpStatusCode']);
  990. printf("RequestId:%s\n", $resp ['RequestId']);
  991. printf("Owner[ID]:%s\n", $resp ['Owner'] ['ID']);
  992. printf("Delivered:%s\n", $resp['Delivered'] ? 'true' : 'false');
  993. $i = 0;
  994. foreach ( $resp ['Grants'] as $grant ) {
  995. printf("Grants[$i][Grantee][ID]:%s,Grants[$i][Grantee][URI]:%s\n", $grant ['Grantee'] ['ID'], $grant ['Grantee'] ['URI']);
  996. printf("Grants[$i][Permission]:%s\n", $grant ['Permission']);
  997. $i ++;
  998. }
  999. } catch ( ObsException $e ) {
  1000. echo $e;
  1001. }
  1002. }
  1003. // restore object
  1004. function RestoreObject() {
  1005. global $obsClient;
  1006. global $bucketName;
  1007. global $objectKey;
  1008. echo "restore object start...\n";
  1009. try {
  1010. $resp = $obsClient->restoreObject(array (
  1011. "Bucket" => $bucketName,
  1012. "Key" => $objectKey,
  1013. "VersionId" => NULL,
  1014. "Days" => 1,
  1015. "Tier" => ObsClient::RestoreTierExpedited
  1016. ));
  1017. printf("HttpStatusCode:%s\n", $resp ['HttpStatusCode']);
  1018. printf("RequestId:%s\n", $resp ["RequestId"]);
  1019. } catch ( ObsException $e ) {
  1020. echo $e;
  1021. }
  1022. }
  1023. // put object
  1024. function PutObject() {
  1025. global $obsClient;
  1026. global $bucketName;
  1027. global $objectKey;
  1028. echo "put object start...\n";
  1029. try {
  1030. $resp = $obsClient->putObject(array (
  1031. 'Bucket' => $bucketName,
  1032. 'Key' => $objectKey,
  1033. 'Metadata' => array (
  1034. 'test' => "value"
  1035. ),
  1036. // 'Body'=>'msg to put',
  1037. 'ContentType' => 'text/plain',
  1038. 'SourceFile' => '/temp/test.txt'
  1039. ));
  1040. printf("HttpStatusCode:%s\n", $resp ['HttpStatusCode']);
  1041. printf("RequestId:%s\n", $resp ['RequestId']);
  1042. printf("ETag:%s,VersionId:%s\n", $resp ['ETag'], $resp ['VersionId']);
  1043. } catch ( ObsException $e ) {
  1044. echo $e;
  1045. }
  1046. }
  1047. // get object metadata
  1048. function GetObjectMetadata() {
  1049. global $obsClient;
  1050. global $bucketName;
  1051. global $objectKey;
  1052. echo "get object metadata start...\n";
  1053. try {
  1054. $resp = $obsClient->getObjectMetadata(array (
  1055. 'Bucket' => $bucketName,
  1056. 'Key' => $objectKey
  1057. ));
  1058. printf("HttpStatusCode:%s\n", $resp ['HttpStatusCode']);
  1059. printf("RequestId:%s\n", $resp ['RequestId']);
  1060. printf("Expiration:%s,LastModified:%s,ContentLength:%d,StorageClass:%s\n", $resp ['Expiration'], $resp ['LastModified'], $resp ['ContentLength'], $resp ['StorageClass']);
  1061. printf("ETag:%s,VersionId:%s,WebsiteRedirectLocation:%s\n", $resp ['ETag'], $resp ['VersionId'], $resp ['WebsiteRedirectLocation']);
  1062. } catch ( ObsException $e ) {
  1063. echo $e;
  1064. }
  1065. }
  1066. // get object
  1067. function GetObject() {
  1068. global $obsClient;
  1069. global $bucketName;
  1070. global $objectKey;
  1071. echo "get object start...\n";
  1072. try {
  1073. $resp = $obsClient->getObject(array (
  1074. 'Bucket' => $bucketName,
  1075. 'Key' => $objectKey,
  1076. 'Range' => 'bytes=0-10',
  1077. 'SaveAsFile' => '/temp/test.txt'
  1078. ));
  1079. printf("HttpStatusCode:%s\n", $resp ['HttpStatusCode']);
  1080. printf("RequestId:%s\n", $resp ['RequestId']);
  1081. printf("Metadata:%s\n", json_encode($resp ['Metadata']));
  1082. printf("DeleteMarker:%s,Expiration:%s,LastModified:%s\n", $resp ['DeleteMarker'], $resp ['Expiration'], $resp ['LastModified']);
  1083. printf("ContentLength:%d,ETag:%s,VersionId:%s,SaveAsFile:%s\n", $resp ['ContentLength'], $resp ['ETag'], $resp ['VersionId'], $resp ['SaveAsFile']);
  1084. printf("Expires:%s,WebsiteRedirectLocation:%s\n", $resp ['Expires'], $resp ['WebsiteRedirectLocation']);
  1085. } catch ( ObsException $e ) {
  1086. echo $e;
  1087. }
  1088. }
  1089. // copy object
  1090. function CopyObject() {
  1091. global $obsClient;
  1092. global $bucketName;
  1093. global $objectKey;
  1094. echo "copy object start...\n";
  1095. try {
  1096. $resp = $obsClient->copyObject(array (
  1097. 'Bucket' => $bucketName,
  1098. 'Key' => $objectKey,
  1099. 'CopySource' => 'bucket003/test',
  1100. 'Metadata' => array (
  1101. 'test' => "value"
  1102. )
  1103. ));
  1104. printf("HttpStatusCode:%s\n", $resp ['HttpStatusCode']);
  1105. printf("RequestId:%s\n", $resp ['RequestId']);
  1106. printf("ETag:%s,VersionId:%s,LastModified:%s,CopySourceVersionId:%s\n", $resp ['ETag'], $resp ['VersionId'], $resp ['LastModified'], $resp ['CopySourceVersionId']);
  1107. } catch ( ObsException $e ) {
  1108. echo $e;
  1109. }
  1110. }
  1111. // initiate multipart upload
  1112. function InitiateMultipartUpload() {
  1113. global $obsClient;
  1114. global $bucketName;
  1115. global $objectKey;
  1116. echo "initiate mutipart upload start...\n";
  1117. try {
  1118. $resp = $obsClient->initiateMultipartUpload(array (
  1119. 'Bucket' => $bucketName,
  1120. 'Key' => $objectKey
  1121. ));
  1122. printf("HttpStatusCode:%s\n", $resp ['HttpStatusCode']);
  1123. printf("RequestId:%s\n", $resp ['RequestId']);
  1124. printf("Bucket:%s,Key:%s,UploadId:%s\n", $resp ['Bucket'], $resp ['Key'], $resp ['UploadId']);
  1125. } catch ( ObsException $e ) {
  1126. echo $e;
  1127. }
  1128. }
  1129. // abort multipart upload
  1130. function AbortMultipartUpload() {
  1131. global $obsClient;
  1132. global $bucketName;
  1133. global $objectKey;
  1134. echo "abort mutipart upload start...\n";
  1135. try {
  1136. $resp = $obsClient->abortMultipartUpload(array (
  1137. 'Bucket' => $bucketName,
  1138. 'Key' => $objectKey,
  1139. 'UploadId' => 'uploadid'
  1140. ));
  1141. printf("HttpStatusCode:%s\n", $resp ['HttpStatusCode']);
  1142. printf("RequestId:%s\n", $resp ['RequestId']);
  1143. } catch ( ObsException $e ) {
  1144. echo $e;
  1145. }
  1146. }
  1147. // list multipart uploads
  1148. function ListMultipartUploads() {
  1149. global $obsClient;
  1150. global $bucketName;
  1151. echo "list mutipart upload start...\n";
  1152. try {
  1153. $resp = $obsClient->listMultipartUploads(array (
  1154. 'Bucket' => $bucketName,
  1155. 'MaxUploads' => 1000
  1156. ));
  1157. printf("RequestId:%s\n", $resp ['RequestId']);
  1158. printf("Bucket锛�%s,KeyMarker:%s,UploadIdMarker:%s,NextKeyMarker:%s\n", $resp ['Bucket'], $resp ['KeyMarker'], $resp ['UploadIdMarker'], $resp ['NextKeyMarker']);
  1159. printf("Prefix:%s,Delimiter:%s,NextUploadIdMarker:%s,MaxUploads:%d,IsTruncated:%s\n", $resp ['Prefix'], $resp ['Delimiter'], $resp ['NextUploadIdMarker'], $resp ['MaxUploads'], $resp ['IsTruncated']);
  1160. $i = 0;
  1161. foreach ( $resp ['CommonPrefixes'] as $common ) {
  1162. printf("CommonPrefixes[$i][Prefix]:%s\n", $common ['Prefix']);
  1163. $i ++;
  1164. }
  1165. $i = 0;
  1166. foreach ( $resp ['Uploads'] as $upload ) {
  1167. printf("Uploads[$i][Key]:%s,Uploads[$i][UploadId]:%s,Uploads[$i][StorageClass]:%s,Uploads[$i][Initiated]:%s\n", $upload ['Key'], $upload ['UploadId'], $upload ['StorageClass'], $upload ['Initiated']);
  1168. printf("Uploads[$i][Initiator][ID]:%s,Uploads[$i][Initiator][DisplayName]:%s\n", $upload ['Initiator'] ['ID'], $upload ['Initiator'] ['DisplayName']);
  1169. printf("Uploads[$i][Owner][ID]:%s,Uploads[$i][Owner][DisplayName]:%s\n", $upload ['Owner'] ['ID'], $upload ['Owner'] ['DisplayName']);
  1170. $i ++;
  1171. }
  1172. } catch ( ObsException $e ) {
  1173. echo $e;
  1174. }
  1175. }
  1176. // upload part
  1177. function UploadPart() {
  1178. global $obsClient;
  1179. global $bucketName;
  1180. global $objectKey;
  1181. echo "upload part start...\n";
  1182. try {
  1183. $resp = $obsClient->uploadPart(array (
  1184. 'Bucket' => $bucketName,
  1185. 'Key' => $objectKey,
  1186. 'UploadId' => 'uploadid',
  1187. 'PartNumber' => 1,
  1188. // 'Body' => 'test',
  1189. 'SourceFile' => '/temp/test.txt'
  1190. ));
  1191. printf("HttpStatusCode:%s\n", $resp ['HttpStatusCode']);
  1192. printf("RequestId:%s\n", $resp ['RequestId']);
  1193. printf("ETag:%s\n", $resp ['ETag']);
  1194. } catch ( ObsException $e ) {
  1195. echo $e;
  1196. }
  1197. }
  1198. // copry part
  1199. function CopyPart() {
  1200. global $obsClient;
  1201. global $bucketName;
  1202. global $objectKey;
  1203. echo "copy part start...\n";
  1204. try {
  1205. $resp = $obsClient->copyPart(array (
  1206. 'Bucket' => $bucketName,
  1207. 'Key' => $objectKey,
  1208. 'UploadId' => 'uploadid',
  1209. 'PartNumber' => 1,
  1210. 'CopySource' => 'bucket003/test'
  1211. ));
  1212. printf("HttpStatusCode:%s\n", $resp ['HttpStatusCode']);
  1213. printf("RequestId:%s\n", $resp ['RequestId']);
  1214. printf("ETag:%s,LastModified:%s\n", $resp ['ETag'], $resp ['LastModified']);
  1215. } catch ( ObsException $e ) {
  1216. echo $e;
  1217. }
  1218. }
  1219. // list parts
  1220. function ListParts() {
  1221. global $obsClient;
  1222. global $bucketName;
  1223. global $objectKey;
  1224. echo "list parts start...\n";
  1225. try {
  1226. $resp = $obsClient->listParts(array (
  1227. 'Bucket' => $bucketName,
  1228. 'Key' => $objectKey,
  1229. 'UploadId' => 'uploadid',
  1230. 'MaxParts' => 500,
  1231. 'PartNumberMarker' => 0
  1232. ));
  1233. printf("HttpStatusCode:%s\n", $resp ['HttpStatusCode']);
  1234. printf("RequestId:%s\n", $resp ['RequestId']);
  1235. printf("Bucket锛�%s,Key:%s,UploadId:%s,PartNumberMarker:%d\n", $resp ['Bucket'], $resp ['Key'], $resp ['UploadId'], $resp ['PartNumberMarker']);
  1236. printf("NextPartNumberMarker:%d,MaxParts:%d,IsTruncated:%d,StorageClass:%s\n", $resp ['NextPartNumberMarker'], $resp ['MaxParts'], $resp ['IsTruncated'], $resp ['StorageClass']);
  1237. printf("Initiator[ID]:%s,Initiator[DisplayName]:%s\n", $resp ['Initiator'] ['ID'], $resp ['Initiator'] ['DisplayName']);
  1238. printf("Owner[ID]:%s,Owner[DisplayName]:%s\n", $resp ['Owner'] ['ID'], $resp ['Owner'] ['DisplayName']);
  1239. $i = 0;
  1240. foreach ( $resp ['Parts'] as $part ) {
  1241. printf("Parts[$i][PartNumber]:%s,Parts[$i][LastModified]:%s,Parts[$i][ETag]:%s,Parts[$i][Size]:%d\n", $part ['PartNumber'], $part ['LastModified'], $part ['ETag'], $part ['Size']);
  1242. $i ++;
  1243. }
  1244. } catch ( ObsException $e ) {
  1245. echo $e;
  1246. }
  1247. }
  1248. // merge parts
  1249. function CompleteMultipartUpload() {
  1250. global $obsClient;
  1251. global $bucketName;
  1252. global $objectKey;
  1253. echo "complete multipart upload start...\n";
  1254. try {
  1255. $resp = $obsClient->completeMultipartUpload(array (
  1256. 'Bucket' => $bucketName,
  1257. 'Key' => $objectKey,
  1258. 'UploadId' => 'uploadid',
  1259. 'Parts' => array (
  1260. 0 => array (
  1261. 'PartNumber' => 1,
  1262. 'ETag' => 'etagvalue'
  1263. )
  1264. )
  1265. ));
  1266. printf("HttpStatusCode:%s\n", $resp ['HttpStatusCode']);
  1267. printf("RequestId:%s\n", $resp ['RequestId']);
  1268. printf("Bucket:%s,Key:%s,ETag:%s,VersionId:%s,Location:%s\n", $resp ['Bucket'], $resp ['Key'], $resp ['ETag'], $resp ['VersionId'], $resp ['Location']);
  1269. } catch ( ObsException $e ) {
  1270. echo $e;
  1271. }
  1272. }
  1273. //----bucket related apis---
  1274. // CreateBucket();
  1275. // ListBuckets();
  1276. // DeleteBucket();
  1277. // ListObjects();
  1278. // ListVersions();
  1279. // HeadBucket();
  1280. // GetBucketMetadata();
  1281. // GetBucketLocation();
  1282. // GetBucketStorageInfo();
  1283. // SetBucketQuota();
  1284. // GetBucketQuota();
  1285. // SetBucketStoragePolicy();
  1286. // GetBucketStoragePolicy();
  1287. // SetBucketAcl();
  1288. // GetBucketAcl();
  1289. // SetBucketLogging();
  1290. // GetBucketLogging();
  1291. // SetBucketPolicy();
  1292. // GetBucketPolicy();
  1293. // DeleteBucketPolicy();
  1294. // SetBucketLifecycle();
  1295. // GetBucketLifecycle();
  1296. // DeleteBucketLifecycle();
  1297. // SetBucketWebsite();
  1298. // GetBucketWebsite();
  1299. // DeleteBucketWebsite();
  1300. // SetBucketVersioning();
  1301. // GetBucketVersioning();
  1302. // SetBucketCors();
  1303. // GetBucketCors();
  1304. // DeleteBucketCors();
  1305. // OptionsBucket();
  1306. // SetBucketTagging();
  1307. // GetBucketTagging();
  1308. // DeleteBucketTagging();
  1309. // SetBucketNotification();
  1310. // GetBucketNotification();
  1311. //-----object related apis--------
  1312. // DeleteObject();
  1313. // OptionsObject();
  1314. // SetObjectAcl();
  1315. // GetObjectAcl();
  1316. // RestoreObject();
  1317. // DeleteObjects();
  1318. // PutObject();
  1319. // GetObject();
  1320. // CopyObject();
  1321. // GetObjectMetadata();
  1322. // InitiateMultipartUpload();
  1323. // ListMultipartUploads();
  1324. // AbortMultipartUpload();
  1325. // UploadPart();
  1326. // ListParts();
  1327. // CompleteMultipartUpload();
  1328. // CopyPart();