暂无描述

BucketLifecycle.php 3.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. <?php
  2. require_once __DIR__ . '/Common.php';
  3. use OSS\OssClient;
  4. use OSS\Core\OssException;
  5. use OSS\Model\LifecycleAction;
  6. use OSS\Model\LifecycleConfig;
  7. use OSS\Model\LifecycleRule;
  8. $bucket = Common::getBucketName();
  9. $ossClient = Common::getOssClient();
  10. if (is_null($ossClient)) exit(1);
  11. //******************************* 简单使用 *******************************************************
  12. //设置lifecycle规则
  13. $lifecycleConfig = new LifecycleConfig();
  14. $actions = array();
  15. $actions[] = new LifecycleAction("Expiration", "Days", 3);
  16. $lifecycleRule = new LifecycleRule("delete obsoleted files", "obsoleted/", "Enabled", $actions);
  17. $lifecycleConfig->addRule($lifecycleRule);
  18. $ossClient->putBucketLifecycle($bucket, $lifecycleConfig);
  19. Common::println("bucket $bucket lifecycleConfig created:" . $lifecycleConfig->serializeToXml());
  20. //获取lifecycle规则
  21. $lifecycleConfig = $ossClient->getBucketLifecycle($bucket);
  22. Common::println("bucket $bucket lifecycleConfig fetched:" . $lifecycleConfig->serializeToXml());
  23. //删除bucket的lifecycle配置
  24. $ossClient->deleteBucketLifecycle($bucket);
  25. Common::println("bucket $bucket lifecycleConfig deleted");
  26. //***************************** 完整用法参考下面函数 ***********************************************
  27. putBucketLifecycle($ossClient, $bucket);
  28. getBucketLifecycle($ossClient, $bucket);
  29. deleteBucketLifecycle($ossClient, $bucket);
  30. getBucketLifecycle($ossClient, $bucket);
  31. /**
  32. * 设置bucket的生命周期配置
  33. *
  34. * @param OssClient $ossClient OssClient实例
  35. * @param string $bucket 存储空间名称
  36. * @return null
  37. */
  38. function putBucketLifecycle($ossClient, $bucket)
  39. {
  40. $lifecycleConfig = new LifecycleConfig();
  41. $actions = array();
  42. $actions[] = new LifecycleAction(OssClient::OSS_LIFECYCLE_EXPIRATION, OssClient::OSS_LIFECYCLE_TIMING_DAYS, 3);
  43. $lifecycleRule = new LifecycleRule("delete obsoleted files", "obsoleted/", "Enabled", $actions);
  44. $lifecycleConfig->addRule($lifecycleRule);
  45. $actions = array();
  46. $actions[] = new LifecycleAction(OssClient::OSS_LIFECYCLE_EXPIRATION, OssClient::OSS_LIFECYCLE_TIMING_DATE, '2022-10-12T00:00:00.000Z');
  47. $lifecycleRule = new LifecycleRule("delete temporary files", "temporary/", "Enabled", $actions);
  48. $lifecycleConfig->addRule($lifecycleRule);
  49. try {
  50. $ossClient->putBucketLifecycle($bucket, $lifecycleConfig);
  51. } catch (OssException $e) {
  52. printf(__FUNCTION__ . ": FAILED\n");
  53. printf($e->getMessage() . "\n");
  54. return;
  55. }
  56. print(__FUNCTION__ . ": OK" . "\n");
  57. }
  58. /**
  59. * 获取bucket的生命周期配置
  60. *
  61. * @param OssClient $ossClient OssClient实例
  62. * @param string $bucket 存储空间名称
  63. * @return null
  64. */
  65. function getBucketLifecycle($ossClient, $bucket)
  66. {
  67. $lifecycleConfig = null;
  68. try {
  69. $lifecycleConfig = $ossClient->getBucketLifecycle($bucket);
  70. } catch (OssException $e) {
  71. printf(__FUNCTION__ . ": FAILED\n");
  72. printf($e->getMessage() . "\n");
  73. return;
  74. }
  75. print(__FUNCTION__ . ": OK" . "\n");
  76. print($lifecycleConfig->serializeToXml() . "\n");
  77. }
  78. /**
  79. * 删除bucket的生命周期配置
  80. *
  81. * @param OssClient $ossClient OssClient实例
  82. * @param string $bucket 存储空间名称
  83. * @return null
  84. */
  85. function deleteBucketLifecycle($ossClient, $bucket)
  86. {
  87. try {
  88. $ossClient->deleteBucketLifecycle($bucket);
  89. } catch (OssException $e) {
  90. printf(__FUNCTION__ . ": FAILED\n");
  91. printf($e->getMessage() . "\n");
  92. return;
  93. }
  94. print(__FUNCTION__ . ": OK" . "\n");
  95. }