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
*
@@ -114,7 +111,6 @@ public void deleteBucket(String bucketname) throws QiniuException {
114
111
* @return 该空间名下的domain
115
112
* @throws QiniuException
116
113
*/
117
-
118
114
public String [] domainList (String bucket ) throws QiniuException {
119
115
String url = String .format ("%s/v6/domain/list?tbl=%s" , configuration .apiHost (), bucket );
120
116
Response r = get (url );
@@ -176,11 +172,15 @@ public FileListing listFiles(String bucket, String prefix, String marker, int li
176
172
* @link http://developer.qiniu.com/kodo/api/stat
177
173
*/
178
174
public FileInfo stat (String bucket , String fileKey ) throws QiniuException {
179
- Response r = rsGet (bucket , String .format ("/stat/%s" , encodedEntry (bucket , fileKey )));
180
- return r .jsonToObject (FileInfo .class );
175
+ Response res = rsGet (bucket , String .format ("/stat/%s" , encodedEntry (bucket , fileKey )));
176
+ if (!res .isOK ()) {
177
+ throw new QiniuException (res );
178
+ }
179
+ FileInfo fileInfo = res .jsonToObject (FileInfo .class );
180
+ res .close ();
181
+ return fileInfo ;
181
182
}
182
183
183
-
184
184
/**
185
185
* 删除指定空间、文件名的文件
186
186
*
@@ -230,7 +230,6 @@ public Response changeHeaders(String bucket, String key, Map<String, String> hea
230
230
return rsPost (bucket , path , null );
231
231
}
232
232
233
-
234
233
/**
235
234
* 修改文件的类型(普通存储或低频存储)
236
235
*
@@ -303,10 +302,13 @@ public Response copy(String fromBucket, String fromFileKey, String toBucket, Str
303
302
*/
304
303
public void copy (String fromBucket , String fromFileKey , String toBucket , String toFileKey )
305
304
throws QiniuException {
306
- copy (fromBucket , fromFileKey , toBucket , toFileKey , false );
305
+ Response res = copy (fromBucket , fromFileKey , toBucket , toFileKey , false );
306
+ if (!res .isOK ()) {
307
+ throw new QiniuException (res );
308
+ }
309
+ res .close ();
307
310
}
308
311
309
-
310
312
/**
311
313
* 移动文件,要求空间在同一账号下
312
314
*
@@ -339,7 +341,6 @@ public Response move(String fromBucket, String fromFileKey, String toBucket, Str
339
341
return move (fromBucket , fromFileKey , toBucket , toFileKey , false );
340
342
}
341
343
342
-
343
344
/**
344
345
* 抓取指定地址的文件,以指定名称保存在指定空间
345
346
* 要求指定url可访问,大文件不建议使用此接口抓取。可先下载再上传
@@ -366,8 +367,13 @@ public FetchRet fetch(String url, String bucket, String key) throws QiniuExcepti
366
367
String resource = UrlSafeBase64 .encodeToString (url );
367
368
String to = encodedEntry (bucket , key );
368
369
String path = String .format ("/fetch/%s/to/%s" , resource , to );
369
- Response r = ioPost (bucket , path );
370
- return r .jsonToObject (FetchRet .class );
370
+ Response res = ioPost (bucket , path );
371
+ if (!res .isOK ()) {
372
+ throw new QiniuException (res );
373
+ }
374
+ FetchRet fetchRet = res .jsonToObject (FetchRet .class );
375
+ res .close ();
376
+ return fetchRet ;
371
377
}
372
378
373
379
/**
@@ -381,7 +387,6 @@ public FetchRet fetch(String url, String bucket, String key) throws QiniuExcepti
381
387
* @return Response
382
388
* @throws QiniuException
383
389
*/
384
-
385
390
public Response asynFetch (String url , String bucket , String key ) throws QiniuException {
386
391
String requesturl = configuration .apiHost (auth .accessKey , bucket ) + "/sisyphus/fetch" ;
387
392
StringMap stringMap = new StringMap ().put ("url" , url ).put ("bucket" , bucket ).putNotNull ("key" , key );
@@ -446,7 +451,11 @@ public Response checkAsynFetchid(String region, String fetchWorkId) throws Qiniu
446
451
public void prefetch (String bucket , String key ) throws QiniuException {
447
452
String resource = encodedEntry (bucket , key );
448
453
String path = String .format ("/prefetch/%s" , resource );
449
- ioPost (bucket , path );
454
+ Response res = ioPost (bucket , path );
455
+ if (!res .isOK ()) {
456
+ throw new QiniuException (res );
457
+ }
458
+ res .close ();
450
459
}
451
460
452
461
/**
@@ -503,35 +512,36 @@ public Response deleteAfterDays(String bucket, String key, int days) throws Qini
503
512
public void setBucketAcl (String bucket , AclType acl ) throws QiniuException {
504
513
String url = String .format ("%s/private?bucket=%s&private=%s" , configuration .ucHost (), bucket , acl .getType ());
505
514
Response res = post (url , null );
506
- res .close ();
507
515
if (!res .isOK ()) {
508
516
throw new QiniuException (res );
509
517
}
518
+ res .close ();
510
519
}
511
520
512
521
public BucketInfo getBucketInfo (String bucket ) throws QiniuException {
513
522
String url = String .format ("%s/v2/bucketInfo?bucket=%s" , configuration .ucHost (), bucket );
514
523
Response res = post (url , null );
515
524
if (!res .isOK ()) {
516
- res .close ();
517
525
throw new QiniuException (res );
518
526
}
519
527
BucketInfo info = res .jsonToObject (BucketInfo .class );
528
+ res .close ();
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 );
536
+ if (!res .isOK ()) {
537
+ throw new QiniuException (res );
538
+ }
539
+ res .close ();
528
540
}
529
541
530
-
531
542
/*
532
543
* 相关请求的方法列表
533
544
* */
534
-
535
545
private Response rsPost (String bucket , String path , byte [] body ) throws QiniuException {
536
546
check (bucket );
537
547
String url = configuration .rsHost (auth .accessKey , bucket ) + path ;
@@ -592,7 +602,6 @@ public BatchOperations() {
592
602
/**
593
603
* 添加chgm指令
594
604
*/
595
-
596
605
public BatchOperations addChgmOp (String bucket , String key , String newMimeType ) {
597
606
String resource = encodedEntry (bucket , key );
598
607
String encodedMime = UrlSafeBase64 .encodeToString (newMimeType );
0 commit comments