Skip to content

Commit df5bce2

Browse files
committed
update RestoreArchive process to use new api.
1 parent a483998 commit df5bce2

File tree

8 files changed

+72
-62
lines changed

8 files changed

+72
-62
lines changed

docs/restorear.md

-2
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@ ak=
1717
sk=
1818
bucket=
1919
days=
20-
cond.[]=
2120
```
2221
|参数名|参数值及类型 | 含义|
2322
|-----|-------|-----|
@@ -26,7 +25,6 @@ cond.[]=
2625
|bucket| 字符串| 操作的资源原空间,当数据源为 qiniu 时无需再设置|
2726
|indexes|字符串| 设置输入行中 key 字段的下标(有默认值),参考[数据源 indexes 设置](datasource.md#1-公共参数)|
2827
|days| 整型数字| 设置解冻的有效期为 days(单位天数),范围 1-7 天|
29-
|cond.[]| 字符串| 可以设置一些操作时的 condition,cond 当前支持设置 hash、mime、fsize、putTime 条件,只有条件匹配才会执行修改操作,如 cond.mime=text/plain|
3028

3129
运行参数:`-config=config.txt`
3230

nigel/type_success_1.txt

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
{"code":200}
2+
7026814.ts {"code":200}

nigel/type_success_2.txt

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
7026814_转码后.ts {"code":200}

nigel/type_success_3.txt

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
UAA-4hndfVc5V6DJX0EvslAUBBI=/llmf1NC-2WRv3wJdHjFs0_szQ_vU {"code":200}

nigel/type_success_4.txt

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
upload_c1b9d660_e284_48c7_b180_2cf4d2debf13_00000135.tmp {"code":200}

nigel/type_success_5.txt

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
视频数据.mp4 {"code":200}

src/main/java/com/qiniu/entry/QSuitsEntry.java

+12-13
Original file line numberDiff line numberDiff line change
@@ -607,19 +607,18 @@ private ILineProcess<Map<String, String>> getChangeType(boolean single) throws I
607607
private ILineProcess<Map<String, String>> getRestoreArchive(boolean single) throws IOException {
608608
String days = entryParam.getValue("days", "1").trim();
609609
ParamsUtils.checked(days, "days", "[1-7]");
610-
StringBuilder condition = new StringBuilder();
611-
for (Map.Entry<String, String> entry : entryParam.getParamsMap().entrySet()) {
612-
if (entry.getKey().startsWith("cond.")) {
613-
if (condition.length() > 0) {
614-
condition.append(entry.getKey().substring(5)).append("=").append(entry.getValue().trim()).append("&");
615-
} else {
616-
condition.append(entry.getKey().substring(5)).append("=").append(entry.getValue().trim());
617-
}
618-
}
619-
}
620-
return single ? new RestoreArchive(qiniuAccessKey, qiniuSecretKey, getQiniuConfig(), bucket, Integer.parseInt(days),
621-
condition.toString()) : new RestoreArchive(qiniuAccessKey, qiniuSecretKey, getQiniuConfig(), bucket,
622-
Integer.parseInt(days), condition.toString(), savePath);
610+
// StringBuilder condition = new StringBuilder();
611+
// for (Map.Entry<String, String> entry : entryParam.getParamsMap().entrySet()) {
612+
// if (entry.getKey().startsWith("cond.")) {
613+
// if (condition.length() > 0) {
614+
// condition.append(entry.getKey().substring(5)).append("=").append(entry.getValue().trim()).append("&");
615+
// } else {
616+
// condition.append(entry.getKey().substring(5)).append("=").append(entry.getValue().trim());
617+
// }
618+
// }
619+
// }
620+
return single ? new RestoreArchive(qiniuAccessKey, qiniuSecretKey, getQiniuConfig(), bucket, Integer.parseInt(days))
621+
: new RestoreArchive(qiniuAccessKey, qiniuSecretKey, getQiniuConfig(), bucket, Integer.parseInt(days), savePath);
623622
}
624623

625624
private ILineProcess<Map<String, String>> getChangeLifecycle(boolean single) throws IOException {

src/main/java/com/qiniu/process/qiniu/RestoreArchive.java

+54-47
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
package com.qiniu.process.qiniu;
22

3-
import com.google.gson.JsonArray;
4-
import com.google.gson.JsonObject;
53
import com.qiniu.common.QiniuException;
64
import com.qiniu.http.Client;
75
import com.qiniu.http.Response;
@@ -10,51 +8,58 @@
108
import com.qiniu.util.*;
119

1210
import java.io.IOException;
11+
import java.util.ArrayList;
1312
import java.util.List;
1413
import java.util.Map;
1514
import java.util.stream.Collectors;
1615

1716
public class RestoreArchive extends Base<Map<String, String>> {
1817

1918
private int days;
20-
private String encodedCondition;
19+
private ArrayList<String> ops;
20+
private List<Map<String, String>> lines;
2121
private Configuration configuration;
2222
private Auth auth;
2323
private Client client;
2424
private static String requestUrl;
2525

26-
public RestoreArchive(String accessKey, String secretKey, Configuration configuration, String bucket, int days,
27-
String condition) throws IOException {
26+
public RestoreArchive(String accessKey, String secretKey, Configuration configuration, String bucket, int days)
27+
throws IOException {
2828
super("restorear", accessKey, secretKey, bucket);
2929
this.days = days;
30-
if (condition != null && !condition.isEmpty()) encodedCondition = UrlSafeBase64.encodeToString(condition);
3130
this.configuration = configuration;
3231
this.auth = Auth.create(accessKey, secretKey);
3332
this.client = new Client(configuration.clone());
3433
CloudApiUtils.checkQiniu(accessKey, secretKey, configuration, bucket);
35-
requestUrl = configuration.useHttpsDomains ? "https://rs.qbox.me/restoreAr" : "http://rs.qbox.me/restoreAr";
34+
requestUrl = configuration.useHttpsDomains ? "https://rs.qbox.me/restoreAr/" : "http://rs.qbox.me/restoreAr/";
3635
}
3736

3837
public RestoreArchive(String accessKey, String secretKey, Configuration configuration, String bucket, int days,
39-
String condition, String savePath, int saveIndex) throws IOException {
38+
String savePath, int saveIndex) throws IOException {
4039
super("restorear", accessKey, secretKey, bucket, savePath, saveIndex);
4140
this.days = days;
42-
if (condition != null && !condition.isEmpty()) encodedCondition = UrlSafeBase64.encodeToString(condition);
41+
this.batchSize = 1000;
42+
this.ops = new ArrayList<>(1000);
43+
this.lines = new ArrayList<>(1000);
4344
this.configuration = configuration;
4445
this.auth = Auth.create(accessKey, secretKey);
4546
this.client = new Client(configuration.clone());
4647
CloudApiUtils.checkQiniu(accessKey, secretKey, configuration, bucket);
47-
requestUrl = configuration.useHttpsDomains ? "https://rs.qbox.me/restoreAr" : "http://rs.qbox.me/restoreAr";
48+
requestUrl = configuration.useHttpsDomains ? "https://rs.qbox.me/restoreAr/" : "http://rs.qbox.me/restoreAr/";
4849
}
4950

5051
public RestoreArchive(String accessKey, String secretKey, Configuration configuration, String bucket, int days,
51-
String condition, String savePath) throws IOException {
52-
this(accessKey, secretKey, configuration, bucket, days, condition, savePath, 0);
52+
String savePath) throws IOException {
53+
this(accessKey, secretKey, configuration, bucket, days, savePath, 0);
5354
}
5455

5556
@Override
5657
public RestoreArchive clone() throws CloneNotSupportedException {
5758
RestoreArchive restoreArchive = (RestoreArchive)super.clone();
59+
if (fileSaveMapper != null) {
60+
restoreArchive.ops = new ArrayList<>(batchSize);
61+
restoreArchive.lines = new ArrayList<>(batchSize);
62+
}
5863
restoreArchive.auth = Auth.create(accessId, secretKey);
5964
restoreArchive.client = new Client(configuration.clone());
6065
return restoreArchive;
@@ -66,29 +71,38 @@ protected String resultInfo(Map<String, String> line) {
6671
}
6772

6873
@Override
69-
protected String batchResult(List<Map<String, String>> lineList) throws IOException {
70-
JsonObject bodyJson = new JsonObject();
71-
JsonArray entries = new JsonArray();
72-
JsonObject entry = new JsonObject();
73-
entry.addProperty("bucket", bucket);
74-
for (Map<String, String> line : lineList) {
75-
entry.addProperty("key", line.get("key"));
76-
entry.addProperty("freeze_after_days", days);
77-
entry.addProperty("cond", encodedCondition);
78-
entries.add(entry);
74+
protected List<Map<String, String>> putBatchOperations(List<Map<String, String>> processList) throws IOException {
75+
ops.clear();
76+
lines.clear();
77+
String key;
78+
// String encodedMetaValue;
79+
// String path;
80+
StringBuilder pathBuilder;
81+
for (Map<String, String> map : processList) {
82+
key = map.get("key");
83+
if (key != null) {
84+
lines.add(map);
85+
pathBuilder = new StringBuilder("/restoreAr/")
86+
.append(UrlSafeBase64.encodeToString(String.join(":", bucket, key)))
87+
.append("/freezeAfterDays/").append(days);
88+
ops.add(pathBuilder.toString());
89+
} else {
90+
fileSaveMapper.writeError("key is not exists or empty in " + map, false);
91+
}
7992
}
80-
bodyJson.add("entries", entries);
81-
byte[] body = bodyJson.toString().getBytes();
82-
bodyJson = null;
83-
entries = null;
84-
entry = null;
85-
return HttpRespUtils.getResult(client.post(requestUrl, body,
86-
auth.authorizationV2(requestUrl, "POST", body, Client.JsonMime), Client.JsonMime));
93+
return lines;
94+
}
95+
96+
@Override
97+
protected String batchResult(List<Map<String, String>> lineList) throws IOException {
98+
byte[] body = StringUtils.utf8Bytes(StringUtils.join(ops, "&op=", "op="));
99+
return HttpRespUtils.getResult(client.post(CloudApiUtils.QINIU_RS_BATCH_URL, body,
100+
auth.authorization(CloudApiUtils.QINIU_RS_BATCH_URL, body, Client.FormMime), Client.FormMime));
87101
}
88102

89103
@Override
90104
protected List<Map<String, String>> parseBatchResult(List<Map<String, String>> processList, String result) throws Exception {
91-
// 归档存储解冻操作本身不响应任何 body 内容
105+
// 归档存储解冻操作可能不响应任何 body 内容
92106
// if (result == null || "".equals(result)) throw new IOException("not valid refresh response.");
93107
if ("".equals(result)) {
94108
fileSaveMapper.writeSuccess(processList.stream().map(this::resultInfo).collect(Collectors.joining("\n")), false);
@@ -100,32 +114,25 @@ protected List<Map<String, String>> parseBatchResult(List<Map<String, String>> p
100114

101115
@Override
102116
protected String singleResult(Map<String, String> line) throws IOException {
103-
JsonObject bodyJson = new JsonObject();
104-
JsonArray entries = new JsonArray();
105-
JsonObject entry = new JsonObject();
106-
entry.addProperty("bucket", bucket);
107117
String key = line.get("key");
108-
entry.addProperty("key", key);
109-
entry.addProperty("freeze_after_days", days);
110-
entry.addProperty("cond", encodedCondition);
111-
entries.add(entry);
112-
bodyJson.add("entries", entries);
113-
byte[] body = bodyJson.toString().getBytes();
114-
Response response = client.post(requestUrl, body,
115-
auth.authorizationV2(requestUrl, "POST", body, Client.JsonMime), Client.JsonMime);
118+
if (key == null) throw new IOException("key is not exists or empty in " + line);
119+
StringBuilder urlBuilder = new StringBuilder(requestUrl)
120+
.append(UrlSafeBase64.encodeToString(String.join(":", bucket, key)))
121+
.append("/freezeAfterDays/").append(days);
122+
StringMap headers = auth.authorization(urlBuilder.toString(), null, Client.FormMime);
123+
Response response = client.post(urlBuilder.toString(), null, headers, Client.FormMime);
116124
if (response.statusCode != 200) throw new QiniuException(response);
117125
response.close();
118-
bodyJson = null;
119-
entries = null;
120-
entry = null;
121-
body = null;
122126
return String.join("\t", key, "200");
123127
}
124128

125129
@Override
126130
public void closeResource() {
127131
super.closeResource();
128-
encodedCondition = null;
132+
if (ops != null) ops.clear();
133+
ops = null;
134+
if (lines != null) lines.clear();
135+
lines = null;
129136
configuration = null;
130137
auth = null;
131138
client = null;

0 commit comments

Comments
 (0)