use of com.aliyun.oss.model.LifecycleRule.StorageTransition in project aliyun-oss-java-sdk by aliyun.
the class BucketLifecycleTest method testNormalSetBucketLifecycle.
@Test
public void testNormalSetBucketLifecycle() throws ParseException {
final String bucketName = "normal-set-bucket-lifecycle";
final String ruleId0 = "delete obsoleted files";
final String matchPrefix0 = "obsoleted0/";
final String ruleId1 = "delete temporary files";
final String matchPrefix1 = "temporary0/";
final String ruleId2 = "delete obsoleted multipart files";
final String matchPrefix2 = "obsoleted1/";
final String ruleId3 = "delete temporary multipart files";
final String matchPrefix3 = "temporary1/";
final String ruleId4 = "delete temporary files(2)";
final String matchPrefix4 = "temporary2/";
final String ruleId5 = "delete temporary files(3)";
final String matchPrefix5 = "temporary3/";
try {
ossClient.createBucket(bucketName);
SetBucketLifecycleRequest request = new SetBucketLifecycleRequest(bucketName);
request.AddLifecycleRule(new LifecycleRule(ruleId0, matchPrefix0, RuleStatus.Enabled, 3));
request.AddLifecycleRule(new LifecycleRule(ruleId1, matchPrefix1, RuleStatus.Enabled, DateUtil.parseIso8601Date("2022-10-12T00:00:00.000Z")));
LifecycleRule rule = new LifecycleRule(ruleId2, matchPrefix2, RuleStatus.Enabled, 3);
LifecycleRule.AbortMultipartUpload abortMultipartUpload = new LifecycleRule.AbortMultipartUpload();
abortMultipartUpload.setExpirationDays(3);
rule.setAbortMultipartUpload(abortMultipartUpload);
request.AddLifecycleRule(rule);
rule = new LifecycleRule(ruleId3, matchPrefix3, RuleStatus.Enabled, 30);
abortMultipartUpload = new LifecycleRule.AbortMultipartUpload();
abortMultipartUpload.setCreatedBeforeDate(DateUtil.parseIso8601Date("2022-10-12T00:00:00.000Z"));
rule.setAbortMultipartUpload(abortMultipartUpload);
List<StorageTransition> storageTransitions = new ArrayList<StorageTransition>();
StorageTransition storageTransition = new StorageTransition();
storageTransition.setStorageClass(StorageClass.IA);
storageTransition.setExpirationDays(10);
storageTransitions.add(storageTransition);
storageTransition = new LifecycleRule.StorageTransition();
storageTransition.setStorageClass(StorageClass.Archive);
storageTransition.setExpirationDays(20);
storageTransitions.add(storageTransition);
rule.setStorageTransition(storageTransitions);
request.AddLifecycleRule(rule);
rule = new LifecycleRule(ruleId4, matchPrefix4, RuleStatus.Enabled);
rule.setCreatedBeforeDate(DateUtil.parseIso8601Date("2022-10-12T00:00:00.000Z"));
request.AddLifecycleRule(rule);
rule = new LifecycleRule(ruleId5, matchPrefix5, RuleStatus.Enabled);
storageTransition = new LifecycleRule.StorageTransition();
storageTransition.setStorageClass(StorageClass.Archive);
storageTransition.setCreatedBeforeDate(DateUtil.parseIso8601Date("2022-10-12T00:00:00.000Z"));
storageTransitions = new ArrayList<StorageTransition>();
storageTransitions.add(storageTransition);
rule.setStorageTransition(storageTransitions);
request.AddLifecycleRule(rule);
ossClient.setBucketLifecycle(request);
List<LifecycleRule> rules = ossClient.getBucketLifecycle(bucketName);
Assert.assertEquals(rules.size(), 6);
LifecycleRule r0 = rules.get(0);
Assert.assertEquals(r0.getId(), ruleId0);
Assert.assertEquals(r0.getPrefix(), matchPrefix0);
Assert.assertEquals(r0.getStatus(), RuleStatus.Enabled);
Assert.assertEquals(r0.getExpirationDays(), 3);
Assert.assertTrue(r0.getAbortMultipartUpload() == null);
LifecycleRule r1 = rules.get(1);
Assert.assertEquals(r1.getId(), ruleId1);
Assert.assertEquals(r1.getPrefix(), matchPrefix1);
Assert.assertEquals(r1.getStatus(), RuleStatus.Enabled);
Assert.assertEquals(DateUtil.formatIso8601Date(r1.getExpirationTime()), "2022-10-12T00:00:00.000Z");
Assert.assertTrue(r1.getAbortMultipartUpload() == null);
LifecycleRule r2 = rules.get(2);
Assert.assertEquals(r2.getId(), ruleId2);
Assert.assertEquals(r2.getPrefix(), matchPrefix2);
Assert.assertEquals(r2.getStatus(), RuleStatus.Enabled);
Assert.assertEquals(r2.getExpirationDays(), 3);
Assert.assertNotNull(r2.getAbortMultipartUpload());
Assert.assertEquals(r2.getAbortMultipartUpload().getExpirationDays(), 3);
LifecycleRule r3 = rules.get(3);
Assert.assertEquals(r3.getId(), ruleId3);
Assert.assertEquals(r3.getPrefix(), matchPrefix3);
Assert.assertEquals(r3.getStatus(), RuleStatus.Enabled);
Assert.assertEquals(r3.getExpirationDays(), 30);
Assert.assertNotNull(r3.getAbortMultipartUpload());
Assert.assertEquals(DateUtil.formatIso8601Date(r3.getAbortMultipartUpload().getCreatedBeforeDate()), "2022-10-12T00:00:00.000Z");
Assert.assertTrue(r3.hasStorageTransition());
Assert.assertTrue(r3.getStorageTransition().get(0).getExpirationDays() == 10);
Assert.assertEquals(r3.getStorageTransition().get(0).getStorageClass(), StorageClass.IA);
Assert.assertTrue(r3.getStorageTransition().get(1).getExpirationDays() == 20);
Assert.assertEquals(r3.getStorageTransition().get(1).getStorageClass(), StorageClass.Archive);
LifecycleRule r4 = rules.get(4);
Assert.assertEquals(r4.getId(), ruleId4);
Assert.assertEquals(r4.getPrefix(), matchPrefix4);
Assert.assertEquals(r4.getStatus(), RuleStatus.Enabled);
Assert.assertEquals(DateUtil.formatIso8601Date(r4.getCreatedBeforeDate()), "2022-10-12T00:00:00.000Z");
Assert.assertTrue(r4.getAbortMultipartUpload() == null);
LifecycleRule r5 = rules.get(5);
Assert.assertEquals(r5.getId(), ruleId5);
Assert.assertEquals(r5.getPrefix(), matchPrefix5);
Assert.assertEquals(r5.getStatus(), RuleStatus.Enabled);
Assert.assertFalse(r5.hasCreatedBeforeDate());
Assert.assertFalse(r5.hasExpirationTime());
Assert.assertFalse(r5.hasExpirationDays());
Assert.assertFalse(r5.hasAbortMultipartUpload());
Assert.assertTrue(r5.hasStorageTransition());
Assert.assertEquals(DateUtil.formatIso8601Date(r5.getStorageTransition().get(0).getCreatedBeforeDate()), "2022-10-12T00:00:00.000Z");
Assert.assertEquals(r5.getStorageTransition().get(0).getStorageClass(), StorageClass.Archive);
// Override existing lifecycle rules
final String nullRuleId = null;
request.clearLifecycles();
request.AddLifecycleRule(new LifecycleRule(nullRuleId, matchPrefix0, RuleStatus.Enabled, 7));
ossClient.setBucketLifecycle(request);
waitForCacheExpiration(5);
rules = ossClient.getBucketLifecycle(bucketName);
Assert.assertEquals(rules.size(), 1);
r0 = rules.get(0);
Assert.assertEquals(matchPrefix0, r0.getPrefix());
Assert.assertEquals(r0.getStatus(), RuleStatus.Enabled);
Assert.assertEquals(r0.getExpirationDays(), 7);
ossClient.deleteBucketLifecycle(bucketName);
// Try get bucket lifecycle again
try {
ossClient.getBucketLifecycle(bucketName);
} catch (OSSException e) {
Assert.assertEquals(OSSErrorCode.NO_SUCH_LIFECYCLE, e.getErrorCode());
Assert.assertTrue(e.getMessage().startsWith(NO_SUCH_LIFECYCLE_ERR));
}
} catch (OSSException e) {
Assert.fail(e.getMessage());
} finally {
ossClient.deleteBucket(bucketName);
}
}
use of com.aliyun.oss.model.LifecycleRule.StorageTransition in project aliyun-oss-java-sdk by aliyun.
the class BucketLifecycleTest method testUnormalSetBucketLifecycle.
@Test
public void testUnormalSetBucketLifecycle() throws ParseException {
final String bucketName = "unormal-set-bucket-lifecycle";
final String ruleId0 = "delete obsoleted files";
final String matchPrefix0 = "obsoleted/";
try {
ossClient.createBucket(bucketName);
// Set non-existent bucket
final String nonexistentBucket = "nonexistent-bucket";
final LifecycleRule r = new LifecycleRule(ruleId0, matchPrefix0, RuleStatus.Enabled, 3);
try {
SetBucketLifecycleRequest request = new SetBucketLifecycleRequest(nonexistentBucket);
request.AddLifecycleRule(r);
ossClient.setBucketLifecycle(request);
Assert.fail("Set bucket lifecycle should not be successful");
} catch (OSSException e) {
Assert.assertEquals(OSSErrorCode.NO_SUCH_BUCKET, e.getErrorCode());
Assert.assertTrue(e.getMessage().startsWith(NO_SUCH_BUCKET_ERR));
}
// Set bucket without ownership
final String bucketWithoutOwnership = "oss";
try {
SetBucketLifecycleRequest request = new SetBucketLifecycleRequest(bucketWithoutOwnership);
request.AddLifecycleRule(r);
ossClient.setBucketLifecycle(request);
Assert.fail("Set bucket lifecycle should not be successful");
} catch (OSSException e) {
Assert.assertEquals(OSSErrorCode.ACCESS_DENIED, e.getErrorCode());
}
// Set length of rule id exceeding RULE_ID_MAX_LENGTH(255)
final String ruleId256 = genRandomString(MAX_RULE_ID_LENGTH + 1);
try {
SetBucketLifecycleRequest request = new SetBucketLifecycleRequest(bucketName);
request.AddLifecycleRule(new LifecycleRule(ruleId256, matchPrefix0, RuleStatus.Enabled, 3));
Assert.fail("Set bucket lifecycle should not be successful");
} catch (Exception e) {
Assert.assertTrue(e instanceof IllegalArgumentException);
}
// Set size of lifecycle rules exceeding LIFECYCLE_RULE_MAX_LIMIT(1000)
try {
SetBucketLifecycleRequest request = new SetBucketLifecycleRequest(nonexistentBucket);
for (int i = 0; i < (MAX_LIFECYCLE_RULE_LIMIT + 1); i++) {
request.AddLifecycleRule(r);
}
Assert.fail("Set bucket lifecycle should not be successful");
} catch (Exception e) {
Assert.assertTrue(e instanceof IllegalArgumentException);
}
// Set both rule id and prefix null
final String nullRuleId = null;
final String nullMatchPrefix = null;
try {
SetBucketLifecycleRequest request = new SetBucketLifecycleRequest(bucketName);
request.AddLifecycleRule(new LifecycleRule(nullRuleId, nullMatchPrefix, RuleStatus.Enabled, 3));
ossClient.setBucketLifecycle(request);
} catch (Exception e) {
Assert.fail(e.getMessage());
}
// Set both expiration day and expiration time
try {
SetBucketLifecycleRequest request = new SetBucketLifecycleRequest(nonexistentBucket);
LifecycleRule invalidRule = new LifecycleRule();
invalidRule.setId(ruleId0);
invalidRule.setPrefix(matchPrefix0);
invalidRule.setStatus(RuleStatus.Enabled);
invalidRule.setExpirationTime(DateUtil.parseIso8601Date("2022-10-12T00:00:00.000Z"));
invalidRule.setExpirationDays(3);
request.AddLifecycleRule(invalidRule);
Assert.fail("Set bucket lifecycle should not be successful");
} catch (Exception e) {
Assert.assertTrue(e instanceof IllegalArgumentException);
}
// Set neither expiration day nor expiration time
try {
SetBucketLifecycleRequest request = new SetBucketLifecycleRequest(nonexistentBucket);
LifecycleRule invalidRule = new LifecycleRule();
invalidRule.setId(ruleId0);
invalidRule.setPrefix(matchPrefix0);
invalidRule.setStatus(RuleStatus.Enabled);
request.AddLifecycleRule(invalidRule);
Assert.fail("Set bucket lifecycle should not be successful");
} catch (Exception e) {
Assert.assertTrue(e instanceof IllegalArgumentException);
}
// With abort multipart upload option
try {
SetBucketLifecycleRequest request = new SetBucketLifecycleRequest(nonexistentBucket);
LifecycleRule invalidRule = new LifecycleRule();
invalidRule.setId(ruleId0);
invalidRule.setPrefix(matchPrefix0);
invalidRule.setStatus(RuleStatus.Enabled);
invalidRule.setExpirationDays(3);
LifecycleRule.AbortMultipartUpload abortMultipartUpload = new AbortMultipartUpload();
abortMultipartUpload.setExpirationDays(3);
abortMultipartUpload.setCreatedBeforeDate(DateUtil.parseIso8601Date("2022-10-12T00:00:00.000Z"));
invalidRule.setAbortMultipartUpload(abortMultipartUpload);
request.AddLifecycleRule(invalidRule);
Assert.fail("Set bucket lifecycle should not be successful");
} catch (Exception e) {
Assert.assertTrue(e instanceof IllegalArgumentException);
}
// With storage transition option
try {
SetBucketLifecycleRequest request = new SetBucketLifecycleRequest(nonexistentBucket);
LifecycleRule invalidRule = new LifecycleRule();
invalidRule.setId(ruleId0);
invalidRule.setPrefix(matchPrefix0);
invalidRule.setStatus(RuleStatus.Enabled);
invalidRule.setExpirationDays(3);
LifecycleRule.StorageTransition storageTransition = new StorageTransition();
storageTransition.setExpirationDays(3);
storageTransition.setCreatedBeforeDate(DateUtil.parseIso8601Date("2022-10-12T00:00:00.000Z"));
List<StorageTransition> storageTransitions = new ArrayList<StorageTransition>();
storageTransitions.add(storageTransition);
invalidRule.setStorageTransition(storageTransitions);
request.AddLifecycleRule(invalidRule);
Assert.fail("Set bucket lifecycle should not be successful");
} catch (Exception e) {
Assert.assertTrue(e instanceof IllegalArgumentException);
}
} finally {
ossClient.deleteBucket(bucketName);
}
}
use of com.aliyun.oss.model.LifecycleRule.StorageTransition in project aliyun-oss-java-sdk by aliyun.
the class ResponseParsers method parseGetBucketLifecycle.
/**
* Unmarshall get bucket lifecycle response body to lifecycle rules.
*/
@SuppressWarnings("unchecked")
public static List<LifecycleRule> parseGetBucketLifecycle(InputStream responseBody) throws ResponseParseException {
try {
Element root = getXmlRootElement(responseBody);
List<LifecycleRule> lifecycleRules = new ArrayList<LifecycleRule>();
List<Element> ruleElements = root.getChildren("Rule");
for (Element ruleElem : ruleElements) {
LifecycleRule rule = new LifecycleRule();
if (ruleElem.getChild("ID") != null) {
rule.setId(ruleElem.getChildText("ID"));
}
if (ruleElem.getChild("Prefix") != null) {
rule.setPrefix(ruleElem.getChildText("Prefix"));
}
if (ruleElem.getChild("Status") != null) {
rule.setStatus(RuleStatus.valueOf(ruleElem.getChildText("Status")));
}
if (ruleElem.getChild("Expiration") != null) {
if (ruleElem.getChild("Expiration").getChild("Date") != null) {
Date expirationDate = DateUtil.parseIso8601Date(ruleElem.getChild("Expiration").getChildText("Date"));
rule.setExpirationTime(expirationDate);
} else if (ruleElem.getChild("Expiration").getChild("Days") != null) {
rule.setExpirationDays(Integer.parseInt(ruleElem.getChild("Expiration").getChildText("Days")));
} else {
Date createdBeforeDate = DateUtil.parseIso8601Date(ruleElem.getChild("Expiration").getChildText("CreatedBeforeDate"));
rule.setCreatedBeforeDate(createdBeforeDate);
}
}
if (ruleElem.getChild("AbortMultipartUpload") != null) {
LifecycleRule.AbortMultipartUpload abortMultipartUpload = new LifecycleRule.AbortMultipartUpload();
if (ruleElem.getChild("AbortMultipartUpload").getChild("Days") != null) {
abortMultipartUpload.setExpirationDays(Integer.parseInt(ruleElem.getChild("AbortMultipartUpload").getChildText("Days")));
} else {
Date createdBeforeDate = DateUtil.parseIso8601Date(ruleElem.getChild("AbortMultipartUpload").getChildText("CreatedBeforeDate"));
abortMultipartUpload.setCreatedBeforeDate(createdBeforeDate);
}
rule.setAbortMultipartUpload(abortMultipartUpload);
}
List<Element> transitionElements = ruleElem.getChildren("Transition");
List<StorageTransition> storageTransitions = new ArrayList<StorageTransition>();
for (Element transitionElem : transitionElements) {
LifecycleRule.StorageTransition storageTransition = new LifecycleRule.StorageTransition();
if (transitionElem.getChild("Days") != null) {
storageTransition.setExpirationDays(Integer.parseInt(transitionElem.getChildText("Days")));
} else {
Date createdBeforeDate = DateUtil.parseIso8601Date(transitionElem.getChildText("CreatedBeforeDate"));
storageTransition.setCreatedBeforeDate(createdBeforeDate);
}
if (transitionElem.getChild("StorageClass") != null) {
storageTransition.setStorageClass(StorageClass.parse(transitionElem.getChildText("StorageClass")));
}
storageTransitions.add(storageTransition);
}
rule.setStorageTransition(storageTransitions);
lifecycleRules.add(rule);
}
return lifecycleRules;
} catch (JDOMParseException e) {
throw new ResponseParseException(e.getPartialDocument() + ": " + e.getMessage(), e);
} catch (Exception e) {
throw new ResponseParseException(e.getMessage(), e);
}
}
use of com.aliyun.oss.model.LifecycleRule.StorageTransition in project aliyun-oss-java-sdk by aliyun.
the class SetBucketLifecycleRequest method AddLifecycleRule.
public void AddLifecycleRule(LifecycleRule lifecycleRule) {
if (lifecycleRule == null) {
throw new IllegalArgumentException("lifecycleRule should not be null or empty.");
}
if (this.lifecycleRules.size() >= MAX_LIFECYCLE_RULE_LIMIT) {
throw new IllegalArgumentException("One bucket not allow exceed one thousand items of LifecycleRules.");
}
if (lifecycleRule.getId() != null && lifecycleRule.getId().length() > MAX_RULE_ID_LENGTH) {
throw new IllegalArgumentException("Length of lifecycle rule id exceeds max limit " + MAX_RULE_ID_LENGTH);
}
int expirationTimeFlag = lifecycleRule.hasExpirationTime() ? 1 : 0;
int expirationDaysFlag = lifecycleRule.hasExpirationDays() ? 1 : 0;
int createdBeforeDateFlag = lifecycleRule.hasCreatedBeforeDate() ? 1 : 0;
int flagSum = expirationTimeFlag + expirationDaysFlag + createdBeforeDateFlag;
if (flagSum > 1 || (flagSum == 0 && !lifecycleRule.hasAbortMultipartUpload() && !lifecycleRule.hasStorageTransition())) {
throw new IllegalArgumentException("Only one expiration property should be specified.");
}
if (lifecycleRule.getStatus() == LifecycleRule.RuleStatus.Unknown) {
throw new IllegalArgumentException("RuleStatus property should be specified with 'Enabled' or 'Disabled'.");
}
if (lifecycleRule.hasAbortMultipartUpload()) {
LifecycleRule.AbortMultipartUpload abortMultipartUpload = lifecycleRule.getAbortMultipartUpload();
expirationDaysFlag = abortMultipartUpload.hasExpirationDays() ? 1 : 0;
createdBeforeDateFlag = abortMultipartUpload.hasCreatedBeforeDate() ? 1 : 0;
flagSum = expirationDaysFlag + createdBeforeDateFlag;
if (flagSum != 1) {
throw new IllegalArgumentException("Only one expiration property for AbortMultipartUpload should be specified.");
}
}
if (lifecycleRule.hasStorageTransition()) {
for (StorageTransition storageTransition : lifecycleRule.getStorageTransition()) {
expirationDaysFlag = storageTransition.hasExpirationDays() ? 1 : 0;
createdBeforeDateFlag = storageTransition.hasCreatedBeforeDate() ? 1 : 0;
flagSum = expirationDaysFlag + createdBeforeDateFlag;
if (flagSum != 1) {
throw new IllegalArgumentException("Only one expiration property for StorageTransition should be specified.");
}
}
}
this.lifecycleRules.add(lifecycleRule);
}
use of com.aliyun.oss.model.LifecycleRule.StorageTransition in project feeyo-hlsserver by variflight.
the class OssUtil method updateExpirationDays.
/**
* 更新生命周期
*/
public void updateExpirationDays(int days) {
// 获取生命周期规则列表
List<LifecycleRule> list = ossClient.getBucketLifecycle(bucketName);
if (list.size() > 0) {
LifecycleRule lifecycleRule = list.get(0);
List<StorageTransition> storageTransitionList = lifecycleRule.getStorageTransition();
if (storageTransitionList.size() > 0) {
StorageTransition storageTransition = storageTransitionList.get(0);
// 如果原转换到归档存储的时间 与设定时间不一样,则更新
if (storageTransition.getExpirationDays() != days) {
storageTransition.setExpirationDays(days);
SetBucketLifecycleRequest setBucketLifecycleRequest = new SetBucketLifecycleRequest(bucketName);
setBucketLifecycleRequest.setLifecycleRules(list);
ossClient.setBucketLifecycle(setBucketLifecycleRequest);
LOGGER.info("oss life cycle set expir : " + days + " days");
}
}
}
}
Aggregations