|
4 | 4 | using Qiniu.Util; |
5 | 5 | using System.Collections.Generic; |
6 | 6 | using System.Collections.Specialized; |
| 7 | +using System.Linq; |
7 | 8 |
|
8 | 9 | namespace Qiniu.Storage |
9 | 10 | { |
@@ -633,7 +634,7 @@ public ListResult ListFiles(string bucket, string prefix, string marker, int lim |
633 | 634 | } |
634 | 635 |
|
635 | 636 | /// <summary> |
636 | | - /// 更新文件生命周期 |
| 637 | + /// 更新文件删除生命周期 |
637 | 638 | /// </summary> |
638 | 639 | /// <param name="bucket">空间名称</param> |
639 | 640 | /// <param name="key">文件key</param> |
@@ -670,6 +671,81 @@ public HttpResult DeleteAfterDays(string bucket, string key, int deleteAfterDays |
670 | 671 | return result; |
671 | 672 | } |
672 | 673 |
|
| 674 | + public HttpResult SetObjectLifecycle( |
| 675 | + string bucket, |
| 676 | + string key, |
| 677 | + int toIaAfterDays = 0, |
| 678 | + int toArchiveAfterDays = 0, |
| 679 | + int toDeepArchiveAfterDays = 0, |
| 680 | + int deleteAfterDays = 0 |
| 681 | + ) |
| 682 | + { |
| 683 | + return SetObjectLifecycle( |
| 684 | + bucket, |
| 685 | + key, |
| 686 | + null, |
| 687 | + toIaAfterDays, |
| 688 | + toArchiveAfterDays, |
| 689 | + toDeepArchiveAfterDays, |
| 690 | + deleteAfterDays |
| 691 | + ); |
| 692 | + } |
| 693 | + |
| 694 | + /// <summary> |
| 695 | + /// 更新文件生命周期 |
| 696 | + /// </summary> |
| 697 | + /// <param name="bucket">空间名称</param> |
| 698 | + /// <param name="key">文件key</param> |
| 699 | + /// <param name="cond">匹配条件,只有条件匹配才会设置成功,目前支持:hash、mime、fsize、putTime</param> |
| 700 | + /// <param name="toIaAfterDays">多少天后将文件转为低频存储,设置为 -1 表示取消已设置的转低频存储的生命周期规则,0 表示不修改转低频生命周期规则。</param> |
| 701 | + /// <param name="toArchiveAfterDays">多少天后将文件转为归档存储,设置为 -1 表示取消已设置的转归档存储的生命周期规则,0 表示不修改转归档生命周期规则。</param> |
| 702 | + /// <param name="toDeepArchiveAfterDays">多少天后将文件转为深度归档存储,设置为 -1 表示取消已设置的转深度归档存储的生命周期规则,0 表示不修改转深度归档生命周期规则。</param> |
| 703 | + /// <param name="deleteAfterDays">多少天后将文件删除,设置为 -1 表示取消已设置的删除存储的生命周期规则,0 表示不修改删除存储的生命周期规则。</param> |
| 704 | + /// <returns>状态码为200时表示OK</returns> |
| 705 | + public HttpResult SetObjectLifecycle( |
| 706 | + string bucket, |
| 707 | + string key, |
| 708 | + Dictionary<string, string> cond = null, |
| 709 | + int toIaAfterDays = 0, |
| 710 | + int toArchiveAfterDays = 0, |
| 711 | + int toDeepArchiveAfterDays = 0, |
| 712 | + int deleteAfterDays = 0 |
| 713 | + ) |
| 714 | + { |
| 715 | + HttpResult result = new HttpResult(); |
| 716 | + |
| 717 | + try |
| 718 | + { |
| 719 | + string updateUrl = string.Format("{0}{1}", this.config.RsHost(this.mac.AccessKey, bucket), |
| 720 | + SetObjectLifecycleOp(bucket, key, cond, toIaAfterDays, toArchiveAfterDays, toDeepArchiveAfterDays, deleteAfterDays)); |
| 721 | + StringDictionary headers = new StringDictionary |
| 722 | + { |
| 723 | + {"Content-Type", ContentType.WWW_FORM_URLENC} |
| 724 | + }; |
| 725 | + string token = auth.CreateManageTokenV2("POST", updateUrl, headers); |
| 726 | + result = httpManager.Post(updateUrl, headers, token); |
| 727 | + } |
| 728 | + catch (QiniuException ex) |
| 729 | + { |
| 730 | + StringBuilder sb = new StringBuilder(); |
| 731 | + sb.AppendFormat("[{0}] [setObjectLifecycle] Error: ", DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.ffff")); |
| 732 | + Exception e = ex; |
| 733 | + while (e != null) |
| 734 | + { |
| 735 | + sb.Append(e.Message + " "); |
| 736 | + e = e.InnerException; |
| 737 | + } |
| 738 | + sb.AppendLine(); |
| 739 | + |
| 740 | + result.Code = ex.HttpResult.Code; |
| 741 | + result.RefCode = ex.HttpResult.Code; |
| 742 | + result.Text = ex.HttpResult.Text; |
| 743 | + result.RefText += sb.ToString(); |
| 744 | + } |
| 745 | + |
| 746 | + return result; |
| 747 | + } |
| 748 | + |
673 | 749 | /// <summary> |
674 | 750 | /// 生成stat操作字符串 |
675 | 751 | /// </summary> |
@@ -835,5 +911,42 @@ public string DeleteAfterDaysOp(string bucket, string key, int deleteAfterDays) |
835 | 911 | Base64.UrlSafeBase64Encode(bucket, key), deleteAfterDays); |
836 | 912 | } |
837 | 913 |
|
| 914 | + /// <summary> |
| 915 | + /// 生成 setObjectLifecycle 操作字符串 |
| 916 | + /// </summary> |
| 917 | + /// <param name="bucket">空间名称</param> |
| 918 | + /// <param name="key">文件key</param> |
| 919 | + /// <param name="cond">匹配条件,只有条件匹配才会设置成功,目前支持:hash、mime、fsize、putTime</param> |
| 920 | + /// <param name="toIaAfterDays">多少天后将文件转为低频存储,设置为 -1 表示取消已设置的转低频存储的生命周期规则,0 表示不修改转低频生命周期规则。</param> |
| 921 | + /// <param name="toArchiveAfterDays">多少天后将文件转为归档存储,设置为 -1 表示取消已设置的转归档存储的生命周期规则,0 表示不修改转归档生命周期规则。</param> |
| 922 | + /// <param name="toDeepArchiveAfterDays">多少天后将文件转为深度归档存储,设置为 -1 表示取消已设置的转深度归档存储的生命周期规则,0 表示不修改转深度归档生命周期规则。</param> |
| 923 | + /// <param name="deleteAfterDays">多少天后将文件删除,设置为 -1 表示取消已设置的删除存储的生命周期规则,0 表示不修改删除存储的生命周期规则。</param> |
| 924 | + /// <returns>updateLifecycle操作字符串</returns> |
| 925 | + public string SetObjectLifecycleOp( |
| 926 | + string bucket, |
| 927 | + string key, |
| 928 | + Dictionary<string, string> cond = null, |
| 929 | + int toIaAfterDays = 0, |
| 930 | + int toArchiveAfterDays = 0, |
| 931 | + int toDeepArchiveAfterDays = 0, |
| 932 | + int deleteAfterDays = 0 |
| 933 | + ) |
| 934 | + { |
| 935 | + string entry = Base64.UrlSafeBase64Encode(bucket, key); |
| 936 | + string result = string.Format( |
| 937 | + "/lifecycle/{0}/toIAAfterDays/{1}/toArchiveAfterDays/{2}/toDeepArchiveAfterDays/{3}/deleteAfterDays/{4}", |
| 938 | + entry, toIaAfterDays, toArchiveAfterDays, toDeepArchiveAfterDays, deleteAfterDays); |
| 939 | + |
| 940 | + if (cond != null) |
| 941 | + { |
| 942 | + string query = string.Join("&", |
| 943 | + cond.Keys.Select(k => k + "=" + cond[k])); |
| 944 | + |
| 945 | + result += "/cond/" + Base64.UrlSafeBase64Encode(query); |
| 946 | + } |
| 947 | + Console.WriteLine(result); |
| 948 | + |
| 949 | + return result; |
| 950 | + } |
838 | 951 | } |
839 | 952 | } |
0 commit comments