11
11
import java .util .Iterator ;
12
12
import java .util .Map ;
13
13
14
-
15
14
/**
16
15
* 主要涉及了空间资源管理及批量操作接口的实现,具体的接口规格可以参考
17
16
* 参考文档:<a href="http://developer.qiniu.com/kodo/api/rs">资源管理</a>
18
17
*/
19
18
public final class BucketManager {
19
+
20
20
/**
21
21
* Auth 对象
22
22
* 该类需要使用QBox鉴权,所以需要指定Auth对象
@@ -27,7 +27,6 @@ public final class BucketManager {
27
27
* Configuration 对象
28
28
* 该类相关的域名配置,解析配置,HTTP请求超时时间设置等
29
29
*/
30
-
31
30
private Configuration configuration ;
32
31
33
32
/**
@@ -53,7 +52,6 @@ public BucketManager(Auth auth, Client client) {
53
52
this .client = client ;
54
53
}
55
54
56
-
57
55
/**
58
56
* EncodedEntryURI格式,其中 bucket+":"+key 称之为 entry
59
57
*
@@ -83,7 +81,6 @@ public static String encodedEntry(String bucket) {
83
81
return encodedEntry (bucket , null );
84
82
}
85
83
86
-
87
84
/**
88
85
* 获取账号下所有空间名称列表
89
86
*
@@ -92,19 +89,32 @@ public static String encodedEntry(String bucket) {
92
89
public String [] buckets () throws QiniuException {
93
90
// 获取 bucket 列表 写死用rs.qiniu.com or rs.qbox.me @冯立元
94
91
String url = String .format ("%s/buckets" , configuration .rsHost ());
95
- Response r = get (url );
96
- return r .jsonToObject (String [].class );
92
+ Response res = get (url );
93
+ if (!res .isOK ()) {
94
+ throw new QiniuException (res );
95
+ }
96
+ String [] buckets = res .jsonToObject (String [].class );
97
+ res .close ();
98
+ return buckets ;
97
99
}
98
100
99
101
public void createBucket (String bucketName , String region ) throws QiniuException {
100
102
String url = String .format ("%s/mkbucketv2/%s/region/%s" , configuration .rsHost (),
101
103
UrlSafeBase64 .encodeToString (bucketName ), region );
102
- post (url , null ).close ();
104
+ Response res = post (url , null );
105
+ if (!res .isOK ()) {
106
+ throw new QiniuException (res );
107
+ }
108
+ res .close ();
103
109
}
104
110
105
111
public void deleteBucket (String bucketname ) throws QiniuException {
106
112
String url = String .format ("%s/drop/%s" , configuration .rsHost (), bucketname );
107
- post (url , null ).close ();
113
+ Response res = post (url , null );
114
+ if (!res .isOK ()) {
115
+ throw new QiniuException (res );
116
+ }
117
+ res .close ();
108
118
}
109
119
110
120
/**
@@ -114,11 +124,15 @@ public void deleteBucket(String bucketname) throws QiniuException {
114
124
* @return 该空间名下的domain
115
125
* @throws QiniuException
116
126
*/
117
-
118
127
public String [] domainList (String bucket ) throws QiniuException {
119
128
String url = String .format ("%s/v6/domain/list?tbl=%s" , configuration .apiHost (), bucket );
120
- Response r = get (url );
121
- return r .jsonToObject (String [].class );
129
+ Response res = get (url );
130
+ if (!res .isOK ()) {
131
+ throw new QiniuException (res );
132
+ }
133
+ String [] domains = res .jsonToObject (String [].class );
134
+ res .close ();
135
+ return domains ;
122
136
}
123
137
124
138
/**
@@ -180,7 +194,6 @@ public FileInfo stat(String bucket, String fileKey) throws QiniuException {
180
194
return r .jsonToObject (FileInfo .class );
181
195
}
182
196
183
-
184
197
/**
185
198
* 删除指定空间、文件名的文件
186
199
*
@@ -230,7 +243,6 @@ public Response changeHeaders(String bucket, String key, Map<String, String> hea
230
243
return rsPost (bucket , path , null );
231
244
}
232
245
233
-
234
246
/**
235
247
* 修改文件的类型(普通存储或低频存储)
236
248
*
@@ -306,7 +318,6 @@ public void copy(String fromBucket, String fromFileKey, String toBucket, String
306
318
copy (fromBucket , fromFileKey , toBucket , toFileKey , false );
307
319
}
308
320
309
-
310
321
/**
311
322
* 移动文件,要求空间在同一账号下
312
323
*
@@ -339,7 +350,6 @@ public Response move(String fromBucket, String fromFileKey, String toBucket, Str
339
350
return move (fromBucket , fromFileKey , toBucket , toFileKey , false );
340
351
}
341
352
342
-
343
353
/**
344
354
* 抓取指定地址的文件,以指定名称保存在指定空间
345
355
* 要求指定url可访问,大文件不建议使用此接口抓取。可先下载再上传
@@ -381,7 +391,6 @@ public FetchRet fetch(String url, String bucket, String key) throws QiniuExcepti
381
391
* @return Response
382
392
* @throws QiniuException
383
393
*/
384
-
385
394
public Response asynFetch (String url , String bucket , String key ) throws QiniuException {
386
395
String requesturl = configuration .apiHost (auth .accessKey , bucket ) + "/sisyphus/fetch" ;
387
396
StringMap stringMap = new StringMap ().put ("url" , url ).put ("bucket" , bucket ).putNotNull ("key" , key );
@@ -520,18 +529,15 @@ public BucketInfo getBucketInfo(String bucket) throws QiniuException {
520
529
return info ;
521
530
}
522
531
523
-
524
532
public void setIndexPage (String bucket , IndexPageType type ) throws QiniuException {
525
533
String url = String .format ("%s/noIndexPage?bucket=%s&noIndexPage=%s" ,
526
534
configuration .ucHost (), bucket , type .getType ());
527
535
Response res = post (url , null );
528
536
}
529
537
530
-
531
538
/*
532
539
* 相关请求的方法列表
533
540
* */
534
-
535
541
private Response rsPost (String bucket , String path , byte [] body ) throws QiniuException {
536
542
check (bucket );
537
543
String url = configuration .rsHost (auth .accessKey , bucket ) + path ;
@@ -592,7 +598,6 @@ public BatchOperations() {
592
598
/**
593
599
* 添加chgm指令
594
600
*/
595
-
596
601
public BatchOperations addChgmOp (String bucket , String key , String newMimeType ) {
597
602
String resource = encodedEntry (bucket , key );
598
603
String encodedMime = UrlSafeBase64 .encodeToString (newMimeType );
0 commit comments