1
1
package com .qiniu .process .qiniu ;
2
2
3
- import com .google .gson .JsonArray ;
4
- import com .google .gson .JsonObject ;
5
3
import com .qiniu .common .QiniuException ;
6
4
import com .qiniu .http .Client ;
7
5
import com .qiniu .http .Response ;
10
8
import com .qiniu .util .*;
11
9
12
10
import java .io .IOException ;
11
+ import java .util .ArrayList ;
13
12
import java .util .List ;
14
13
import java .util .Map ;
15
14
import java .util .stream .Collectors ;
16
15
17
16
public class RestoreArchive extends Base <Map <String , String >> {
18
17
19
18
private int days ;
20
- private String encodedCondition ;
19
+ private ArrayList <String > ops ;
20
+ private List <Map <String , String >> lines ;
21
21
private Configuration configuration ;
22
22
private Auth auth ;
23
23
private Client client ;
24
24
private static String requestUrl ;
25
25
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 {
28
28
super ("restorear" , accessKey , secretKey , bucket );
29
29
this .days = days ;
30
- if (condition != null && !condition .isEmpty ()) encodedCondition = UrlSafeBase64 .encodeToString (condition );
31
30
this .configuration = configuration ;
32
31
this .auth = Auth .create (accessKey , secretKey );
33
32
this .client = new Client (configuration .clone ());
34
33
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/ " ;
36
35
}
37
36
38
37
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 {
40
39
super ("restorear" , accessKey , secretKey , bucket , savePath , saveIndex );
41
40
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 );
43
44
this .configuration = configuration ;
44
45
this .auth = Auth .create (accessKey , secretKey );
45
46
this .client = new Client (configuration .clone ());
46
47
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/ " ;
48
49
}
49
50
50
51
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 );
53
54
}
54
55
55
56
@ Override
56
57
public RestoreArchive clone () throws CloneNotSupportedException {
57
58
RestoreArchive restoreArchive = (RestoreArchive )super .clone ();
59
+ if (fileSaveMapper != null ) {
60
+ restoreArchive .ops = new ArrayList <>(batchSize );
61
+ restoreArchive .lines = new ArrayList <>(batchSize );
62
+ }
58
63
restoreArchive .auth = Auth .create (accessId , secretKey );
59
64
restoreArchive .client = new Client (configuration .clone ());
60
65
return restoreArchive ;
@@ -66,29 +71,38 @@ protected String resultInfo(Map<String, String> line) {
66
71
}
67
72
68
73
@ 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
+ }
79
92
}
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 ));
87
101
}
88
102
89
103
@ Override
90
104
protected List <Map <String , String >> parseBatchResult (List <Map <String , String >> processList , String result ) throws Exception {
91
- // 归档存储解冻操作本身不响应任何 body 内容
105
+ // 归档存储解冻操作可能不响应任何 body 内容
92
106
// if (result == null || "".equals(result)) throw new IOException("not valid refresh response.");
93
107
if ("" .equals (result )) {
94
108
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
100
114
101
115
@ Override
102
116
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 );
107
117
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 );
116
124
if (response .statusCode != 200 ) throw new QiniuException (response );
117
125
response .close ();
118
- bodyJson = null ;
119
- entries = null ;
120
- entry = null ;
121
- body = null ;
122
126
return String .join ("\t " , key , "200" );
123
127
}
124
128
125
129
@ Override
126
130
public void closeResource () {
127
131
super .closeResource ();
128
- encodedCondition = null ;
132
+ if (ops != null ) ops .clear ();
133
+ ops = null ;
134
+ if (lines != null ) lines .clear ();
135
+ lines = null ;
129
136
configuration = null ;
130
137
auth = null ;
131
138
client = null ;
0 commit comments