HuaweiObs.php 54 KB

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