use of com.aliyun.oss.model.LifecycleRule 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 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