forked from qiniu/java-sdk
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBaseUploader.java
More file actions
76 lines (61 loc) · 2.17 KB
/
BaseUploader.java
File metadata and controls
76 lines (61 loc) · 2.17 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
package com.qiniu.storage;
import com.qiniu.common.QiniuException;
import com.qiniu.http.Client;
import com.qiniu.http.Response;
public abstract class BaseUploader {
protected final Client client;
protected final String key;
protected final String upToken;
protected final ConfigHelper configHelper;
protected final Configuration config;
BaseUploader(Client client, String upToken, String key, Configuration config) {
this.client = client;
this.key = key;
this.upToken = upToken;
if (config == null) {
this.config = Configuration.create();
} else {
this.config = config.clone();
}
this.configHelper = new ConfigHelper(this.config);
}
public Response upload() throws QiniuException {
if (this.config == null) {
throw QiniuException.unrecoverable("config can't be empty");
}
return uploadWithRegionRetry();
}
private Response uploadWithRegionRetry() throws QiniuException {
Response response = null;
QiniuException exception = null;
while (true) {
response = null;
exception = null;
try {
response = uploadFlows();
} catch (QiniuException e) {
exception = e;
}
if (!Retry.canSwitchRegionAndRetry(response, exception)
|| !couldReloadSource() || !reloadSource()) {
break;
}
// context 过期,不需要切换 region
if (response != null && response.isContextExpiredError()
|| exception != null && exception.response != null && exception.response.isContextExpiredError()) {
continue;
}
// 切换 region
if (config.region == null || !config.region.switchRegion(new UploadToken(upToken))) {
break;
}
}
if (exception != null) {
throw exception;
}
return response;
}
abstract Response uploadFlows() throws QiniuException;
abstract boolean couldReloadSource();
abstract boolean reloadSource();
}