Search in sources :

Example 6 with LifecycleRule

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);
    }
}
Also used : JDOMParseException(org.jdom.input.JDOMParseException) Element(org.jdom.Element) ArrayList(java.util.ArrayList) StorageTransition(com.aliyun.oss.model.LifecycleRule.StorageTransition) StorageTransition(com.aliyun.oss.model.LifecycleRule.StorageTransition) LifecycleRule(com.aliyun.oss.model.LifecycleRule) Date(java.util.Date) ParseException(java.text.ParseException) JDOMParseException(org.jdom.input.JDOMParseException) ResponseParseException(com.aliyun.oss.common.parser.ResponseParseException) ResponseParseException(com.aliyun.oss.common.parser.ResponseParseException)

Example 7 with LifecycleRule

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");
            }
        }
    }
}
Also used : SetBucketLifecycleRequest(com.aliyun.oss.model.SetBucketLifecycleRequest) StorageTransition(com.aliyun.oss.model.LifecycleRule.StorageTransition) LifecycleRule(com.aliyun.oss.model.LifecycleRule)

Aggregations

LifecycleRule (com.aliyun.oss.model.LifecycleRule)7 SetBucketLifecycleRequest (com.aliyun.oss.model.SetBucketLifecycleRequest)6 OSSException (com.aliyun.oss.OSSException)4 StorageTransition (com.aliyun.oss.model.LifecycleRule.StorageTransition)4 ArrayList (java.util.ArrayList)4 Test (org.junit.Test)3 TestUtils.genRandomString (com.aliyun.oss.integrationtests.TestUtils.genRandomString)2 AbortMultipartUpload (com.aliyun.oss.model.LifecycleRule.AbortMultipartUpload)2 ParseException (java.text.ParseException)2 OSS (com.aliyun.oss.OSS)1 OSSClient (com.aliyun.oss.OSSClient)1 OSSClientBuilder (com.aliyun.oss.OSSClientBuilder)1 ResponseParseException (com.aliyun.oss.common.parser.ResponseParseException)1 AccessControlList (com.aliyun.oss.model.AccessControlList)1 BucketLoggingResult (com.aliyun.oss.model.BucketLoggingResult)1 BucketReferer (com.aliyun.oss.model.BucketReferer)1 CannedAccessControlList (com.aliyun.oss.model.CannedAccessControlList)1 Grant (com.aliyun.oss.model.Grant)1 ObjectListing (com.aliyun.oss.model.ObjectListing)1 SetBucketCORSRequest (com.aliyun.oss.model.SetBucketCORSRequest)1