Skip to content

Commit 6ab28f5

Browse files
authored
Merge pull request #383 from qiniu/features/support-object-lifecycle
support setObjectLifecycle api
2 parents 4dc187e + 4a64068 commit 6ab28f5

File tree

2 files changed

+183
-0
lines changed

2 files changed

+183
-0
lines changed

src/Qiniu/Storage/BucketManager.php

+99
Original file line numberDiff line numberDiff line change
@@ -926,6 +926,74 @@ public function deleteAfterDays($bucket, $key, $days)
926926
return $this->rsPost($bucket, $path);
927927
}
928928

929+
/**
930+
* 更新 object 生命周期
931+
*
932+
* @param string $bucket 空间名
933+
* @param string $key 目标资源
934+
* @param int $to_line_after_days 多少天后将文件转为低频存储,设置为 -1 表示取消已设置的转低频存储的生命周期规则, 0 表示不修改转低频生命周期规则。
935+
* @param int $to_archive_after_days 多少天后将文件转为归档存储,设置为 -1 表示取消已设置的转归档存储的生命周期规则, 0 表示不修改转归档生命周期规则。
936+
* @param int $to_deep_archive_after_days 多少天后将文件转为深度归档存储,设置为 -1 表示取消已设置的转深度归档存储的生命周期规则, 0 表示不修改转深度归档生命周期规则。
937+
* @param int $delete_after_days 多少天后将文件删除,设置为 -1 表示取消已设置的删除存储的生命周期规则, 0 表示不修改删除存储的生命周期规则。
938+
* @return array
939+
*/
940+
public function setObjectLifecycle(
941+
$bucket,
942+
$key,
943+
$to_line_after_days = 0,
944+
$to_archive_after_days = 0,
945+
$to_deep_archive_after_days = 0,
946+
$delete_after_days = 0
947+
) {
948+
return $this->setObjectLifecycleWithCond(
949+
$bucket,
950+
$key,
951+
null,
952+
$to_line_after_days,
953+
$to_archive_after_days,
954+
$to_deep_archive_after_days,
955+
$delete_after_days
956+
);
957+
}
958+
959+
/**
960+
* 更新 object 生命周期
961+
*
962+
* @param string $bucket 空间名
963+
* @param string $key 目标资源
964+
* @param int $to_line_after_days 多少天后将文件转为低频存储,设置为 -1 表示取消已设置的转低频存储的生命周期规则, 0 表示不修改转低频生命周期规则。
965+
* @param int $to_archive_after_days 多少天后将文件转为归档存储,设置为 -1 表示取消已设置的转归档存储的生命周期规则, 0 表示不修改转归档生命周期规则。
966+
* @param int $to_deep_archive_after_days 多少天后将文件转为深度归档存储,设置为 -1 表示取消已设置的转深度归档存储的生命周期规则, 0 表示不修改转深度归档生命周期规则。
967+
* @param int $delete_after_days 多少天后将文件删除,设置为 -1 表示取消已设置的删除存储的生命周期规则, 0 表示不修改删除存储的生命周期规则。
968+
* @param array<string, mixed> $cond 匹配条件,只有条件匹配才会设置成功,目前支持:hash、mime、fsize、putTime
969+
* @return array
970+
*/
971+
public function setObjectLifecycleWithCond(
972+
$bucket,
973+
$key,
974+
$cond = null,
975+
$to_line_after_days = 0,
976+
$to_archive_after_days = 0,
977+
$to_deep_archive_after_days = 0,
978+
$delete_after_days = 0
979+
) {
980+
$encodedEntry = \Qiniu\entry($bucket, $key);
981+
$path = '/lifecycle/' . $encodedEntry .
982+
'/toIAAfterDays/' . $to_line_after_days .
983+
'/toArchiveAfterDays/' . $to_archive_after_days .
984+
'/toDeepArchiveAfterDays/' . $to_deep_archive_after_days .
985+
'/deleteAfterDays/' . $delete_after_days;
986+
if ($cond != null) {
987+
$condStrArr = array();
988+
foreach ($cond as $key => $value) {
989+
array_push($condStrArr, $key . '=' . $value);
990+
}
991+
$condStr = implode('&', $condStrArr);
992+
$path .= '/cond' . \Qiniu\base64_urlSafeEncode($condStr);
993+
}
994+
return $this->rsPost($bucket, $path);
995+
}
996+
929997
private function getUcHost()
930998
{
931999
$scheme = "http://";
@@ -1062,6 +1130,37 @@ public static function buildBatchDeleteAfterDays($bucket, $key_day_pairs)
10621130
return $data;
10631131
}
10641132

1133+
/**
1134+
* @param string $bucket 空间名
1135+
* @param array<string> $keys 目标资源
1136+
* @param int $to_line_after_days 多少天后将文件转为低频存储,设置为 -1 表示取消已设置的转低频存储的生命周期规则, 0 表示不修改转低频生命周期规则。
1137+
* @param int $to_archive_after_days 多少天后将文件转为归档存储,设置为 -1 表示取消已设置的转归档存储的生命周期规则, 0 表示不修改转归档生命周期规则。
1138+
* @param int $to_deep_archive_after_days 多少天后将文件转为深度归档存储,设置为 -1 表示取消已设置的转深度归档存储的生命周期规则, 0 表示不修改转深度归档生命周期规则。
1139+
* @param int $delete_after_days 多少天后将文件删除,设置为 -1 表示取消已设置的删除存储的生命周期规则, 0 表示不修改删除存储的生命周期规则。
1140+
*
1141+
* @retrun array<string>
1142+
*/
1143+
public static function buildBatchSetObjectLifecycle(
1144+
$bucket,
1145+
$keys,
1146+
$to_line_after_days,
1147+
$to_archive_after_days,
1148+
$to_deep_archive_after_days,
1149+
$delete_after_days
1150+
) {
1151+
$result = array();
1152+
foreach ($keys as $key) {
1153+
$encodedEntry = \Qiniu\entry($bucket, $key);
1154+
$op = '/lifecycle/' . $encodedEntry .
1155+
'/toIAAfterDays/' . $to_line_after_days .
1156+
'/toArchiveAfterDays/' . $to_archive_after_days .
1157+
'/toDeepArchiveAfterDays/' . $to_deep_archive_after_days .
1158+
'/deleteAfterDays/' . $delete_after_days;
1159+
array_push($result, $op);
1160+
}
1161+
return $result;
1162+
}
1163+
10651164
public static function buildBatchChangeMime($bucket, $key_mime_pairs)
10661165
{
10671166
$data = array();

tests/Qiniu/Tests/BucketTest.php

+84
Original file line numberDiff line numberDiff line change
@@ -476,6 +476,90 @@ public function testDeleteAfterDays()
476476
$this->assertEquals(null, $ret);
477477
}
478478

479+
public function testSetObjectLifecycle()
480+
{
481+
$key = 'setObjectLifeCycle' . rand();
482+
$this->bucketManager->delete($this->bucketName, $key);
483+
484+
$this->bucketManager->copy(
485+
$this->bucketName,
486+
$this->key,
487+
$this->bucketName,
488+
$key
489+
);
490+
list($ret, $err) = $this->bucketManager->setObjectLifecycle(
491+
$this->bucketName,
492+
$key,
493+
10,
494+
20,
495+
30,
496+
40
497+
);
498+
$this->assertNull($err);
499+
500+
$this->bucketManager->delete($this->bucketName, $key);
501+
}
502+
503+
public function testSetObjectLifecycleWithCond()
504+
{
505+
$key = 'setObjectLifeCycleWithCond' . rand();
506+
$this->bucketManager->delete($this->bucketName, $key);
507+
508+
$this->bucketManager->copy(
509+
$this->bucketName,
510+
$this->key,
511+
$this->bucketName,
512+
$key
513+
);
514+
515+
list($ret, $err) = $this->bucketManager->stat($this->bucketName, $key);
516+
$this->assertNull($err);
517+
$key_hash = $ret['hash'];
518+
$key_fsize = $ret['fsize'];
519+
520+
list($ret, $err) = $this->bucketManager->setObjectLifecycleWithCond(
521+
$this->bucketName,
522+
$key,
523+
array(
524+
'hash' => $key_hash,
525+
'fsize' => $key_fsize
526+
),
527+
10,
528+
20,
529+
30,
530+
40
531+
);
532+
$this->assertNull($err);
533+
534+
$this->bucketManager->delete($this->bucketName, $key);
535+
}
536+
537+
public function testBatchSetObjectLifecycle()
538+
{
539+
$key = 'batchSetObjectLifeCycle' . rand();
540+
$this->bucketManager->delete($this->bucketName, $key);
541+
542+
$this->bucketManager->copy(
543+
$this->bucketName,
544+
$this->key,
545+
$this->bucketName,
546+
$key
547+
);
548+
$ops = BucketManager::buildBatchSetObjectLifecycle(
549+
$this->bucketName,
550+
array($key),
551+
10,
552+
20,
553+
30,
554+
40
555+
);
556+
list($ret, $err) = $this->bucketManager->batch($ops);
557+
$this->assertNull($err);
558+
$this->assertEquals(200, $ret[0]['code']);
559+
560+
$this->bucketManager->delete($this->bucketName, $key);
561+
}
562+
479563
public function testGetCorsRules()
480564
{
481565
list($ret, $err) = $this->bucketManager->getCorsRules($this->bucketName);

0 commit comments

Comments
 (0)