BucketLogging.php 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. <?php
  2. require_once __DIR__ . '/Common.php';
  3. use OSS\OssClient;
  4. use OSS\Core\OssException;
  5. $bucket = Common::getBucketName();
  6. $ossClient = Common::getOssClient();
  7. if (is_null($ossClient)) exit(1);
  8. //*******************************简单使用***************************************************************
  9. // 设置Bucket访问日志记录规则, 访问日志文件的存放位置是同bucket下的access.log前缀的文件
  10. $ossClient->putBucketLogging($bucket, $bucket, "access.log", array());
  11. Common::println("bucket $bucket lifecycleConfig created");
  12. // 获取Bucket访问日志记录规则
  13. $loggingConfig = $ossClient->getBucketLogging($bucket, array());
  14. Common::println("bucket $bucket lifecycleConfig fetched:" . $loggingConfig->serializeToXml());
  15. // 删除Bucket访问日志记录规则
  16. $loggingConfig = $ossClient->getBucketLogging($bucket, array());
  17. Common::println("bucket $bucket lifecycleConfig deleted");
  18. //******************************* 完整用法参考下面函数 ****************************************************
  19. putBucketLogging($ossClient, $bucket);
  20. getBucketLogging($ossClient, $bucket);
  21. deleteBucketLogging($ossClient, $bucket);
  22. getBucketLogging($ossClient, $bucket);
  23. /**
  24. * 设置bucket的Logging配置
  25. *
  26. * @param OssClient $ossClient OssClient实例
  27. * @param string $bucket 存储空间名称
  28. * @return null
  29. */
  30. function putBucketLogging($ossClient, $bucket)
  31. {
  32. $option = array();
  33. //访问日志存放在本bucket下
  34. $targetBucket = $bucket;
  35. $targetPrefix = "access.log";
  36. try {
  37. $ossClient->putBucketLogging($bucket, $targetBucket, $targetPrefix, $option);
  38. } catch (OssException $e) {
  39. printf(__FUNCTION__ . ": FAILED\n");
  40. printf($e->getMessage() . "\n");
  41. return;
  42. }
  43. print(__FUNCTION__ . ": OK" . "\n");
  44. }
  45. /**
  46. * 获取bucket的Logging配置
  47. *
  48. * @param OssClient $ossClient OssClient实例
  49. * @param string $bucket 存储空间名称
  50. * @return null
  51. */
  52. function getBucketLogging($ossClient, $bucket)
  53. {
  54. $loggingConfig = null;
  55. $options = array();
  56. try {
  57. $loggingConfig = $ossClient->getBucketLogging($bucket, $options);
  58. } catch (OssException $e) {
  59. printf(__FUNCTION__ . ": FAILED\n");
  60. printf($e->getMessage() . "\n");
  61. return;
  62. }
  63. print(__FUNCTION__ . ": OK" . "\n");
  64. print($loggingConfig->serializeToXml() . "\n");
  65. }
  66. /**
  67. * 删除bucket的Logging配置
  68. *
  69. * @param OssClient $ossClient OssClient实例
  70. * @param string $bucket 存储空间名称
  71. * @return null
  72. */
  73. function deleteBucketLogging($ossClient, $bucket)
  74. {
  75. try {
  76. $ossClient->deleteBucketLogging($bucket);
  77. } catch (OssException $e) {
  78. printf(__FUNCTION__ . ": FAILED\n");
  79. printf($e->getMessage() . "\n");
  80. return;
  81. }
  82. print(__FUNCTION__ . ": OK" . "\n");
  83. }