Skip to content

Commit 23fa4e6

Browse files
authored
Merge pull request #230 from qiniu/features/add-restore-api
add restore api
2 parents 198ef1b + 1b0dc7f commit 23fa4e6

File tree

4 files changed

+109
-2
lines changed

4 files changed

+109
-2
lines changed

src/Qiniu/Storage/BucketManager.cs

Lines changed: 48 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -308,7 +308,7 @@ public HttpResult ChangeMime(string bucket, string key, string mimeType)
308308
/// </summary>
309309
/// <param name="bucket">空间名称</param>
310310
/// <param name="key">文件key</param>
311-
/// <param name="fileType">修改后的文件存储类型,0表示普通存储,1表示低频存储</param>
311+
/// <param name="fileType">修改后的文件存储类型,0表示普通存储,1表示低频存储,2表示归档存储,3表示深度归档存储</param>
312312
/// <returns>状态码为200时表示OK</returns>
313313
public HttpResult ChangeType(string bucket, string key, int fileType)
314314
{
@@ -342,6 +342,39 @@ public HttpResult ChangeType(string bucket, string key, int fileType)
342342
return result;
343343
}
344344

345+
public HttpResult RestoreAr(string bucket, string key, int freezeAfterDays)
346+
{
347+
HttpResult result = new HttpResult();
348+
349+
try
350+
{
351+
string restoreUrl = string.Format("{0}{1}",
352+
this.config.RsHost(this.mac.AccessKey, bucket),
353+
RestoreArOp(bucket, key, freezeAfterDays));
354+
string token = auth.CreateManageToken(restoreUrl);
355+
result = httpManager.Post(restoreUrl, token);
356+
}
357+
catch (QiniuException ex)
358+
{
359+
StringBuilder sb = new StringBuilder();
360+
sb.AppendFormat("[{0}] [restore] Error: ", DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.ffff"));
361+
Exception e = ex;
362+
while (e != null)
363+
{
364+
sb.Append(e.Message + " ");
365+
e = e.InnerException;
366+
}
367+
sb.AppendLine();
368+
369+
result.Code = ex.HttpResult.Code;
370+
result.RefCode = ex.HttpResult.Code;
371+
result.Text = ex.HttpResult.Text;
372+
result.RefText += sb.ToString();
373+
}
374+
375+
return result;
376+
}
377+
345378
/// <summary>
346379
/// 批处理
347380
/// </summary>
@@ -753,6 +786,20 @@ public string ChangeTypeOp(string bucket, string key, int fileType)
753786
fileType);
754787
}
755788

789+
/// <summary>
790+
/// 生成chtype操作字符串
791+
/// </summary>
792+
/// <param name="bucket">空间名称</param>
793+
/// <param name="key">文件key</param>
794+
/// <param name="freezeAfterDays">修改后文件类型</param>
795+
/// <returns>chtype操作字符串</returns>
796+
public string RestoreArOp(string bucket, string key, int freezeAfterDays)
797+
{
798+
return string.Format("/restoreAr/{0}/freezeAfterDays/{1}",
799+
Base64.UrlSafeBase64Encode(bucket, key),
800+
freezeAfterDays);
801+
}
802+
756803
/// <summary>
757804
/// 生成fetch操作字符串
758805
/// </summary>

src/Qiniu/Storage/PutPolicy.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -135,7 +135,7 @@ public class PutPolicy
135135
public int? DeleteAfterDays { get; set; }
136136

137137
/// <summary>
138-
/// [可选]文件的存储类型,默认为普通存储,设置为1为低频存储
138+
/// [可选]文件的存储类型,默认为普通存储,设置为:0 标准存储(默认),1 低频存储,2 归档存储,3 深度归档存储
139139
/// </summary>
140140
[JsonProperty("fileType", NullValueHandling = NullValueHandling.Ignore)]
141141
public int? FileType { get; set; }

src/QiniuTests/Storage/BucketManagerTests.cs

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ public void StatTest()
1414
{
1515
Config config = new Config();
1616
config.Zone = Zone.ZONE_CN_East;
17+
config.UseHttps = true;
1718
Mac mac = new Mac(AccessKey, SecretKey);
1819
BucketManager bucketManager = new BucketManager(mac, config);
1920
string key = "qiniu.png";
@@ -114,6 +115,64 @@ public void ChangeTypeTest()
114115
Console.WriteLine(ret.ToString());
115116
}
116117

118+
[Test]
119+
public void RestoreArArchiveTest()
120+
{
121+
Config config = new Config();
122+
config.Zone = Zone.ZONE_CN_East;
123+
Mac mac = new Mac(AccessKey, SecretKey);
124+
BucketManager bucketManager = new BucketManager(mac, config);
125+
126+
string newKey = "qiniu-archive-to-restore.png";
127+
HttpResult copyRet = bucketManager.Copy(Bucket, "qiniu.png", Bucket, newKey, true);
128+
if (copyRet.Code != (int)HttpCode.OK)
129+
{
130+
Assert.Fail("copy error: " + copyRet.ToString());
131+
}
132+
Console.WriteLine(copyRet.ToString());
133+
134+
HttpResult changeTypeRet = bucketManager.ChangeType(Bucket, newKey, 2);
135+
if (changeTypeRet.Code != (int)HttpCode.OK && !changeTypeRet.Text.Contains("already in line stat"))
136+
{
137+
Assert.Fail("change type error: " + changeTypeRet.ToString());
138+
}
139+
140+
HttpResult ret = bucketManager.RestoreAr(Bucket, newKey, 2);
141+
if (ret.Code != (int)HttpCode.OK && !ret.Text.Contains("already in line stat"))
142+
{
143+
Assert.Fail("change type error: " + ret.ToString());
144+
}
145+
}
146+
147+
[Test]
148+
public void RestoreArDeepArchiveTest()
149+
{
150+
Config config = new Config();
151+
config.Zone = Zone.ZONE_CN_East;
152+
Mac mac = new Mac(AccessKey, SecretKey);
153+
BucketManager bucketManager = new BucketManager(mac, config);
154+
155+
string newKey = "qiniu-deep-archive-to-restore.png";
156+
HttpResult copyRet = bucketManager.Copy(Bucket, "qiniu.png", Bucket, newKey, true);
157+
if (copyRet.Code != (int)HttpCode.OK)
158+
{
159+
Assert.Fail("copy error: " + copyRet.ToString());
160+
}
161+
Console.WriteLine(copyRet.ToString());
162+
163+
HttpResult changeTypeRet = bucketManager.ChangeType(Bucket, newKey, 3);
164+
if (changeTypeRet.Code != (int)HttpCode.OK && !changeTypeRet.Text.Contains("already in line stat"))
165+
{
166+
Assert.Fail("change type error: " + changeTypeRet.ToString());
167+
}
168+
169+
HttpResult ret = bucketManager.RestoreAr(Bucket, newKey, 2);
170+
if (ret.Code != (int)HttpCode.OK && !ret.Text.Contains("already in line stat"))
171+
{
172+
Assert.Fail("change type error: " + ret.ToString());
173+
}
174+
}
175+
117176
[Test]
118177
public void DeleteAfterDaysTest()
119178
{

src/QiniuTests/Storage/OperationManagerTests.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ public void PfopTest()
2222
string fops = string.Join(";", new string[] { avthumbMp4Fop, vframeJpgFop });
2323
Mac mac = new Mac(AccessKey, SecretKey);
2424
Config config = new Config();
25+
config.UseHttps = true;
2526
OperationManager manager = new OperationManager(mac, config);
2627
string pipeline = "sdktest";
2728
string notifyUrl = "http://api.example.com/qiniu/pfop/notify";

0 commit comments

Comments
 (0)