OSS允许用户对Bucket设置访问日志记录,设置之后对于Bucket的访问会被记录成日志,日志存储在OSS上由用户指定的Bucket中,文件的格式为:
<TargetPrefix><SourceBucket>-YYYY-mm-DD-HH-MM-SS-UniqueString
其中TargetPrefix由用户指定。日志规则由以下3项组成:
提示:
- 以下场景的完整代码参见:GitHub
<?php
/**
* 设置bucket的Logging配置
*
* @param OssClient $ossClient OSSClient实例
* @param string $bucket 存储空间名称
* @return null
*/
function putBucketLogging($ossClient, $bucket)
{
$option = array();
//访问日志存放在本bucket下
$targetBucket = $bucket;
$targetPrefix = "access.log";
try {
$ossClient->putBucketLogging($bucket, $targetBucket, $targetPrefix, $option);
} catch (OssException $e) {
printf(__FUNCTION__ . ": FAILED\n");
printf($e->getMessage() . "\n");
return;
}
print(__FUNCTION__ . ": OK" . "\n");
}
<?php
/**
* 获取bucket的Logging配置
*
* @param OssClient $ossClient OSSClient实例
* @param string $bucket 存储空间名称
* @return null
*/
function getBucketLogging($ossClient, $bucket)
{
$loggingConfig = null;
$options = array();
try {
$loggingConfig = $ossClient->getBucketLogging($bucket, $options);
} catch (OssException $e) {
printf(__FUNCTION__ . ": FAILED\n");
printf($e->getMessage() . "\n");
return;
}
print(__FUNCTION__ . ": OK" . "\n");
print($loggingConfig->serializeToXml() . "\n");
}
<?php
/**
* 删除bucket的Logging配置
*
* @param OssClient $ossClient OSSClient实例
* @param string $bucket 存储空间名称
* @return null
*/
function deleteBucketLogging($ossClient, $bucket)
{
try {
$ossClient->deleteBucketLogging($bucket);
} catch (OssException $e) {
printf(__FUNCTION__ . ": FAILED\n");
printf($e->getMessage() . "\n");
return;
}
print(__FUNCTION__ . ": OK" . "\n");
}
版权声明:本文内容由阿里云实名注册用户自发贡献,版权归原作者所有,阿里云开发者社区不拥有其著作权,亦不承担相应法律责任。具体规则请查看《阿里云开发者社区用户服务协议》和《阿里云开发者社区知识产权保护指引》。如果您发现本社区中有涉嫌抄袭的内容,填写侵权投诉表单进行举报,一经查实,本社区将立刻删除涉嫌侵权内容。