sample.php 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824
  1. <?php
  2. require 'vendor/autoload.php';
  3. $cosClient = new Qcloud\Cos\Client(array(
  4. 'region' => 'COS_REGION', #地域,如ap-guangzhou,ap-beijing-1
  5. 'credentials' => array(
  6. 'secretId' => 'COS_KEY',
  7. 'secretKey' => 'COS_SECRET',
  8. ),
  9. ));
  10. // 若初始化 Client 时未填写 appId,则 bucket 的命名规则为{name}-{appid} ,此处填写的存储桶名称必须为此格式
  11. $bucket = 'test2-1252448703';
  12. $key = 'a.txt';
  13. $local_path = "E:/a.txt";
  14. # 上传文件
  15. ## putObject(上传接口,最大支持上传5G文件)
  16. ### 上传内存中的字符串
  17. try {
  18. $result = $cosClient->putObject(array(
  19. 'Bucket' => $bucket,
  20. 'Key' => $key,
  21. 'Body' => 'Hello World!'
  22. ));
  23. print_r($result);
  24. # 可以直接通过$result读出返回结果
  25. echo ($result['ETag']);
  26. } catch (\Exception $e) {
  27. echo($e);
  28. }
  29. ### 上传文件流
  30. try {
  31. $result = $cosClient->putObject(array(
  32. 'Bucket' => $bucket,
  33. 'Key' => $key,
  34. 'Body' => fopen($local_path, 'rb')
  35. ));
  36. print_r($result);
  37. } catch (\Exception $e) {
  38. echo($e);
  39. }
  40. ### 设置header和meta
  41. try {
  42. $result = $cosClient->putObject(array(
  43. 'Bucket' => $bucket,
  44. 'Key' => $key,
  45. 'Body' => fopen($local_path, 'rb'),
  46. 'ACL' => 'string',
  47. 'CacheControl' => 'string',
  48. 'ContentDisposition' => 'string',
  49. 'ContentEncoding' => 'string',
  50. 'ContentLanguage' => 'string',
  51. 'ContentLength' => integer,
  52. 'cONTENTType' => 'string',
  53. 'Expires' => 'mixed type: string (date format)|int (unix timestamp)|\DateTime',
  54. 'GrantFullControl' => 'string',
  55. 'GrantRead' => 'string',
  56. 'GrantWrite' => 'string',
  57. 'Metadata' => array(
  58. 'string' => 'string',
  59. ),
  60. 'StorageClass' => 'string'
  61. ));
  62. print_r($result);
  63. } catch (\Exception $e) {
  64. echo($e);
  65. }
  66. ## Upload(高级上传接口,默认使用分块上传最大支持50T)
  67. ### 上传内存中的字符串
  68. try {
  69. $result = $cosClient->upload(
  70. $bucket = $bucket,
  71. $key = $key,
  72. $body = 'Hello World!'
  73. );
  74. print_r($result);
  75. } catch (\Exception $e) {
  76. echo($e);
  77. }
  78. ### 上传文件流
  79. try {
  80. $result = $cosClient->upload(
  81. $bucket = $bucket,
  82. $key = $key,
  83. $body = fopen($local_path, 'rb')
  84. );
  85. print_r($result);
  86. } catch (\Exception $e) {
  87. echo($e);
  88. }
  89. ### 设置header和meta
  90. try {
  91. $result = $cosClient->upload(
  92. $bucket = $bucket,
  93. $key = $key,
  94. $body = fopen($local_path, 'rb'),
  95. $options = array(
  96. 'ACL' => 'string',
  97. 'CacheControl' => 'string',
  98. 'ContentDisposition' => 'string',
  99. 'ContentEncoding' => 'string',
  100. 'ContentLanguage' => 'string',
  101. 'ContentLength' => integer,
  102. 'ContentType' => 'string',
  103. 'Expires' => 'mixed type: string (date format)|int (unix timestamp)|\DateTime',
  104. 'GrantFullControl' => 'string',
  105. 'GrantRead' => 'string',
  106. 'GrantWrite' => 'string',
  107. 'Metadata' => array(
  108. 'string' => 'string',
  109. ),
  110. 'StorageClass' => 'string'
  111. )
  112. );
  113. print_r($result);
  114. } catch (\Exception $e) {
  115. echo($e);
  116. }
  117. ## 预签名上传createPresignedUrl
  118. ## 获取带有签名的url
  119. ### 简单上传预签名
  120. try {
  121. #此处可以替换为其他上传接口
  122. $command = $cosClient->getCommand('putObject', array(
  123. 'Bucket' => $bucket,
  124. 'Key' => $key,
  125. 'Body' => '', //Body可以任意
  126. ));
  127. $signedUrl = $command->createPresignedUrl('+10 minutes');
  128. echo ($signedUrl);
  129. } catch (\Exception $e) {
  130. echo($e);
  131. }
  132. ### 分块上传预签名
  133. try {
  134. #此处可以替换为其他上传接口
  135. $command = $cosClient->getCommand('uploadPart', array(
  136. 'Bucket' => $bucket,
  137. 'Key' => $key,
  138. 'UploadId' => '',
  139. 'PartNumber' => '1',
  140. 'Body' => '', //Body可以任意
  141. ));
  142. $signedUrl = $command->createPresignedUrl('+10 minutes');
  143. echo ($signedUrl);
  144. } catch (\Exception $e) {
  145. echo($e);
  146. }
  147. ### 获取签名
  148. try {
  149. #此处可以替换为其他上传接口
  150. $command = $cosClient->getCommand('putObject', array(
  151. 'Bucket' => $bucket,
  152. 'Key' => $key,
  153. 'Body' => '', //Body可以任意
  154. ));
  155. $signedUrl = $command->createAuthorization('+10 minutes');
  156. echo ($signedUrl);
  157. } catch (\Exception $e) {
  158. echo($e);
  159. }
  160. # 下载文件
  161. ## getObject(下载文件)
  162. ### 下载到内存
  163. try {
  164. $result = $cosClient->getObject(array(
  165. 'Bucket' => $bucket,
  166. 'Key' => $key
  167. ));
  168. echo $result['Body'];
  169. } catch (\Exception $e) {
  170. echo($e);
  171. }
  172. ### 下载到本地
  173. try {
  174. $result = $cosClient->getObject(array(
  175. 'Bucket' => $bucket,
  176. 'Key' => $key,
  177. 'SaveAs' => $local_path
  178. ));
  179. } catch (\Exception $e) {
  180. echo($e);
  181. }
  182. ### 指定下载范围
  183. /*
  184. * Range 字段格式为 'bytes=a-b'
  185. */
  186. try {
  187. $result = $cosClient->getObject(array(
  188. 'Bucket' => $bucket,
  189. 'Key' => $key,
  190. 'Range' => 'bytes=0-10',
  191. 'SaveAs' => $local_path
  192. ));
  193. } catch (\Exception $e) {
  194. echo($e);
  195. }
  196. ### 设置返回header
  197. try {
  198. $result = $cosClient->getObject(array(
  199. 'Bucket' => $bucket,
  200. 'Key' => $key,
  201. 'ResponseCacheControl' => 'string',
  202. 'ResponseContentDisposition' => 'string',
  203. 'ResponseContentEncoding' => 'string',
  204. 'ResponseContentLanguage' => 'string',
  205. 'ResponseContentType' => 'string',
  206. 'ResponseExpires' => 'mixed type: string (date format)|int (unix timestamp)|\DateTime',
  207. 'SaveAs' => $local_path
  208. ));
  209. } catch (\Exception $e) {
  210. echo($e);
  211. }
  212. ## getObjectUrl(获取文件UrL)
  213. try {
  214. $signedUrl = $cosClient->getObjectUrl($bucket, $key, '+10 minutes');
  215. echo $signedUrl;
  216. } catch (\Exception $e) {
  217. echo($e);
  218. }
  219. # 删除object
  220. ## deleteObject
  221. try {
  222. $result = $cosClient->deleteObject(array(
  223. 'Bucket' => $bucket,
  224. 'Key' => $key,
  225. 'VersionId' => 'string'
  226. ));
  227. print_r($result);
  228. } catch (\Exception $e) {
  229. echo($e);
  230. }
  231. # 删除多个object
  232. ## deleteObjects
  233. try {
  234. $result = $cosClient->deleteObjects(array(
  235. 'Bucket' => 'string',
  236. 'Objects' => array(
  237. array(
  238. 'Key' => $key,
  239. 'VersionId' => 'string',
  240. ),
  241. // ... repeated
  242. ),
  243. ));
  244. print_r($result);
  245. } catch (\Exception $e) {
  246. echo($e);
  247. }
  248. # 获取object信息
  249. ## headObject
  250. /*
  251. * 可代替isObjectExist接口,查询object是否存在
  252. */
  253. try {
  254. $result = $cosClient->headObject(array(
  255. 'Bucket' => $bucket,
  256. 'Key' => '11',
  257. 'VersionId' => '111',
  258. ));
  259. print_r($result);
  260. } catch (\Exception $e) {
  261. echo($e);
  262. }
  263. # 获取bucket列表
  264. ## listBuckets
  265. try {
  266. $result = $cosClient->listBuckets();
  267. print_r($result);
  268. } catch (\Exception $e) {
  269. echo($e);
  270. }
  271. # 创建bucket
  272. ## createBucket
  273. try {
  274. $result = $cosClient->createBucket(array('Bucket' => $bucket));
  275. print_r($result);
  276. } catch (\Exception $e) {
  277. echo($e);
  278. }
  279. # 删除bucket
  280. ## deleteBucket
  281. try {
  282. $result = $cosClient->deleteBucket(array(
  283. 'Bucket' => $bucket
  284. ));
  285. print_r($result);
  286. } catch (\Exception $e) {
  287. echo($e);
  288. }
  289. # 获取bucket信息
  290. ## headBucket
  291. /*
  292. * 可代替isBucketExist接口,查询bucket是否存在
  293. */
  294. try {
  295. $result = $cosClient->headBucket(array(
  296. 'Bucket' => $bucket
  297. ));
  298. print_r($result);
  299. } catch (\Exception $e) {
  300. echo($e);
  301. }
  302. # 列出bucket下的object
  303. ## listObjects
  304. ### 列出所有object
  305. /*
  306. * 该接口一次最多列出1000个,需要列出所有请参考其他服务中的清空并删除bucket接口
  307. */
  308. try {
  309. $result = $cosClient->listObjects(array(
  310. 'Bucket' => $bucket
  311. ));
  312. foreach ($result['Contents'] as $rt) {
  313. print_r($rt);
  314. }
  315. } catch (\Exception $e) {
  316. echo($e);
  317. }
  318. ### 列出带有前缀的object
  319. try {
  320. $result = $cosClient->listObjects(array(
  321. 'Bucket' => $bucket,
  322. 'Prefix' => 'string'
  323. ));
  324. foreach ($result['Contents'] as $rt) {
  325. print_r($rt);
  326. }
  327. } catch (\Exception $e) {
  328. echo($e);
  329. }
  330. # 获取bucket地域
  331. ## getBucketLocation
  332. try {
  333. $result = $cosClient->getBucketLocation(array(
  334. 'Bucket' => 'lewzylu02',
  335. ));
  336. } catch (\Exception $e) {
  337. echo($e);
  338. };
  339. # 多版本相关
  340. ## putBucketVersioning(开启关闭某个bucket的多版本)
  341. try {
  342. $result = $cosClient->putBucketVersioning(array(
  343. 'Bucket' => $bucket,
  344. 'Status' => 'Enabled'
  345. ));
  346. print_r($result);
  347. } catch (\Exception $e) {
  348. echo($e);
  349. }
  350. ## ListObjectVersions(列出多版本object)
  351. /*
  352. * 同名文件会出现多个版本
  353. */
  354. try {
  355. $result = $cosClient->listObjectVersions(array(
  356. 'Bucket' => $bucket,
  357. 'Prefix' => 'string'
  358. ));
  359. print_r($result);
  360. } catch (\Exception $e) {
  361. echo($e);
  362. }
  363. ## getBucketVersioning(获取某个bucket多版本属性)
  364. try {
  365. $result = $cosClient->getBucketVersioning(
  366. array('Bucket' => $bucket));
  367. print_r($result);
  368. } catch (\Exception $e) {
  369. echo($e);
  370. }
  371. # ACL相关
  372. ## PutBucketAcl(设置bucketACL)
  373. try {
  374. $result = $cosClient->putBucketAcl(array(
  375. 'Bucket' => $bucket,
  376. 'Grants' => array(
  377. array(
  378. 'Grantee' => array(
  379. 'DisplayName' => 'qcs::cam::uin/327874225:uin/327874225',
  380. 'ID' => 'qcs::cam::uin/327874225:uin/327874225',
  381. 'Type' => 'CanonicalUser',
  382. ),
  383. 'Permission' => 'FULL_CONTROL',
  384. ),
  385. // ... repeated
  386. ),
  387. 'Owner' => array(
  388. 'DisplayName' => 'qcs::cam::uin/3210232098:uin/3210232098',
  389. 'ID' => 'qcs::cam::uin/3210232098:uin/3210232098',
  390. )));
  391. print_r($result);
  392. } catch (\Exception $e) {
  393. echo($e);
  394. }
  395. ## getBucketAcl(获取bucketACL)
  396. try {
  397. $result = $cosClient->getBucketAcl(array(
  398. 'Bucket' => $bucket));
  399. print_r($result);
  400. } catch (\Exception $e) {
  401. echo($e);
  402. }
  403. ## putObjectAcl(设置objectACL)
  404. try {
  405. $result = $cosClient->putObjectAcl(array(
  406. 'Bucket' => $bucket,
  407. 'Key' => $key,
  408. 'Grants' => array(
  409. array(
  410. 'Grantee' => array(
  411. 'DisplayName' => 'qcs::cam::uin/327874225:uin/327874225',
  412. 'ID' => 'qcs::cam::uin/327874225:uin/327874225',
  413. 'Type' => 'CanonicalUser',
  414. ),
  415. 'Permission' => 'FULL_CONTROL',
  416. ),
  417. // ... repeated
  418. ),
  419. 'Owner' => array(
  420. 'DisplayName' => 'qcs::cam::uin/3210232098:uin/3210232098',
  421. 'ID' => 'qcs::cam::uin/3210232098:uin/3210232098',
  422. )));
  423. print_r($result);
  424. } catch (\Exception $e) {
  425. echo($e);
  426. }
  427. ## GetObjectAcl(获取objectACL)
  428. try {
  429. $result = $cosClient->getObjectAcl(array(
  430. 'Bucket' => $bucket,
  431. 'Key' => $key));
  432. print_r($result);
  433. } catch (\Exception $e) {
  434. echo($e);
  435. }
  436. # 生命周期相关
  437. ## putBucketLifecycle(设置bucket生命周期)
  438. try {
  439. $result = $cosClient->putBucketLifecycle(array(
  440. 'Bucket' => $bucket,
  441. 'Rules' => array(
  442. array(
  443. 'Expiration' => array(
  444. 'Days' => 1000,
  445. ),
  446. 'ID' => 'id1',
  447. 'Filter' => array(
  448. 'Prefix' => 'documents/',
  449. ),
  450. 'Status' => 'Enabled',
  451. 'Transitions' => array(
  452. array(
  453. 'Days' => 200,
  454. 'StorageClass' => 'NEARLINE'),
  455. ),
  456. ),
  457. )));
  458. print_r($result);
  459. } catch (\Exception $e) {
  460. echo($e);
  461. }
  462. ## getBucketLifecycle(获取bucket生命周期)
  463. try {
  464. $result = $cosClient->getBucketLifecycle(array(
  465. 'Bucket' => $bucket,
  466. ));
  467. print_r($result);
  468. } catch (\Exception $e) {
  469. echo($e);
  470. }
  471. ## deleteBucketLifecycle(删除bucket生命周期)
  472. try {
  473. $result = $cosClient->deleteBucketLifecycle(array(
  474. 'Bucket' => $bucket,
  475. ));
  476. print_r($result);
  477. } catch (\Exception $e) {
  478. echo($e);
  479. }
  480. # 跨域相关
  481. ## putBucketCors(设置bucket跨域)
  482. try {
  483. $result = $cosClient->putBucketCors(array(
  484. 'Bucket' => $bucket,
  485. 'CORSRules' => array(
  486. array(
  487. 'ID' => '1234',
  488. 'AllowedHeaders' => array('*'),
  489. 'AllowedMethods' => array('PUT'),
  490. 'AllowedOrigins' => array('http://www.qq.com'),
  491. ),
  492. ),
  493. ));
  494. print_r($result);
  495. } catch (\Exception $e) {
  496. echo($e);
  497. }
  498. ## getBucketCors(获取bucket跨域信息)
  499. try {
  500. $result = $cosClient->getBucketCors(array());
  501. print_r($result);
  502. } catch (\Exception $e) {
  503. echo($e);
  504. }
  505. ## deleteBucketCors(删除bucket跨域)
  506. try {
  507. $result = $cosClient->deleteBucketCors(array(
  508. // Bucket is required
  509. 'Bucket' => $bucket,
  510. ));
  511. print_r($result);
  512. } catch (\Exception $e) {
  513. echo($e);
  514. }
  515. # 跨区域复制相关
  516. ## PutBucketReplication(设置bucket跨区域复制)
  517. ### 注意:目标bucket和源bucket都需要开启多版本
  518. try {
  519. $result = $cosClient->putBucketReplication(array(
  520. 'Bucket' => $bucket,
  521. 'Role' => 'qcs::cam::uin/327874225:uin/327874225',
  522. 'Rules'=>array(
  523. array(
  524. 'Status' => 'Enabled',
  525. 'ID' => 'string',
  526. 'Prefix' => 'string',
  527. 'Destination' => array(
  528. 'Bucket' => 'qcs::cos:ap-guangzhou::lewzylu01-1252448703',
  529. 'StorageClass' => 'standard',
  530. ),
  531. // ...repeated
  532. ),
  533. ),
  534. ));
  535. print_r($result);
  536. } catch (\Exception $e) {
  537. echo($e);
  538. }
  539. ## GetBucketReplication(获取bucket跨区域复制信息)
  540. try {
  541. $result = $cosClient->getBucketReplication(array(
  542. 'Bucket' => $bucket
  543. ));
  544. print_r($result);
  545. } catch (\Exception $e) {
  546. echo($e);
  547. }
  548. ## DeleteBucketReplication(删除bucket跨区域复制信息)
  549. try {
  550. $result = $cosClient->deleteBucketReplication(array(
  551. 'Bucket' => $bucket
  552. ));
  553. print_r($result);
  554. } catch (\Exception $e) {
  555. echo($e);
  556. }
  557. # 回调相关
  558. ## PutBucketNotification
  559. try {
  560. $result = $cosClient->putBucketNotification(array(
  561. "Bucket" => $bucket,
  562. "CloudFunctionConfigurations"=> array(
  563. array(
  564. "Id" => "test-1",
  565. "Filter" => array(
  566. "Key" => array(
  567. "FilterRules" => array(
  568. array(
  569. "Name" => "Prefix",
  570. "Value" => "111"
  571. ),
  572. array(
  573. "Name" => "Suffix",
  574. "Value" => "111"
  575. ),
  576. ),
  577. )
  578. ),
  579. "CloudFunction" => "qcs:0:video:sh:appid/1253125191:video/10010",
  580. "Events" => array(
  581. 'Event' => "cos:ObjectCreated:*"
  582. )
  583. ),
  584. array(
  585. "Id" => "test-2",
  586. "Filter" => array(
  587. "Key" => array(
  588. "FilterRules" => array(
  589. array(
  590. "Name" => "Prefix",
  591. "Value" => "111"
  592. ),
  593. array(
  594. "Name" => "Suffix",
  595. "Value" => "111"
  596. ),
  597. ),
  598. )
  599. ),
  600. "CloudFunction" => "qcs:0:video:sh:appid/1253125191:video/10010",
  601. "Events" => array(
  602. 'Event' => "cos:ObjectRemove:*"
  603. )
  604. ),
  605. ))
  606. );
  607. print_r($result);
  608. } catch (\Exception $e) {
  609. echo($e);
  610. }
  611. ## GetBucketNotification
  612. try {
  613. $result = $cosClient->getBucketNotification(array(
  614. 'Bucket' => $bucket
  615. ));
  616. print_r($result);
  617. } catch (\Exception $e) {
  618. echo($e);
  619. }
  620. # 复制
  621. ## copyobject(简单复制)
  622. /*
  623. * 将{bucket},{region},{cos_path},{versionId}替换成复制源的真实信息
  624. */
  625. try {
  626. $result = $cosClient->copyObject(array(
  627. 'Bucket' => $bucket,
  628. 'CopySource' => '{bucket}.cos.{region}.myqcloud.com/{cos_path}?versionId={versionId}',
  629. 'Key' => 'string',
  630. ));
  631. print_r($result);
  632. } catch (\Exception $e) {
  633. echo($e);
  634. }
  635. ## Copy(分块并发复制)
  636. /*
  637. * 将{bucket},{region},{cos_path},{versionId}替换成复制源的真实信息
  638. */
  639. try {
  640. $result = $cosClient->copy(
  641. $bucket = $bucket,
  642. $key = $key,
  643. $copysource = '{bucket}.cos.{region}.myqcloud.com/{cos_path}',
  644. $options = array('VersionId' => '{versionId}'
  645. ));
  646. print_r($result);
  647. } catch (\Exception $e) {
  648. echo($e);
  649. }
  650. # 恢复归档文件
  651. ## restoreObject
  652. try {
  653. $result = $cosClient->restoreObject(array(
  654. 'Bucket' => $bucket,
  655. 'Key' => $key,
  656. 'Days' => 7,
  657. 'CASJobParameters' => array(
  658. 'Tier' => 'Bulk',
  659. ),
  660. ));
  661. print_r($result);
  662. } catch (\Exception $e) {
  663. echo($e);
  664. }
  665. # 其他服务
  666. ## 列出某bucket下所有的object
  667. try {
  668. $prefix = '';
  669. $marker = '';
  670. while (true) {
  671. $result = $cosClient->listObjects(array(
  672. 'Bucket' => $bucket,
  673. 'Marker' => $marker,
  674. 'MaxKeys' => 1000
  675. ));
  676. foreach ($result['Contents'] as $rt) {
  677. print_r($rt['Key'] . " ");
  678. /*
  679. * 使用下面的代码可以删除全部object
  680. */
  681. // try {
  682. // $result = $cosClient->deleteobjects(array(
  683. // 'Bucket' => $bucket,
  684. // 'Key' => $rt['Key']));
  685. // print_r($result);
  686. // } catch (\Exception $e) {
  687. // echo($e);
  688. // }
  689. }
  690. $marker = $result['NextMarker'];
  691. if (!$result['IsTruncated']) {
  692. break;
  693. }
  694. }
  695. } catch (\Exception $e) {
  696. echo($e);
  697. }
  698. ## 删除所有因上传失败而产生的分块
  699. /*
  700. * 可以清理掉因分块上传失败
  701. */
  702. try {
  703. while (true) {
  704. $result = $cosClient->listMultipartUploads(
  705. array('Bucket' => $bucket,
  706. 'Prefix' => ''));
  707. if (count($result['Uploads']) == 0) {
  708. break;
  709. }
  710. foreach ($result['Uploads'] as $upload) {
  711. try {
  712. $rt = $cosClient->abortMultipartUpload(array(
  713. 'Bucket' => $bucket,
  714. 'Key' => $upload['Key'],
  715. 'UploadId' => $upload['UploadId']
  716. ));
  717. print_r($rt);
  718. } catch (\Exception $e) {
  719. echo($e);
  720. }
  721. }
  722. }
  723. } catch (\Exception $e) {
  724. echo($e);
  725. }
  726. ## 分块上传断点重传
  727. /*
  728. * 仅适用于分块上传失败的情况
  729. * 需要填写上传失败的uploadId
  730. */
  731. try {
  732. $result = $cosClient->resumeUpload(
  733. $bucket = $bucket,
  734. $key = $key,
  735. $body = fopen("E:/test.txt", 'rb'),
  736. $uploadId = '152448808231afdf221eb558ab15d1e455d2afd025c5663936142fdf5614ebf6d1668e2eda'
  737. );
  738. print_r($result);
  739. } catch (\Exception $e) {
  740. echo($e);
  741. }
  742. ## 删除某些前缀的空bucket
  743. function startsWith($haystack, $needle)
  744. {
  745. $length = strlen($needle);
  746. return (substr($haystack, 0, $length) === $needle);
  747. }
  748. try {
  749. $result = $cosClient->listBuckets();
  750. foreach ($result['Buckets'] as $bucket) {
  751. $region = $bucket['Location'];
  752. $name = $bucket['Name'];
  753. if (startsWith($name, 'lewzylu')) {
  754. try {
  755. $cosClient2 = new Qcloud\Cos\Client(array(
  756. 'region' => $region,
  757. 'credentials' => array(
  758. //getenv为获取本地环境变量,请替换为真实密钥
  759. 'secretId' => getenv('COS_KEY'),
  760. 'secretKey' => getenv('COS_SECRET'))
  761. ));
  762. $rt = $cosClient2->deleteBucket(array('Bucket' => $name));
  763. print_r($rt);
  764. } catch (\Exception $e) {
  765. }
  766. }
  767. }
  768. } catch (\Exception $e) {
  769. echo($e);
  770. }