Files
addons
app_download_files
extend
hyhproject
mobile
oss
static
thinkphp
upload
vendor
5ini99
composer
oss-sdk
samples
Bucket.php
BucketCors.php
BucketLifecycle.php
BucketLogging.php
BucketReferer.php
BucketWebsite.php
Callback.php
Common.php
Config.php
Image.php
LiveChannel.php
MultipartUpload.php
Object.php
RunAll.php
Signature.php
src
tests
.coveralls.yml
.gitignore
.travis.yml
CHANGELOG.md
LICENSE.md
README-CN.md
README.md
autoload.php
build-phar.sh
composer.json
example.jpg
index.php
phpunit.xml
swoole
wechat
.htaccess
autoload.php
wxtmp
.gitignore
.htaccess
.user.ini
404.html
H5436787D.wgt
admin.php
app-release.apk
app_download.html
cash.lock
demo.php
get_version.php
get_version_new.php
index.html
index.php
reg.lock
robots.txt
qlg.tsgz.moe/vendor/oss-sdk/samples/BucketLifecycle.php
2019-09-06 23:53:10 +08:00

110 lines
3.5 KiB
PHP
Executable File

<?php
require_once __DIR__ . '/Common.php';
use OSS\OssClient;
use OSS\Core\OssException;
use OSS\Model\LifecycleAction;
use OSS\Model\LifecycleConfig;
use OSS\Model\LifecycleRule;
$bucket = Common::getBucketName();
$ossClient = Common::getOssClient();
if (is_null($ossClient)) exit(1);
//******************************* Simple Usage *******************************************************
// Set lifecycle configuration
$lifecycleConfig = new LifecycleConfig();
$actions = array();
$actions[] = new LifecycleAction("Expiration", "Days", 3);
$lifecycleRule = new LifecycleRule("delete obsoleted files", "obsoleted/", "Enabled", $actions);
$lifecycleConfig->addRule($lifecycleRule);
$ossClient->putBucketLifecycle($bucket, $lifecycleConfig);
Common::println("bucket $bucket lifecycleConfig created:" . $lifecycleConfig->serializeToXml());
// Get lifecycle configuration
$lifecycleConfig = $ossClient->getBucketLifecycle($bucket);
Common::println("bucket $bucket lifecycleConfig fetched:" . $lifecycleConfig->serializeToXml());
// Delete bucket lifecycle configuration
$ossClient->deleteBucketLifecycle($bucket);
Common::println("bucket $bucket lifecycleConfig deleted");
//***************************** For complete usage, see the following functions ***********************************************
putBucketLifecycle($ossClient, $bucket);
getBucketLifecycle($ossClient, $bucket);
deleteBucketLifecycle($ossClient, $bucket);
getBucketLifecycle($ossClient, $bucket);
/**
* Set bucket lifecycle configuration
*
* @param OssClient $ossClient OssClient instance
* @param string $bucket bucket name
* @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");
}
/**
* Get bucket lifecycle configuration
*
* @param OssClient $ossClient OssClient instance
* @param string $bucket bucket name
* @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");
}
/**
* Delete bucket lifecycle configuration
*
* @param OssClient $ossClient OssClient instance
* @param string $bucket bucket name
* @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");
}