SDK 提供了普通下载接口Download
和流式下载接口DownloadFile
。
Download
与DownloadFile
方法请求的 US3 API 均为 GetFile,具体详见GetFile API文档。
普通下载
Download 要求传入文件的下载URL,然后把文件下载到 HTTP Body 里面,适用于下载小文件。
示例如下
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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
public class GetObjectSample {
private static final String TAG = "GetObjectSample";
private static ObjectConfig config = new ObjectConfig("cn-sh2", "ufileos.com");
public static void main(String[] args) {
String keyName = "";
String bucketName = "";
String localDir = "";
String saveName = "";
// 5 * 60秒 --> 5分钟后过期
int expiresDuration = 5 * 60;
try {
String url = UfileClient.object(Constants.OBJECT_AUTHORIZER, config)
.getDownloadUrlFromPrivateBucket(keyName, bucketName, expiresDuration)
/**
* 使用Content-Disposition: attachment,并且默认文件名为KeyName
*/
// .withAttachment()
/**
* 使用Content-Disposition: attachment,并且配置文件名
*/
// .withAttachment("filename")
/**
* 图片处理服务
* https://docs.ucloud.cn/ufile/service/pic
*/
// .withIopCmd("iopcmd=rotate°ree=90")
.createUrl();
getStream(url, localDir, saveName);
} catch (UfileParamException e) {
e.printStackTrace();
} catch (UfileAuthorizationException e) {
e.printStackTrace();
} catch (UfileSignatureException e) {
e.printStackTrace();
} catch (UfileClientException e) {
e.printStackTrace();
}
}
public static void getFile(String url, String localDir, String saveName) {
try {
DownloadFileBean response = UfileClient.object(Constants.OBJECT_AUTHORIZER, config)
.getFile(url)
.saveAt(localDir, saveName)
/**
* 选择要下载的对象的范围,Default = [0, whole size]
*/
// .withinRange(0, 0)
/**
* 是否覆盖本地已有文件, Default = true;
*/
// .withCoverage(false)
/**
* 指定progress callback的间隔
*/
// .withProgressConfig(ProgressConfig.callbackWithPercent(10))
/**
* 配置读写流Buffer的大小, Default = 256 KB, MIN = 4 KB, MAX = 4 MB
*/
// .setBufferSize(4 << 20)
/**
* 配置进度监听
*/
.setOnProgressListener(new OnProgressListener() {
@Override
public void onProgress(long bytesWritten, long contentLength) {
JLog.D(TAG, String.format("[progress] = %d%% - [%d/%d]", (int) (bytesWritten * 1.f / contentLength * 100), bytesWritten, contentLength));
}
})
.execute();
JLog.D(TAG, String.format("[res] = %s", (response == null ? "null" : response.toString())));
} catch (UfileClientException e) {
e.printStackTrace();
} catch (UfileServerException e) {
e.printStackTrace();
}
}
public static void getFileAsync(String url, String localDir, String saveName) {
UfileClient.object(Constants.OBJECT_AUTHORIZER, config)
.getFile(url)
.saveAt(localDir, saveName)
/**
* 选择要下载的对象的范围,Default = [0, whole size]
*/
// .withinRange(0, 0)
/**
* 是否覆盖本地已有文件, Default = true;
*/
// .withCoverage(false)
/**
* 指定progress callback的间隔
*/
// .withProgressConfig(ProgressConfig.callbackWithPercent(10))
/**
* 配置读写流Buffer的大小, Default = 256 KB, MIN = 4 KB, MAX = 4 MB
*/
// .setBufferSize(4 << 20)
.executeAsync(new UfileCallback<DownloadFileBean>() {
@Override
public void onProgress(long bytesWritten, long contentLength) {
JLog.D(TAG, String.format("[progress] = %d%% - [%d/%d]", (int) (bytesWritten * 1.f / contentLength * 100), bytesWritten, contentLength));
}
@Override
public void onResponse(DownloadFileBean response) {
JLog.D(TAG, String.format("[res] = %s", (response == null ? "null" : response.toString())));
}
@Override
public void onError(Request request, ApiError error, UfileErrorBean response) {
JLog.D(TAG, String.format("[error] = %s\n[info] = %s",
(error == null ? "null" : error.toString()),
(response == null ? "null" : response.toString())));
}
});
}
public static void getStream(String url, String localDir, String saveName) {
try {
OutputStream os = null;
os = new FileOutputStream(new File(localDir, saveName));
DownloadStreamBean response = UfileClient.object(Constants.OBJECT_AUTHORIZER, config)
.getStream(url)
/**
* 选择要下载的对象的范围,Default = [0, whole size]
*/
// .withinRange(0, 0)
/**
* 重定向流
*
* 默认不重定向流,下载的流会以InputStream的形式在Response中回调,并且不会回调下载进度 onProgress;
*
* 如果配置了重定向的输出流,则Response {@link DownloadStreamBean}的 InputStream = null,
* 因为流已被重定向导流到OutputStream,并且会回调进度 onProgress。
*/
.redirectStream(os)
/**
* 指定progress callback的间隔
*/
// .withProgressConfig(ProgressConfig.callbackWithPercent(10))
/**
* 配置读写流Buffer的大小, Default = 256 KB, MIN = 4 KB, MAX = 4 MB
*/
// .setBufferSize(4 << 20)
/**
* 配置进度监听
*/
.setOnProgressListener(new OnProgressListener() {
@Override
public void onProgress(long bytesWritten, long contentLength) {
JLog.D(TAG, String.format("[progress] = %d%% - [%d/%d]", (int) (bytesWritten * 1.f / contentLength * 100), bytesWritten, contentLength));
}
})
.execute();
JLog.D(TAG, String.format("[res] = %s", (response == null ? "null" : response.toString())));
} catch (UfileServerException e) {
e.printStackTrace();
} catch (FileNotFoundException e1) {
e1.printStackTrace();
} catch (UfileClientException e1) {
e1.printStackTrace();
}
}
public static void getStreamAsync(String url, String localDir, String saveName) {
OutputStream os = null;
try {
os = new FileOutputStream(new File(localDir, saveName));
} catch (FileNotFoundException e) {
e.printStackTrace();
}
UfileClient.object(Constants.OBJECT_AUTHORIZER, config)
.getStream(url)
/**
* 选择要下载的对象的范围,Default = [0, whole size]
*/
// .withinRange(0, 0)
/**
* 重定向流
*
* 默认不重定向流,下载的流会以InputStream的形式在Response中回调,并且不会回调下载进度 onProgress;
*
* 如果配置了重定向的输出流,则Response {@link DownloadStreamBean}的 InputStream = null,
* 因为流已被重定向导流到OutputStream,并且会回调进度 onProgress。
*/
.redirectStream(os)
/**
* 指定progress callback的间隔
*/
// .withProgressConfig(ProgressConfig.callbackWithPercent(10))
/**
* 配置读写流Buffer的大小, Default = 256 KB, MIN = 4 KB, MAX = 4 MB
*/
// .setBufferSize(4 << 20)
.executeAsync(new UfileCallback<DownloadStreamBean>() {
@Override
public void onProgress(long bytesWritten, long contentLength) {
JLog.D(TAG, String.format("[progress] = %d%% - [%d/%d]", (int) (bytesWritten * 1.f / contentLength * 100), bytesWritten, contentLength));
}
@Override
public void onResponse(DownloadStreamBean response) {
JLog.D(TAG, String.format("[res] = %s", (response == null ? "null" : response.toString())));
}
@Override
public void onError(Request request, ApiError error, UfileErrorBean response) {
JLog.D(TAG, String.format("[error] = %s\n[info] = %s",
(error == null ? "null" : error.toString()),
(response == null ? "null" : response.toString())));
}
});
}
}
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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
package cn.ucloud.ufile.sample.object;
import cn.ucloud.ufile.UfileClient;
import cn.ucloud.ufile.api.ApiError;
import cn.ucloud.ufile.api.object.ObjectConfig;
import cn.ucloud.ufile.bean.DownloadFileBean;
import cn.ucloud.ufile.bean.ObjectProfile;
import cn.ucloud.ufile.bean.UfileErrorBean;
import cn.ucloud.ufile.exception.UfileClientException;
import cn.ucloud.ufile.exception.UfileServerException;
import cn.ucloud.ufile.http.OnProgressListener;
import cn.ucloud.ufile.http.UfileCallback;
import cn.ucloud.ufile.sample.Constants;
import cn.ucloud.ufile.util.JLog;
import okhttp3.Request;
/**
* @author: joshua
* @E-mail: joshua.yin@ucloud.cn
* @date: 2018-12-11 14:32
*/
public class DownloadFileSample {
private static final String TAG = "DownloadFileSample";
private static ObjectConfig config = new ObjectConfig("cn-sh2", "ufileos.com");
public static void main(String[] args) {
String keyName = "";
String bucketName = "";
String localDir = "";
String saveName = "";
downloadFile(keyName, bucketName, localDir, saveName);
}
public static void downloadFile(String keyName, String bucketName, String localDir, String saveName) {
try {
ObjectProfile profile = UfileClient.object(Constants.OBJECT_AUTHORIZER, config)
.objectProfile(keyName, bucketName)
.execute();
JLog.D(TAG, String.format("[res] = %s", (profile == null ? "null" : profile.toString())));
if (profile == null)
return;
DownloadFileBean response = UfileClient.object(Constants.OBJECT_AUTHORIZER, config)
.downloadFile(profile)
.saveAt(localDir, saveName)
/**
* 选择要下载的对象的范围,Default = [0, whole size]
*/
// .withinRange(0, 0)
/**
* 配置同时分片下载的进程数,Default = 10
*/
// .together(5)
/**
* 是否覆盖本地已有文件, Default = true;
*/
// .withCoverage(false)
/**
* 指定progress callback的间隔
*/
// .withProgressConfig(ProgressConfig.callbackWithPercent(10))
/**
* 配置读写流Buffer的大小, Default = 256 KB, MIN = 4 KB, MAX = 4 MB
*/
// .setBufferSize(4 << 20)
/**
* 配置进度监听
*/
.setOnProgressListener(new OnProgressListener() {
@Override
public void onProgress(long bytesWritten, long contentLength) {
JLog.D(TAG, String.format("[progress] = %d%% - [%d/%d]", (int) (bytesWritten * 1.f / contentLength * 100), bytesWritten, contentLength));
}
})
.execute();
JLog.D(TAG, String.format("[res] = %s", (response == null ? "null" : response.toString())));
} catch (UfileClientException e) {
e.printStackTrace();
} catch (UfileServerException e) {
e.printStackTrace();
}
}
public static void downloadFileAsync(String keyName, String bucketName, final String localDir, final String saveName) {
UfileClient.object(Constants.OBJECT_AUTHORIZER, config)
.objectProfile(keyName, bucketName)
.executeAsync(new UfileCallback<ObjectProfile>() {
@Override
public void onResponse(ObjectProfile profile) {
JLog.D(TAG, String.format("[res] = %s", (profile == null ? "null" : profile.toString())));
if (profile == null)
return;
UfileClient.object(Constants.OBJECT_AUTHORIZER, config)
.downloadFile(profile)
.saveAt(localDir, saveName)
/**
* 选择要下载的对象的范围,Default = (0, whole size)
*/
// .withinRange(0, 0)
/**
* 配置同时分片下载的进程数,Default = 10
*/
// .together(5)
/**
* 是否覆盖本地已有文件, Default = true;
*/
// .withCoverage(false)
/**
* 指定progress callback的间隔
*/
// .withProgressConfig(ProgressConfig.callbackWithPercent(10))
/**
* 配置读写流Buffer的大小, Default = 256 KB, MIN = 4 KB, MAX = 4 MB
*/
// .setBufferSize(4 << 20)
.executeAsync(new UfileCallback<DownloadFileBean>() {
@Override
public void onProgress(long bytesWritten, long contentLength) {
JLog.D(TAG, String.format("[progress] = %d%% - [%d/%d]", (int) (bytesWritten * 1.f / contentLength * 100), bytesWritten, contentLength));
}
@Override
public void onResponse(DownloadFileBean response) {
JLog.D(TAG, String.format("[res] = %s", (response == null ? "null" : response.toString())));
}
@Override
public void onError(Request request, ApiError error, UfileErrorBean response) {
JLog.D(TAG, String.format("[error] = %s\n[info] = %s",
(error == null ? "null" : error.toString()),
(response == null ? "null" : response.toString())));
}
});
}
@Override
public void onError(Request request, ApiError error, UfileErrorBean response) {
JLog.D(TAG, String.format("[error] = %s\n[info] = %s",
(error == null ? "null" : error.toString()),
(response == null ? "null" : response.toString())));
}
});
}
}
错误码
HTTP 状态码 | RetCode | ErrMsg | 描述 |
---|---|---|---|
400 | -148653 | bucket not exists | 存储空间不存在 |
400 | -15036 | check md5 failed | MD5校验失败 |
401 | -148643 | no authorization found | 上传凭证错误 |
403 | -148643 | invalid signature | API公私钥错误 |
404 | -148654 | file not exist | 文件不存在 |
401 | -148660 | request expired | 下载URL过期 |