OSS提供文件生命周期管理来为用户管理对象。用户可以为某个存储空间定义生命周期配置,来为该存储空间的文件定义各种规则。目前,用户可以通过规则来删除相匹配的文件。每条规则都由如下几个部分组成:
提示:
- 以下场景的完整代码参见:GitHub
<?xml version="1.0" encoding="utf-8"?>
<LifecycleConfiguration>
<Rule>
<ID>delete obsoleted files</ID>
<Prefix>obsoleted/</Prefix>
<Status>Enabled</Status>
<Expiration><Days>3</Days></Expiration>
</Rule>
<Rule>
<ID>delete temporary files</ID>
<Prefix>temporary/</Prefix>
<Status>Enabled</Status>
<Expiration><Date>2022-10-12T00:00:00.000Z</Date></Expiration>
</Rule>
</LifecycleConfiguration>
<?php
/**
* 设置bucket的生命周期配置
*
* @param OssClient $ossClient OssClient实例
* @param string $bucket 存储空间名称
* @return null
*/
function putBucketLifecycle($ossClient, $bucket)
{
$lifecycleConfig = new LifecycleConfig();
$actions = array();
$actions[] = new LifecycleAction(OssClient::OSS_LIFECYCLE_EXPIRATION, OssClient::OSS_LIFECYCLE_TIMING_DAYS, 3);
$lifecycleRule = new LifecycleRule("delete obsoleted files", "obsoleted/", "Enabled", $actions);
$lifecycleConfig->addRule($lifecycleRule);
$actions = array();
$actions[] = new LifecycleAction(OssClient::OSS_LIFECYCLE_EXPIRATION, OssClient::OSS_LIFECYCLE_TIMING_DATE, '2022-10-12T00:00:00.000Z');
$lifecycleRule = new LifecycleRule("delete temporary files", "temporary/", "Enabled", $actions);
$lifecycleConfig->addRule($lifecycleRule);
try {
$ossClient->putBucketLifecycle($bucket, $lifecycleConfig);
} catch (OssException $e) {
printf(__FUNCTION__ . ": FAILED\n");
printf($e->getMessage() . "\n");
return;
}
print(__FUNCTION__ . ": OK" . "\n");
}
<?php
/**
* 获取bucket的生命周期配置
*
* @param OssClient $ossClient OSSClient实例
* @param string $bucket bucket名字
* @return null
*/
function getBucketLifecycle($ossClient, $bucket)
{
$lifecycleConfig = null;
try{
$lifecycleConfig = $ossClient->getBucketLifecycle($bucket);
} catch(OssException $e) {
printf(__FUNCTION__ . ": FAILED\n");
printf($e->getMessage() . "\n");
return;
}
print(__FUNCTION__ . ": OK" . "\n");
print($lifecycleConfig->serializeToXml() . "\n");
}
<?php
/**
* 删除bucket的生命周期配置
*
* @param OssClient $ossClient OSSClient实例
* @param string $bucket bucket名字
* @return null
*/
function deleteBucketLifecycle($ossClient, $bucket)
{
try{
$ossClient->deleteBucketLifecycle($bucket);
} catch(OssException $e) {
printf(__FUNCTION__ . ": FAILED\n");
printf($e->getMessage() . "\n");
return;
}
print(__FUNCTION__ . ": OK" . "\n");
}
版权声明:本文内容由阿里云实名注册用户自发贡献,版权归原作者所有,阿里云开发者社区不拥有其著作权,亦不承担相应法律责任。具体规则请查看《阿里云开发者社区用户服务协议》和《阿里云开发者社区知识产权保护指引》。如果您发现本社区中有涉嫌抄袭的内容,填写侵权投诉表单进行举报,一经查实,本社区将立刻删除涉嫌侵权内容。