TestHelper.php 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. <?php
  2. namespace Qcloud\Cos\Tests;
  3. use Qcloud\Cos\Client;
  4. class TestHelper {
  5. public static function nuke($bucket) {
  6. try {
  7. $cosClient = new Client(array('region' => getenv('COS_REGION'),
  8. 'credentials'=> array(
  9. 'appId' => getenv('COS_APPID'),
  10. 'secretId' => getenv('COS_KEY'),
  11. 'secretKey' => getenv('COS_SECRET'))));
  12. $result = $cosClient->listObjects(array('Bucket' => $bucket));
  13. if (isset($result['Contents'])) {
  14. foreach ($result['Contents'] as $content) {
  15. $cosClient->deleteObject(array('Bucket' => $bucket, 'Key' => $content['Key']));
  16. }
  17. }
  18. while(True){
  19. $result = $cosClient->ListMultipartUploads(
  20. array('Bucket' => $bucket));
  21. if (count($result['Uploads']) == 0){
  22. break;
  23. }
  24. foreach ($result['Uploads'] as $upload) {
  25. try {
  26. $rt = $cosClient->AbortMultipartUpload(
  27. array('Bucket' => $bucket,
  28. 'Key' => $upload['Key'],
  29. 'UploadId' => $upload['UploadId']));
  30. } catch (\Exception $e) {
  31. print_r($e);
  32. }
  33. }
  34. }
  35. $cosClient->deleteBucket(array('Bucket' => $bucket));
  36. } catch (\Exception $e) {
  37. // echo "$e\n";
  38. }
  39. }
  40. }