Skip to content

Commit 6fdd100

Browse files
authored
Merge pull request #526 from qiniu/ys_dev
add batch restore archive
2 parents 28bf849 + 73d4211 commit 6fdd100

File tree

6 files changed

+151
-8
lines changed

6 files changed

+151
-8
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,8 @@
11
# Changelog
2+
## 7.8.0 (2021-06-22)
3+
4+
## 增加
5+
* 支持批量解冻归档
26

37
## 7.7.0 (2021-05-28)
48

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,12 +12,12 @@
1212
<dependency>
1313
<groupId>com.qiniu</groupId>
1414
<artifactId>qiniu-java-sdk</artifactId>
15-
<version>[7.7.0, 7.7.99]</version>
15+
<version>[7.8.0, 7.8.99]</version>
1616
</dependency>
1717
```
1818
或者 Gradle:
1919
```groovy
20-
compile 'com.qiniu:qiniu-java-sdk:7.7.+'
20+
compile 'com.qiniu:qiniu-java-sdk:7.8.+'
2121
```
2222

2323
## 运行环境

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/common/Constants.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ public final class Constants {
99
/**
1010
* 版本号
1111
*/
12-
public static final String VERSION = "7.7.0";
12+
public static final String VERSION = "7.8.0";
1313
/**
1414
* 块大小,不能改变
1515
*/

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: 123 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1559,6 +1559,129 @@ public void testFile(TestConfig.TestFile file, BucketManager bucketManager) thro
15591559
});
15601560
}
15611561

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

0 commit comments

Comments
 (0)