Skip to content

Commit

Permalink
object lifecycle add archiveIR & fix batch rs host (#258)
Browse files Browse the repository at this point in the history
  • Loading branch information
lihsai0 authored Dec 8, 2023
1 parent 5c849f7 commit 2e55079
Show file tree
Hide file tree
Showing 5 changed files with 80 additions and 19 deletions.
21 changes: 15 additions & 6 deletions src/Qiniu/Storage/BatchInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ public class BatchData
/// 1 低频存储
/// 2 归档存储
/// 3 深度归档存储
/// 4 归档直读存储
/// </summary>
[JsonProperty("type", NullValueHandling = NullValueHandling.Ignore)]
public int FileType { get; set; }
Expand Down Expand Up @@ -99,24 +100,32 @@ public class BatchData

/// <summary>
/// 文件生命周期中转为低频存储的日期,Unix 时间戳格式。
/// 文件在设置过期时间后才会返回该字段
/// 历史文件过期仍会自动删除,但不会返回该字段,重新设置文件过期时间可使历史文件返回该字段
/// 文件在设置低频存储转换时间后才会返回该字段
/// 历史文件到期仍会自动转换,但不会返回该字段,重新设置文件转换时间可使历史文件返回该字段
/// </summary>
[JsonProperty("TransitionToIA", NullValueHandling = NullValueHandling.Ignore)]
public int TransitionToIa { get; set; }

/// <summary>
/// 文件生命周期中转为归档直读存储的日期,Unix 时间戳格式。
/// 文件在设置归档直读存储转换时间后才会返回该字段。
/// 历史文件到期仍会自动转换,但不会返回该字段,重新设置文件转换时间可使历史文件返回该字段。
/// </summary>
[JsonProperty("transitionToArchiveIR", NullValueHandling = NullValueHandling.Ignore)]
public int TransitionToArchiveIr { get; set; }

/// <summary>
/// 文件生命周期中转为归档存储的日期,Unix 时间戳格式。
/// 文件在设置过期时间后才会返回该字段
/// 历史文件过期仍会自动删除,但不会返回该字段,重新设置文件过期时间可使历史文件返回该字段
/// 文件在设置归档存储转换时间后才会返回该字段
/// 历史文件到期仍会自动转换,但不会返回该字段,重新设置文件转换时间可使历史文件返回该字段
/// </summary>
[JsonProperty("transitionToARCHIVE", NullValueHandling = NullValueHandling.Ignore)]
public int TransitionToArchive { get; set; }

/// <summary>
/// 文件生命周期中转为深度归档存储的日期,Unix 时间戳格式。
/// 文件在设置过期时间后才会返回该字段
/// 历史文件过期仍会自动删除,但不会返回该字段,重新设置文件过期时间可使历史文件返回该字段
/// 文件在设置深度归档存储转换时间后才会返回该字段
/// 历史文件到期仍会自动转换,但不会返回该字段,重新设置文件转换时间可使历史文件返回该字段
/// </summary>
[JsonProperty("transitionToDeepArchive", NullValueHandling = NullValueHandling.Ignore)]
public int TransitionToDeepArchive { get; set; }
Expand Down
61 changes: 49 additions & 12 deletions src/Qiniu/Storage/BucketManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -338,7 +338,14 @@ public HttpResult ChangeStatus(string bucket, string key, int status)
/// </summary>
/// <param name="bucket">空间名称</param>
/// <param name="key">文件key</param>
/// <param name="fileType">修改后的文件存储类型,0表示普通存储,1表示低频存储,2表示归档存储,3表示深度归档存储</param>
/// <param name="fileType">
/// 修改后的文件存储类型:
/// 0 表示普通存储;
/// 1 表示低频存储;
/// 2 表示归档存储;
/// 3 表示深度归档存储;
/// 4 表示归档直读存储;
/// </param>
/// <returns>状态码为200时表示OK</returns>
public HttpResult ChangeType(string bucket, string key, int fileType)
{
Expand Down Expand Up @@ -414,11 +421,36 @@ private BatchResult Batch(string batchOps)
{
BatchResult result = new BatchResult();

string bucket = "";
foreach (string op in batchOps.Split('&'))
{
string[] segments = op.Split('/');
if (segments.Length < 3)
{
continue;
}

string entry = Encoding.UTF8.GetString(Base64.UrlsafeBase64Decode(segments[2]));
bucket = entry.Split(':')[0];

if (bucket.Length > 0)
{
break;
}
}

if (bucket.Length == 0)
{
result.Code = (int)HttpCode.INVALID_ARGUMENT;
result.RefCode = (int)HttpCode.INVALID_ARGUMENT;
result.Text = "{\"error\":\"bucket is Empty\"}";
result.RefText = "bucket is Empty";
return result;
}

try
{
string scheme = this.config.UseHttps ? "https://" : "http://";
string rsHost = string.Format("{0}{1}", scheme, Config.DefaultRsHost);
string batchUrl = rsHost + "/batch";
string batchUrl = string.Format("{0}/batch", this.config.RsHost(this.mac.AccessKey, bucket));

HttpResult hr = httpManager.PostForm(batchUrl, null, batchOps, auth);
result.Shadow(hr);
Expand Down Expand Up @@ -707,7 +739,8 @@ public HttpResult SetObjectLifecycle(
int toIaAfterDays = 0,
int toArchiveAfterDays = 0,
int toDeepArchiveAfterDays = 0,
int deleteAfterDays = 0
int deleteAfterDays = 0,
int toArchiveIrAfterDays = 0
)
{
return SetObjectLifecycle(
Expand All @@ -717,7 +750,8 @@ public HttpResult SetObjectLifecycle(
toIaAfterDays,
toArchiveAfterDays,
toDeepArchiveAfterDays,
deleteAfterDays
deleteAfterDays,
toArchiveIrAfterDays
);
}

Expand All @@ -728,6 +762,7 @@ public HttpResult SetObjectLifecycle(
/// <param name="key">文件key</param>
/// <param name="cond">匹配条件,只有条件匹配才会设置成功,目前支持:hash、mime、fsize、putTime</param>
/// <param name="toIaAfterDays">多少天后将文件转为低频存储,设置为 -1 表示取消已设置的转低频存储的生命周期规则,0 表示不修改转低频生命周期规则。</param>
/// <param name="toArchiveIrAfterDays">多少天后将文件转为归档直读存储,设置为 -1 表示取消已设置的转归档直读存储的生命周期规则,0 表示不修改转归档直读生命周期规则。</param>
/// <param name="toArchiveAfterDays">多少天后将文件转为归档存储,设置为 -1 表示取消已设置的转归档存储的生命周期规则,0 表示不修改转归档生命周期规则。</param>
/// <param name="toDeepArchiveAfterDays">多少天后将文件转为深度归档存储,设置为 -1 表示取消已设置的转深度归档存储的生命周期规则,0 表示不修改转深度归档生命周期规则。</param>
/// <param name="deleteAfterDays">多少天后将文件删除,设置为 -1 表示取消已设置的删除存储的生命周期规则,0 表示不修改删除存储的生命周期规则。</param>
Expand All @@ -739,15 +774,16 @@ public HttpResult SetObjectLifecycle(
int toIaAfterDays = 0,
int toArchiveAfterDays = 0,
int toDeepArchiveAfterDays = 0,
int deleteAfterDays = 0
int deleteAfterDays = 0,
int toArchiveIrAfterDays = 0
)
{
HttpResult result = new HttpResult();

try
{
string updateUrl = string.Format("{0}{1}", this.config.RsHost(this.mac.AccessKey, bucket),
SetObjectLifecycleOp(bucket, key, cond, toIaAfterDays, toArchiveAfterDays, toDeepArchiveAfterDays, deleteAfterDays));
SetObjectLifecycleOp(bucket, key, cond, toIaAfterDays, toArchiveAfterDays, toDeepArchiveAfterDays, deleteAfterDays, toArchiveIrAfterDays));
StringDictionary headers = new StringDictionary
{
{"Content-Type", ContentType.WWW_FORM_URLENC}
Expand Down Expand Up @@ -954,6 +990,7 @@ public string DeleteAfterDaysOp(string bucket, string key, int deleteAfterDays)
/// <param name="key">文件key</param>
/// <param name="cond">匹配条件,只有条件匹配才会设置成功,目前支持:hash、mime、fsize、putTime</param>
/// <param name="toIaAfterDays">多少天后将文件转为低频存储,设置为 -1 表示取消已设置的转低频存储的生命周期规则,0 表示不修改转低频生命周期规则。</param>
/// <param name="toArchiveIrAfterDays">多少天后将文件转为归档直读存储,设置为 -1 表示取消已设置的转归档直读存储的生命周期规则,0 表示不修改转归档直读生命周期规则。</param>
/// <param name="toArchiveAfterDays">多少天后将文件转为归档存储,设置为 -1 表示取消已设置的转归档存储的生命周期规则,0 表示不修改转归档生命周期规则。</param>
/// <param name="toDeepArchiveAfterDays">多少天后将文件转为深度归档存储,设置为 -1 表示取消已设置的转深度归档存储的生命周期规则,0 表示不修改转深度归档生命周期规则。</param>
/// <param name="deleteAfterDays">多少天后将文件删除,设置为 -1 表示取消已设置的删除存储的生命周期规则,0 表示不修改删除存储的生命周期规则。</param>
Expand All @@ -965,13 +1002,14 @@ public string SetObjectLifecycleOp(
int toIaAfterDays = 0,
int toArchiveAfterDays = 0,
int toDeepArchiveAfterDays = 0,
int deleteAfterDays = 0
int deleteAfterDays = 0,
int toArchiveIrAfterDays = 0
)
{
string entry = Base64.UrlSafeBase64Encode(bucket, key);
string result = string.Format(
"/lifecycle/{0}/toIAAfterDays/{1}/toArchiveAfterDays/{2}/toDeepArchiveAfterDays/{3}/deleteAfterDays/{4}",
entry, toIaAfterDays, toArchiveAfterDays, toDeepArchiveAfterDays, deleteAfterDays);
"/lifecycle/{0}/toIAAfterDays/{1}/toArchiveIRAfterDays/{2}/toArchiveAfterDays/{3}/toDeepArchiveAfterDays/{4}/deleteAfterDays/{5}",
entry, toIaAfterDays, toArchiveIrAfterDays, toArchiveAfterDays, toDeepArchiveAfterDays, deleteAfterDays);

if (cond != null)
{
Expand All @@ -980,7 +1018,6 @@ public string SetObjectLifecycleOp(

result += "/cond/" + Base64.UrlSafeBase64Encode(query);
}
Console.WriteLine(result);

return result;
}
Expand Down
11 changes: 10 additions & 1 deletion src/Qiniu/Storage/FileInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ public class FileInfo
/// 1 低频存储
/// 2 归档存储
/// 3 深度归档存储
/// 4 归档直读存储
/// </summary>
[JsonProperty("type")]
public int FileType { get; set; }
Expand Down Expand Up @@ -79,9 +80,17 @@ public class FileInfo
/// 文件在设置过期时间后才会返回该字段。
/// 历史文件过期仍会自动删除,但不会返回该字段,重新设置文件过期时间可使历史文件返回该字段。
/// </summary>
[JsonProperty("TransitionToIA", NullValueHandling = NullValueHandling.Ignore)]
[JsonProperty("transitionToIA", NullValueHandling = NullValueHandling.Ignore)]
public int TransitionToIa { get; set; }

/// <summary>
/// 文件生命周期中转为归档直读存储的日期,Unix 时间戳格式。
/// 文件在设置过期时间后才会返回该字段。
/// 历史文件过期仍会自动删除,但不会返回该字段,重新设置文件过期时间可使历史文件返回该字段。
/// </summary>
[JsonProperty("transitionToArchiveIR", NullValueHandling = NullValueHandling.Ignore)]
public int TransitionToArchiveIr { get; set; }

/// <summary>
/// 文件生命周期中转为归档存储的日期,Unix 时间戳格式。
/// 文件在设置过期时间后才会返回该字段。
Expand Down
1 change: 1 addition & 0 deletions src/Qiniu/Storage/ListItem.cs
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ public class ListItem
/// 1 低频存储
/// 2 归档存储
/// 3 深度归档存储
/// 4 归档直读存储
/// </summary>
[JsonProperty("type")]
public int FileType { get; set; }
Expand Down
5 changes: 5 additions & 0 deletions src/QiniuTests/Storage/BucketManagerTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -268,6 +268,8 @@ public void SetObjectLifecycleTest()
20,
30,
40);
// 40,
// 15);
if (ret.Code != (int)HttpCode.OK)
{
Assert.Fail("deleteAfterDays error: " + ret.ToString());
Expand All @@ -279,6 +281,7 @@ public void SetObjectLifecycleTest()
Assert.Fail("stat error: " + statRet.ToString());
}
Assert.True(statRet.Result.TransitionToIa > 0);
// Assert.True(statRet.Result.TransitionToArchiveIr > 0);
Assert.True(statRet.Result.TransitionToArchive > 0);
Assert.True(statRet.Result.TransitionToDeepArchive > 0);
Assert.True(statRet.Result.Expiration > 0);
Expand Down Expand Up @@ -319,6 +322,8 @@ public void SetObjectLifecycleCondTest()
20,
30,
40);
// 40,
// 15);
if (ret.Code != (int)HttpCode.OK)
{
Assert.Fail("deleteAfterDays error: " + ret.ToString());
Expand Down

0 comments on commit 2e55079

Please sign in to comment.