Skip to content

Commit 36a2e60

Browse files
committed
add batch restore archive
1 parent 4f5f606 commit 36a2e60

File tree

3 files changed

+145
-5
lines changed

3 files changed

+145
-5
lines changed

build.gradle

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,11 @@ repositories {
1313
}
1414

1515
jacocoTestReport {
16-
reports {
17-
xml.enabled true
18-
html.enabled false
19-
}
20-
}
16+
reports {
17+
xml.enabled true
18+
html.enabled false
19+
}
20+
}
2121

2222
dependencies {
2323
implementation group: 'com.squareup.okhttp3', name: 'okhttp', version: '3.14.4'

src/main/java/com/qiniu/storage/BucketManager.java

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1207,6 +1207,22 @@ public BatchOperations addDeleteAfterDaysOps(String bucket, int days, String...
12071207
return this;
12081208
}
12091209

1210+
/**
1211+
* 添加解冻归档存储指令
1212+
*
1213+
* @param bucket keys 所在 bucket
1214+
* @param freezeAfterDays 解冻有效时长,取值范围 1~7
1215+
* @param keys keys
1216+
* @return BatchOperations
1217+
*/
1218+
public BatchOperations addRestoreArchiveOps(String bucket, int freezeAfterDays, String... keys) {
1219+
for (String key : keys) {
1220+
ops.add(String.format("restoreAr/%s/freezeAfterDays/%d", encodedEntry(bucket, key), freezeAfterDays));
1221+
}
1222+
setExecBucket(bucket);
1223+
return this;
1224+
}
1225+
12101226
public byte[] toBody() {
12111227
String body = StringUtils.join(ops, "&op=", "op=");
12121228
return StringUtils.utf8Bytes(body);

src/test/java/test/com/qiniu/storage/BucketTest.java

Lines changed: 124 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -236,6 +236,7 @@ public void testStat() throws Exception {
236236
public void testFile(TestConfig.TestFile file, BucketManager bucketManager) throws IOException {
237237
String bucket = file.getBucketName();
238238
String key = file.getKey();
239+
key = "12344";
239240
try {
240241
FileInfo info = bucketManager.stat(bucket, key);
241242
Assert.assertNotNull(info.hash);
@@ -1559,6 +1560,129 @@ public void testFile(TestConfig.TestFile file, BucketManager bucketManager) thro
15591560
});
15601561
}
15611562

1563+
@Test
1564+
public void testBatchRestoreArchive() throws Exception {
1565+
1566+
testFileWithHandler(new TestFileHandler() {
1567+
@Override
1568+
public void testFile(TestConfig.TestFile file, BucketManager bucketManager) throws IOException {
1569+
String bucket = file.getBucketName();
1570+
String key = file.getKey();
1571+
String keyToTestPrefix = "batch_restore_archive";
1572+
1573+
BucketManager.BatchOperations deleteOps = new BucketManager.BatchOperations();
1574+
try {
1575+
List<String> keys = new ArrayList<>();
1576+
for (int i = 0; i < 5; i++) {
1577+
keys.add(keyToTestPrefix + "_" + i);
1578+
}
1579+
1580+
for (String keyToTest : keys) {
1581+
// if stat, delete
1582+
try {
1583+
Response resp = bucketManager.statResponse(bucket, keyToTest);
1584+
if (resp.statusCode == 200) bucketManager.delete(bucket, keyToTest);
1585+
} catch (QiniuException ex) {
1586+
System.out.println("file " + keyToTest + " not exists, ok.");
1587+
}
1588+
}
1589+
1590+
// copy and changeType to Archive
1591+
BucketManager.BatchOperations copyOps = new BucketManager.BatchOperations();
1592+
BucketManager.BatchOperations copyAfterArchiveOps = new BucketManager.BatchOperations();
1593+
BucketManager.BatchOperations changeTypeOps = new BucketManager.BatchOperations();
1594+
BucketManager.BatchOperations restoreArchiveOps = new BucketManager.BatchOperations();
1595+
for (String keyToTest : keys) {
1596+
copyOps.addCopyOp(bucket, key, bucket, keyToTest);
1597+
copyAfterArchiveOps.addCopyOp(bucket, keyToTest, bucket, keyToTest + "_copy");
1598+
deleteOps.addDeleteOp(bucket, keyToTest, keyToTest + "_copy");
1599+
changeTypeOps.addChangeTypeOps(bucket, StorageType.Archive, keyToTest);
1600+
restoreArchiveOps.addRestoreArchiveOps(bucket, 1, keyToTest);
1601+
}
1602+
Response copyResponse = bucketManager.batch(copyOps);
1603+
Assert.assertEquals(200, copyResponse.statusCode);
1604+
1605+
Response changeTypeResponse = bucketManager.batch(changeTypeOps);
1606+
Assert.assertEquals(200, changeTypeResponse.statusCode);
1607+
1608+
// 验证归档不可 copy
1609+
try {
1610+
Response copyAfterArchiveResponse = bucketManager.batch(copyAfterArchiveOps);
1611+
String bodyString = copyAfterArchiveResponse.bodyString();
1612+
Assert.assertNotEquals(200, "batch copy can't be success" + bodyString);
1613+
} catch (QiniuException ex) {
1614+
Assert.assertEquals(400, ex.response.statusCode);
1615+
System.out.println(ex.response.bodyString());
1616+
}
1617+
1618+
// restoreArchive
1619+
Response restoreResponse = bucketManager.batch(restoreArchiveOps);
1620+
String bodyString = restoreResponse.bodyString();
1621+
System.out.println(bodyString);
1622+
Assert.assertEquals(200, restoreResponse.statusCode);
1623+
1624+
//test for 400 Bad Request {"error":"invalid freeze after days"}
1625+
try {
1626+
restoreResponse = bucketManager.batch(restoreArchiveOps);
1627+
bodyString = restoreResponse.bodyString();
1628+
System.out.println(bodyString);
1629+
} catch (QiniuException ex) {
1630+
Assert.assertEquals(400, ex.response.statusCode);
1631+
System.out.println(ex.response.bodyString());
1632+
}
1633+
1634+
long checkStart = new Date().getTime();
1635+
boolean shouldCheck = true;
1636+
boolean success = false;
1637+
while (shouldCheck) {
1638+
// 验证解归档可 copy
1639+
try {
1640+
Response copyAfterArchiveResponse = bucketManager.batch(copyAfterArchiveOps);
1641+
bodyString = copyAfterArchiveResponse.bodyString();
1642+
System.out.println(bodyString);
1643+
// 可以 copy 但文件已存在
1644+
if (bodyString.contains("\"code\":614")) {
1645+
success = true;
1646+
break;
1647+
}
1648+
} catch (QiniuException ex) {
1649+
System.out.println(ex.response.bodyString());
1650+
if (ex.response.statusCode == 400) {
1651+
success = true;
1652+
break;
1653+
}
1654+
}
1655+
1656+
long current = new Date().getTime();
1657+
if (current - checkStart > 1000 * 60 * 5.5) {
1658+
shouldCheck = false;
1659+
}
1660+
1661+
try {
1662+
Thread.sleep(1000 * 10);
1663+
} catch (InterruptedException e) {
1664+
e.printStackTrace();
1665+
}
1666+
}
1667+
1668+
Assert.assertTrue("can copy after restore archive", success);
1669+
} catch (QiniuException e) {
1670+
Assert.fail(bucket + ":" + key + " > " + keyToTestPrefix + " >> " + e.response.toString());
1671+
} finally {
1672+
try {
1673+
Response response = bucketManager.batch(deleteOps);
1674+
String bodyString = response.bodyString();
1675+
System.out.println(bodyString);
1676+
Assert.assertTrue(bodyString, response.statusCode == 200 || response.statusCode == 298);
1677+
} catch (QiniuException ex) {
1678+
Assert.assertEquals(400, ex.response.statusCode);
1679+
System.out.println(ex.response.bodyString());
1680+
}
1681+
}
1682+
}
1683+
});
1684+
}
1685+
15621686
/**
15631687
* 测试noIndexPage
15641688
*

0 commit comments

Comments
 (0)