/// <summary> /// 批量设置生存时间(超时会自动删除) /// </summary> /// <param name="request"></param> /// <returns></returns> public List <ExpireResultDto> SetExpireRange(SetExpireRangeParam request) { return(ToolCommon.GetResponse(() => { var zone = Core.Tools.GetZone(_aLiYunConfig, request.PersistentOps.Zone, () => ZoneEnum.HangZhou); var client = _aLiYunConfig.GetClient(zone); var bucket = Core.Tools.GetBucket(_aLiYunConfig, request.PersistentOps.Bucket); var setBucketLifecycleRequest = new SetBucketLifecycleRequest(bucket); List <ExpireResultDto> list = new List <ExpireResultDto>(); request.Keys.ForEach(key => { LifecycleRule lcr = new LifecycleRule() { ID = "delete " + key + " files", Prefix = key, Status = RuleStatus.Enabled, ExpriationDays = request.Expire }; setBucketLifecycleRequest.AddLifecycleRule(lcr); list.Add(new ExpireResultDto(true, key, "success")); }); client.SetBucketLifecycle(setBucketLifecycleRequest); //调整生命周期 return list; }, message => { List <ExpireResultDto> list = new List <ExpireResultDto>(); request.Keys.ForEach(key => { list.Add(new ExpireResultDto(false, key, message)); }); return list; })); }
/// <summary> /// 添加一条LifecycleRule。 /// </summary> /// <param name="lifecycleRule"></param> public void AddLifecycleRule(LifecycleRule lifecycleRule) { if (lifecycleRule == null) throw new ArgumentException("lifecycleRule should not be null or empty."); if (_lifecycleRules.Count >= OssUtils.LifecycleRuleLimit) throw new ArgumentException("One bucket not allow exceed one thousand item of LifecycleRules."); if ((!lifecycleRule.ExpirationTime.HasValue && !lifecycleRule.ExpriationDays.HasValue) || (lifecycleRule.ExpirationTime.HasValue && lifecycleRule.ExpriationDays.HasValue)) { throw new ArgumentException("Only one expiration property should be specified."); } _lifecycleRules.Add(lifecycleRule); }
/// <summary> /// Adds a LifeCycle rule /// </summary> /// <param name="lifecycleRule"></param> public void AddLifecycleRule(LifecycleRule lifecycleRule) { if (lifecycleRule == null) { throw new ArgumentException("lifecycleRule should not be null or empty."); } if (_lifecycleRules.Count >= OssUtils.LifecycleRuleLimit) { throw new ArgumentException("One bucket not allow exceed one thousand item of LifecycleRules."); } if (!lifecycleRule.Validate()) { throw new ArgumentException("Only one expiration property should be specified."); } _lifecycleRules.Add(lifecycleRule); }
/// <summary> /// 设置生存时间(超时会自动删除) /// </summary> /// <param name="request"></param> /// <returns></returns> public ExpireResultDto SetExpire(SetExpireParam request) { return(ToolCommon.GetResponse(() => { new SetExpireParamValidator().Validate(request).Check(HttpStatus.Err.Name); var zone = Core.Tools.GetZone(_aLiYunConfig, request.PersistentOps.Zone, () => ZoneEnum.HangZhou); var client = _aLiYunConfig.GetClient(zone); var bucket = Core.Tools.GetBucket(_aLiYunConfig, request.PersistentOps.Bucket); var setBucketLifecycleRequest = new SetBucketLifecycleRequest(bucket); LifecycleRule lcr = new LifecycleRule() { ID = "delete " + request.Key + " files", Prefix = request.Key, Status = RuleStatus.Enabled, ExpriationDays = request.Expire }; setBucketLifecycleRequest.AddLifecycleRule(lcr); client.SetBucketLifecycle(setBucketLifecycleRequest); //调整生命周期 return new ExpireResultDto(true, request.Key, "success"); }, message => new ExpireResultDto(false, request.Key, message))); }