forked from qiniu/java-sdk
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBucketManager.java
More file actions
1653 lines (1523 loc) · 60.6 KB
/
BucketManager.java
File metadata and controls
1653 lines (1523 loc) · 60.6 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
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
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
package com.qiniu.storage;
import com.google.gson.JsonArray;
import com.google.gson.JsonElement;
import com.google.gson.JsonNull;
import com.google.gson.JsonObject;
import com.qiniu.common.Constants;
import com.qiniu.common.QiniuException;
import com.qiniu.http.Client;
import com.qiniu.http.MethodType;
import com.qiniu.http.Response;
import com.qiniu.storage.model.*;
import com.qiniu.util.*;
import java.util.*;
/**
* 主要涉及了空间资源管理及批量操作接口的实现,具体的接口规格可以参考
* 参考文档:<a href="http://developer.qiniu.com/kodo/api/rs">资源管理</a>
*/
public final class BucketManager {
/**
* Auth 对象
* 该类需要使用QBox鉴权,所以需要指定Auth对象
*/
private final Auth auth;
/**
* HTTP Client 对象
* 该类需要通过该对象来发送HTTP请求
*/
private final Client client;
/**
* ConfigHelper 对象
* 该类相关的域名配置,解析配置,HTTP请求超时时间设置等
*/
private ConfigHelper configHelper;
private Configuration config;
/**
* 构建一个新的 BucketManager 对象
*
* @param auth Auth对象
* @param cfg Configuration对象
*/
public BucketManager(Auth auth, Configuration cfg) {
this.auth = auth;
Configuration c2 = cfg == null ? Configuration.create() : cfg.clone();
this.config = c2;
this.configHelper = new ConfigHelper(c2);
client = new Client(c2);
}
public BucketManager(Auth auth, Client client) {
this.auth = auth;
this.client = client;
this.config = Configuration.create();
this.configHelper = new ConfigHelper(this.config);
}
public BucketManager(Auth auth, Configuration cfg, Client client) {
this.auth = auth;
this.client = client;
Configuration c2 = cfg == null ? Configuration.create() : cfg.clone();
this.config = c2;
this.configHelper = new ConfigHelper(c2);
}
/**
* EncodedEntryURI格式,其中 bucket+":"+key 称之为 entry
*
* @param bucket 空间名
* @param key 文件 key
* @return UrlSafeBase64.encodeToString(entry)
* <a href="http://developer.qiniu.com/kodo/api/data-format"> 相关链接 </a>
*/
public static String encodedEntry(String bucket, String key) {
String encodedEntry;
if (key != null) {
encodedEntry = UrlSafeBase64.encodeToString(bucket + ":" + key);
} else {
encodedEntry = UrlSafeBase64.encodeToString(bucket);
}
return encodedEntry;
}
/**
* EncodedEntryURI格式,用在不指定key值的情况下
*
* @param bucket 空间名称
* @return UrlSafeBase64.encodeToString(bucket)
* <a href="http://developer.qiniu.com/kodo/api/data-format"> 相关链接 </a>
*/
public static String encodedEntry(String bucket) {
return encodedEntry(bucket, null);
}
/**
* 获取账号下所有空间名称列表
*
* @return 空间名称列表
* @throws QiniuException 异常
*/
public String[] buckets() throws QiniuException {
Response res = bucketsResponse();
String[] buckets = res.jsonToObject(String[].class);
res.close();
return buckets;
}
public Response bucketsResponse() throws QiniuException {
String url = String.format("%s/buckets", configHelper.ucHost());
Response res = post(url, null, ucInterceptors());
if (!res.isOK()) {
throw new QiniuException(res);
}
return res;
}
/**
* 创建空间
*
* @param bucketName 空间名
* @param region 区域信息
* @return Response
* @throws QiniuException 异常
*/
public Response createBucket(String bucketName, String region) throws QiniuException {
String url = String.format("%s/mkbucketv3/%s/region/%s", configHelper.ucHost(), bucketName, region);
Response res = post(url, null, ucInterceptors());
if (!res.isOK()) {
throw new QiniuException(res);
}
return res;
}
/**
* 删除空间
*
* @param bucketName 空间名
* @return Response
* @throws QiniuException 异常
*/
public Response deleteBucket(String bucketName) throws QiniuException {
String url = String.format("%s/drop/%s", configHelper.ucHost(), bucketName);
Response res = post(url, null, ucInterceptors());
if (!res.isOK()) {
throw new QiniuException(res);
}
return res;
}
/**
* 获取该空间下所有的domain
*
* @param bucket 空间名
* @return 该空间名下的 domain
* @throws QiniuException 异常
*/
public String[] domainList(String bucket) throws QiniuException {
Response res = domainListResponse(bucket);
String[] domains = res.jsonToObject(String[].class);
res.close();
return domains;
}
public Response domainListResponse(String bucket) throws QiniuException {
String url = String.format("%s/v2/domains?tbl=%s", configHelper.ucHost(), bucket);
Response res = get(url, ucInterceptors());
if (!res.isOK()) {
throw new QiniuException(res);
}
return res;
}
/**
* 根据前缀获取文件列表的迭代器
*
* @param bucket 空间名
* @param prefix 文件名前缀
* @return FileInfo迭代器
*/
public FileListIterator createFileListIterator(String bucket, String prefix) {
return new FileListIterator(bucket, prefix, 1000, null);
}
/**
* 根据前缀获取文件列表的迭代器
*
* @param bucket 空间名
* @param prefix 文件名前缀
* @param limit 每次迭代的长度限制,最大1000,推荐值 100
* @param delimiter 指定目录分隔符,列出所有公共前缀(模拟列出目录效果)。缺省值为空字符串
* @return FileInfo迭代器
*/
public FileListIterator createFileListIterator(String bucket, String prefix, int limit, String delimiter) {
return new FileListIterator(bucket, prefix, limit, delimiter);
}
private String listQuery(String bucket, String prefix, String marker, int limit, String delimiter) {
StringMap map = new StringMap().put("bucket", bucket).putNotEmpty("marker", marker)
.putNotEmpty("prefix", prefix).putNotEmpty("delimiter", delimiter).putWhen("limit", limit, limit > 0);
return map.formString();
}
/**
* 列举空间文件 v1 接口,返回一个 response 对象。
*
* @param bucket 空间名
* @param prefix 文件名前缀
* @param marker 上一次获取文件列表时返回的 marker
* @param limit 每次迭代的长度限制,最大1000,推荐值 100
* @param delimiter 指定目录分隔符,列出所有公共前缀(模拟列出目录效果)。缺省值为空字符串
* @return Response
* @throws QiniuException 异常
*/
public Response listV1(String bucket, String prefix, String marker, int limit, String delimiter)
throws QiniuException {
String url = String.format("%s/list?%s", configHelper.rsfHost(auth.accessKey, bucket),
listQuery(bucket, prefix, marker, limit, delimiter));
return get(url);
}
/**
* 列举空间文件 v1 接口
*
* @param bucket 空间名
* @param prefix 文件名前缀
* @param marker 上一次获取文件列表时返回的 marker
* @param limit 每次迭代的长度限制,推荐值 1000
* @param delimiter 指定目录分隔符,列出所有公共前缀(模拟列出目录效果)。缺省值为空字符串
* @return FileListing
* @throws QiniuException 异常
*/
public FileListing listFiles(String bucket, String prefix, String marker, int limit, String delimiter)
throws QiniuException {
Response response = listV1(bucket, prefix, marker, limit, delimiter);
if (!response.isOK()) {
response.close();
throw new QiniuException(response);
}
FileListing fileListing = response.jsonToObject(FileListing.class);
response.close();
return fileListing;
}
/**
* 列举空间文件 v2 接口,返回一个 response 对象。v2 接口可以避免由于大量删除导致的列举超时问题,返回的 response 对象中的 body 可以转换为
* string stream 来处理。
* Deprecated,使用 {@link BucketManager#listV1(String, String, String, int, String)} } 替换
*
* @param bucket 空间名
* @param prefix 文件名前缀
* @param marker 上一次获取文件列表时返回的 marker
* @param limit 每次迭代的长度限制,推荐值 1000
* @param delimiter 指定目录分隔符,列出所有公共前缀(模拟列出目录效果)。缺省值为空字符串
* @return Response 返回一个 okhttp response 对象
* @throws QiniuException 异常
*/
@Deprecated
public Response listV2(String bucket, String prefix, String marker, int limit, String delimiter)
throws QiniuException {
String url = String.format("%s/v2/list?%s", configHelper.rsfHost(auth.accessKey, bucket),
listQuery(bucket, prefix, marker, limit, delimiter));
return post(url, null);
}
/**
* 列举空间文件 v2 接口
* Deprecated,使用 {@link BucketManager#listFiles(String, String, String, int, String)} 替换
*
* @param bucket 空间名
* @param prefix 文件名前缀
* @param marker 上一次获取文件列表时返回的 marker
* @param limit 每次迭代的长度限制,推荐值 1000
* @param delimiter 指定目录分隔符,列出所有公共前缀(模拟列出目录效果)。缺省值为空字符串
* @return FileListing
* @throws QiniuException 异常
*/
@Deprecated
public FileListing listFilesV2(String bucket, String prefix, String marker, int limit, String delimiter)
throws QiniuException {
Response response = listV2(bucket, prefix, marker, limit, delimiter);
final String result = response.bodyString();
response.close();
List<String> lineList = Arrays.asList(result.split("\n"));
FileListing fileListing = new FileListing();
List<FileInfo> fileInfoList = new ArrayList<>();
Set<String> commonPrefixSet = new HashSet<>();
for (int i = 0; i < lineList.size(); i++) {
String line = lineList.get(i);
JsonObject jsonObject = Json.decode(line, JsonObject.class);
if (jsonObject == null) {
continue;
}
if (!(jsonObject.get("item") instanceof JsonNull)) {
fileInfoList.add(Json.decode(jsonObject.get("item"), FileInfo.class));
}
String dir = jsonObject.get("dir").getAsString();
if (!"".equals(dir)) commonPrefixSet.add(dir);
if (i == lineList.size() - 1)
fileListing.marker = jsonObject.get("marker").getAsString();
}
fileListing.items = fileInfoList.toArray(new FileInfo[]{});
fileListing.commonPrefixes = commonPrefixSet.toArray(new String[]{});
return fileListing;
}
/**
* 获取空间中文件的属性
*
* @param bucket 空间名称
* @param fileKey 文件名称
* @return 文件属性
* @throws QiniuException 异常
* <a href="http://developer.qiniu.com/kodo/api/stat"> 相关链接 </a>
*/
public FileInfo stat(String bucket, String fileKey) throws QiniuException {
Response res = statResponse(bucket, fileKey);
FileInfo fileInfo = res.jsonToObject(FileInfo.class);
res.close();
return fileInfo;
}
public Response statResponse(String bucket, String fileKey) throws QiniuException {
Response res = rsPost(bucket, String.format("/stat/%s", encodedEntry(bucket, fileKey)), null);
if (!res.isOK()) {
throw new QiniuException(res);
}
return res;
}
/**
* 删除指定空间、文件名的文件
*
* @param bucket 空间名称
* @param key 文件名称
* @return Response
* @throws QiniuException 异常
* <a href="http://developer.qiniu.com/kodo/api/delete"> 相关链接 </a>
*/
public Response delete(String bucket, String key) throws QiniuException {
return rsPost(bucket, String.format("/delete/%s", encodedEntry(bucket, key)), null);
}
/**
* 修改文件的MimeType
*
* @param bucket 空间名称
* @param key 文件名称
* @param mime 文件的新MimeType
* @return Response
* @throws QiniuException 异常
* <a href="http://developer.qiniu.com/kodo/api/chgm"> 相关链接 </a>
*/
public Response changeMime(String bucket, String key, String mime)
throws QiniuException {
String resource = encodedEntry(bucket, key);
String encodedMime = UrlSafeBase64.encodeToString(mime);
String path = String.format("/chgm/%s/mime/%s", resource, encodedMime);
return rsPost(bucket, path, null);
}
/**
* 修改文件的元数据
*
* @param bucket 空间名称
* @param key 文件名称
* @param headers 需要修改的文件元数据
* @return Response
* @throws QiniuException 异常
* <a href="https://developer.qiniu.com/kodo/api/1252/chgm"> 相关链接 </a>
*/
public Response changeHeaders(String bucket, String key, Map<String, String> headers)
throws QiniuException {
String resource = encodedEntry(bucket, key);
String path = String.format("/chgm/%s", resource);
for (String k : headers.keySet()) {
String encodedMetaValue = UrlSafeBase64.encodeToString(headers.get(k));
path = String.format("%s/x-qn-meta-!%s/%s", path, k, encodedMetaValue);
}
return rsPost(bucket, path, null);
}
/**
* 修改文件的类型(普通存储或低频存储)
*
* @param bucket 空间名称
* @param key 文件名称
* @param type 存储类型
* @return Response
* @throws QiniuException 异常
*/
public Response changeType(String bucket, String key, StorageType type)
throws QiniuException {
String resource = encodedEntry(bucket, key);
String path = String.format("/chtype/%s/type/%d", resource, type.ordinal());
return rsPost(bucket, path, null);
}
/**
* 解冻归档存储
* 文档:https://developer.qiniu.com/kodo/api/6380/restore-archive
*
* @param bucket 空间名称
* @param key 文件名称
* @param freezeAfterDays 解冻有效时长,取值范围 1~7
* @return Response
* @throws QiniuException 异常
*/
public Response restoreArchive(String bucket, String key, int freezeAfterDays)
throws QiniuException {
String resource = encodedEntry(bucket, key);
String path = String.format("/restoreAr/%s/freezeAfterDays/%s", resource, Integer.toString(freezeAfterDays));
String requestUrl = configHelper.rsHost(auth.accessKey, bucket) + path;
return client.post(requestUrl, null,
auth.authorizationV2(requestUrl, "POST", null, "application/json"), Client.JsonMime);
}
/**
* 修改文件的状态(禁用或者正常)
*
* @param bucket 空间名称
* @param key 文件名称
* @param status 0表示启用;1表示禁用。
* @return Response
* @throws QiniuException 异常
*/
public Response changeStatus(String bucket, String key, int status)
throws QiniuException {
String resource = encodedEntry(bucket, key);
String path = String.format("/chstatus/%s/status/%d", resource, status);
return rsPost(bucket, path, null);
}
/**
* 重命名空间中的文件,可以设置force参数为true强行覆盖空间已有同名文件
*
* @param bucket 空间名称
* @param oldFileKey 文件名称
* @param newFileKey 新文件名
* @param force 强制覆盖空间中已有同名(和 newFileKey 相同)的文件
* @return Response
* @throws QiniuException 异常
*/
public Response rename(String bucket, String oldFileKey, String newFileKey, boolean force)
throws QiniuException {
return move(bucket, oldFileKey, bucket, newFileKey, force);
}
/**
* 重命名空间中的文件
*
* @param bucket 空间名称
* @param oldFileKey 文件名称
* @param newFileKey 新文件名
* @return Response
* @throws QiniuException 异常
* <a href="http://developer.qiniu.com/kodo/api/move">相关链接</a>
*/
public Response rename(String bucket, String oldFileKey, String newFileKey)
throws QiniuException {
return move(bucket, oldFileKey, bucket, newFileKey);
}
/**
* 复制文件,要求空间在同一账号下,可以设置force参数为true强行覆盖空间已有同名文件
*
* @param fromBucket 源空间名称
* @param fromFileKey 源文件名称
* @param toBucket 目的空间名称
* @param toFileKey 目的文件名称
* @param force 强制覆盖空间中已有同名(和 toFileKey 相同)的文件
* @return Response
* @throws QiniuException 异常
*/
public Response copy(String fromBucket, String fromFileKey, String toBucket, String toFileKey, boolean force)
throws QiniuException {
String from = encodedEntry(fromBucket, fromFileKey);
String to = encodedEntry(toBucket, toFileKey);
String path = String.format("/copy/%s/%s/force/%s", from, to, force);
return rsPost(fromBucket, path, null);
}
/**
* 复制文件,要求空间在同一账号下
*
* @param fromBucket 源空间名称
* @param fromFileKey 源文件名称
* @param toBucket 目的空间名称
* @param toFileKey 目的文件名称
* @return Response
* @throws QiniuException 异常
*/
public Response copy(String fromBucket, String fromFileKey, String toBucket, String toFileKey)
throws QiniuException {
Response res = copy(fromBucket, fromFileKey, toBucket, toFileKey, false);
if (!res.isOK()) {
throw new QiniuException(res);
}
return res;
}
/**
* 移动文件,要求空间在同一账号下
*
* @param fromBucket 源空间名称
* @param fromFileKey 源文件名称
* @param toBucket 目的空间名称
* @param toFileKey 目的文件名称
* @param force 强制覆盖空间中已有同名(和 toFileKey 相同)的文件
* @return Response
* @throws QiniuException 异常
*/
public Response move(String fromBucket, String fromFileKey, String toBucket,
String toFileKey, boolean force) throws QiniuException {
String from = encodedEntry(fromBucket, fromFileKey);
String to = encodedEntry(toBucket, toFileKey);
String path = String.format("/move/%s/%s/force/%s", from, to, force);
return rsPost(fromBucket, path, null);
}
/**
* 移动文件。要求空间在同一账号下, 可以添加force参数为true强行移动文件。
*
* @param fromBucket 源空间名称
* @param fromFileKey 源文件名称
* @param toBucket 目的空间名称
* @param toFileKey 目的文件名称
* @return Response
* @throws QiniuException 异常
*/
public Response move(String fromBucket, String fromFileKey, String toBucket, String toFileKey)
throws QiniuException {
return move(fromBucket, fromFileKey, toBucket, toFileKey, false);
}
/**
* 抓取指定地址的文件,以指定名称保存在指定空间
* 要求指定url可访问,大文件不建议使用此接口抓取。可先下载再上传
* 如果不指定保存的文件名,那么以文件内容的 etag 作为文件名
*
* @param url 待抓取的文件链接
* @param bucket 文件抓取后保存的空间
* @return Response
* @throws QiniuException 异常
*/
public FetchRet fetch(String url, String bucket) throws QiniuException {
return fetch(url, bucket, null);
}
/**
* 抓取指定地址的文件,以指定名称保存在指定空间
* 要求指定url可访问,大文件不建议使用此接口抓取。可先下载再上传
*
* @param url 待抓取的文件链接
* @param bucket 文件抓取后保存的空间
* @param key 文件抓取后保存的文件名
* @return Response
* @throws QiniuException 异常
*/
public FetchRet fetch(String url, String bucket, String key) throws QiniuException {
Response res = fetchResponse(url, bucket, key);
FetchRet fetchRet = res.jsonToObject(FetchRet.class);
res.close();
return fetchRet;
}
public Response fetchResponse(String url, String bucket, String key) throws QiniuException {
String resource = UrlSafeBase64.encodeToString(url);
String to = encodedEntry(bucket, key);
String path = String.format("/fetch/%s/to/%s", resource, to);
Response res = ioPost(bucket, path);
if (!res.isOK()) {
throw new QiniuException(res);
}
return res;
}
/**
* 异步第三方资源抓取 从指定 URL 抓取资源,并将该资源存储到指定空间中。每次只抓取一个文件,抓取时可以指定保存空间名和最终资源名。
* 主要对于大文件进行抓取
* https://developer.qiniu.com/kodo/api/4097/asynch-fetch
*
* @param url 待抓取的文件链接,支持设置多个,以';'分隔
* @param bucket 文件抓取后保存的空间
* @param key 文件抓取后保存的文件名
* @return Response
* @throws QiniuException 异常
*/
public Response asynFetch(String url, String bucket, String key) throws QiniuException {
StringMap params = new StringMap().putNotNull("key", key);
return asyncFetch(url, bucket, params);
}
/**
* 异步第三方资源抓取 从指定 URL 抓取资源,并将该资源存储到指定空间中。每次只抓取一个文件,抓取时可以指定保存空间名和最终资源名。
* https://developer.qiniu.com/kodo/api/4097/asynch-fetch
* 提供更多参数的抓取 可以对抓取文件进行校验 和自定义抓取回调地址等
*
* @param url 待抓取的文件链接,支持设置多个,以';'分隔
* @param bucket 文件抓取后保存的空间
* @param key 文件抓取后保存的文件名
* @param md5 文件md5,传入以后会在存入存储时对文件做校验,校验失败则不存入指定空间
* @param etag 文件etag,传入以后会在存入存储时对文件做校验,校验失败则不存入指定空间
* @param callbackurl 回调URL,详细解释请参考上传策略中的callbackUrl
* @param callbackbody 回调Body,如果callbackurl不为空则必须指定。与普通上传一致支持魔法变量,
* @param callbackbodytype 回调Body内容类型,默认为"application/x-www-form-urlencoded",
* @param callbackhost 回调时使用的Host
* @param fileType 存储文件类型 0:正常存储(默认),1:低频存储
* @return Response
* @throws QiniuException 异常
*/
public Response asynFetch(String url, String bucket, String key, String md5, String etag,
String callbackurl, String callbackbody, String callbackbodytype,
String callbackhost, int fileType) throws QiniuException {
StringMap params = new StringMap()
.putNotNull("key", key)
.putNotNull("md5", md5)
.putNotNull("etag", etag)
.putNotNull("callbackurl", callbackurl)
.putNotNull("callbackbody", callbackbody)
.putNotNull("callbackbodytype", callbackbodytype)
.putNotNull("callbackhost", callbackhost)
.putNotNull("file_type", fileType);
return asyncFetch(url, bucket, params);
}
/**
* 异步第三方资源抓取 从指定 URL 抓取资源,并将该资源存储到指定空间中。每次只抓取一个文件,抓取时可以指定保存空间名和最终资源名。
* 主要对于大文件进行抓取
* https://developer.qiniu.com/kodo/api/4097/asynch-fetch
*
* @param url 待抓取的文件链接,支持设置多个,以';'分隔
* @param bucket 文件抓取后保存的空间
* @param params 其他参数
* @return Response
* @throws QiniuException 异常
*/
public Response asyncFetch(String url, String bucket, StringMap params) throws QiniuException {
if (params == null) params = new StringMap();
params.put("url", url).put("bucket", bucket);
String requestUrl = configHelper.apiHost(auth.accessKey, bucket) + "/sisyphus/fetch";
byte[] bodyByte = Json.encode(params).getBytes(Constants.UTF_8);
return client.post(requestUrl, bodyByte,
auth.authorizationV2(requestUrl, "POST", bodyByte, "application/json"), Client.JsonMime);
}
/**
* 查询异步抓取任务
*
* @param region 抓取任务所在bucket区域 华东 z0 华北 z1 华南 z2 北美 na0 东南亚 as0
* @param fetchWorkId 抓取任务id
* @return Response
* @throws QiniuException 异常
*/
public Response checkAsynFetchid(String region, String fetchWorkId) throws QiniuException {
String path = String.format("http://api-%s.qiniu.com/sisyphus/fetch?id=%s", region, fetchWorkId);
return client.get(path, auth.authorizationV2(path));
}
/**
* 对于设置了镜像存储的空间,从镜像源站抓取指定名称的资源并存储到该空间中
* 如果该空间中已存在该名称的资源,则会将镜像源站的资源覆盖空间中相同名称的资源
*
* @param bucket 空间名称
* @param key 文件名称
* @return Response
* @throws QiniuException 异常
*/
public Response prefetch(String bucket, String key) throws QiniuException {
String resource = encodedEntry(bucket, key);
String path = String.format("/prefetch/%s", resource);
Response res = ioPost(bucket, path);
if (!res.isOK()) {
throw new QiniuException(res);
}
return res;
}
/**
* 设置空间的镜像源站
*
* @param bucket 空间名称
* @param srcSiteUrl 镜像回源地址
* @return Response
* @throws QiniuException 异常
*/
@Deprecated
public Response setImage(String bucket, String srcSiteUrl) throws QiniuException {
return setImage(bucket, srcSiteUrl, null);
}
/**
* 设置空间的镜像源站
*
* @param bucket 空间名称
* @param srcSiteUrl 镜像回源地址
* @param host 镜像回源Host
* @return Response
* @throws QiniuException 异常
*/
@Deprecated
public Response setImage(String bucket, String srcSiteUrl, String host) throws QiniuException {
String encodedSiteUrl = UrlSafeBase64.encodeToString(srcSiteUrl);
String encodedHost = null;
if (host != null && host.length() > 0) {
encodedHost = UrlSafeBase64.encodeToString(host);
}
String path = String.format("/image/%s/from/%s", bucket, encodedSiteUrl);
if (encodedHost != null) {
path += String.format("/host/%s", encodedHost);
}
path = String.format("%s%s", configHelper.ucHost(), path);
return post(path, null, ucInterceptors());
}
/**
* 取消空间的镜像源站设置
*
* @param bucket 空间名称
* @return Response
* @throws QiniuException 异常
*/
@Deprecated
public Response unsetImage(String bucket) throws QiniuException {
String path = String.format("%s/unimage/%s", configHelper.ucHost(), bucket);
return post(path, null, ucInterceptors());
}
/**
* 设置文件的存活时间
*
* @param bucket 空间名称
* @param key 文件名称
* @param days 存活时间,单位:天
* @return Response
* @throws QiniuException 异常
*/
public Response deleteAfterDays(String bucket, String key, int days) throws QiniuException {
return rsPost(bucket, String.format("/deleteAfterDays/%s/%d", encodedEntry(bucket, key), days), null);
}
/**
* 设置 Bucket noIndexPage 属性<br>
*
* @param bucket 空间名
* @param type type 为 0 表示启用 indexPage,为 1 表示不启用indexPage
* @return Response
* @throws QiniuException 异常
*/
public Response setIndexPage(String bucket, IndexPageType type) throws QiniuException {
String url = String.format("%s/noIndexPage?bucket=%s&noIndexPage=%s",
configHelper.ucHost(), bucket, type.getType());
Response res = post(url, null, ucInterceptors());
if (!res.isOK()) {
throw new QiniuException(res);
}
return res;
}
/**
* 查询空间信息
*
* @param bucket 空间名
* @return bucket 信息
* @throws QiniuException 异常
*/
public BucketInfo getBucketInfo(String bucket) throws QiniuException {
Response res = getBucketInfoResponse(bucket);
BucketInfo info = res.jsonToObject(BucketInfo.class);
res.close();
return info;
}
public Response getBucketInfoResponse(String bucket) throws QiniuException {
String url = String.format("%s/v2/bucketInfo?bucket=%s", configHelper.ucHost(), bucket);
Response res = post(url, null, ucInterceptors());
if (!res.isOK()) {
throw new QiniuException(res);
}
res.close();
return res;
}
/**
* 设置空间 referer 防盗链
*
* @param bucket 空间名
* @param antiLeech 空间 referer 防盗链信息
* @return Response
* @throws QiniuException 异常
*/
public Response putReferAntiLeech(String bucket, BucketReferAntiLeech antiLeech) throws QiniuException {
String url = String.format("%s/referAntiLeech?bucket=%s&%s",
configHelper.ucHost(), bucket, antiLeech.asQueryString());
Response res = post(url, null, ucInterceptors());
if (!res.isOK()) {
throw new QiniuException(res);
}
return res;
}
/**
* 设置存储空间内文件的生命周期规则
*
* @param bucket 空间名
* @param rule 生命周期规则
* @return Response
* @throws QiniuException 异常
*/
public Response putBucketLifecycleRule(String bucket, BucketLifeCycleRule rule) throws QiniuException {
String url = String.format("%s/rules/add?bucket=%s&%s", configHelper.ucHost(), bucket, rule.asQueryString());
Response res = post(url, null, ucInterceptors());
if (!res.isOK()) {
throw new QiniuException(res);
}
return res;
}
/**
* 删除特定存储空间上设定的规则
*
* @param bucket 空间名
* @param ruleName 规则名
* @return Response
* @throws QiniuException 异常
*/
public Response deleteBucketLifecycleRule(String bucket, String ruleName) throws QiniuException {
String url = String.format("%s/rules/delete?bucket=%s&name=%s", configHelper.ucHost(), bucket, ruleName);
Response res = post(url, null, ucInterceptors());
if (!res.isOK()) {
throw new QiniuException(res);
}
return res;
}
/**
* 更新特定存储空间上的生命周期规则
*
* @param bucket 空间名
* @param rule 生命周期规则
* @return Response
* @throws QiniuException 异常
*/
public Response updateBucketLifeCycleRule(String bucket, BucketLifeCycleRule rule) throws QiniuException {
String url = String.format("%s/rules/update?bucket=%s&%s",
configHelper.ucHost(), bucket, rule.asQueryString());
Response res = post(url, null, ucInterceptors());
if (!res.isOK()) {
throw new QiniuException(res);
}
return res;
}
/**
* 获取指定空间上设置的生命周期规则
*
* @param bucket 空间名
* @return 生命周期规则
* @throws QiniuException 异常
*/
public BucketLifeCycleRule[] getBucketLifeCycleRule(String bucket) throws QiniuException {
Response res = getBucketLifeCycleRuleResponse(bucket);
BucketLifeCycleRule[] rules;
JsonElement element = Json.decode(res.bodyString(), JsonElement.class);
if (element instanceof JsonNull) {
rules = new BucketLifeCycleRule[0];
} else {
JsonArray array = (JsonArray) element;
rules = new BucketLifeCycleRule[array.size()];
for (int i = 0; i < array.size(); i++) {
rules[i] = Json.decode(array.get(i), BucketLifeCycleRule.class);
}
}
res.close();
return rules;
}
public Response getBucketLifeCycleRuleResponse(String bucket) throws QiniuException {
String url = String.format("%s/rules/get?bucket=%s", configHelper.ucHost(), bucket);
Response res = post(url, null, ucInterceptors());
if (!res.isOK()) {
throw new QiniuException(res);
}
return res;
}
/**
* 增加事件通知规则
*
* @param bucket 空间名
* @param rule 通知规则
* @return Response
* @throws QiniuException 异常
*/
public Response putBucketEvent(String bucket, BucketEventRule rule) throws QiniuException {
String url = String.format("%s/events/add?bucket=%s&%s", configHelper.ucHost(), bucket, rule.asQueryString());
Response res = post(url, null, ucInterceptors());
if (!res.isOK()) {
throw new QiniuException(res);
}
return res;
}
/**
* 删除事件通知规则
*
* @param bucket 空间名
* @param ruleName 规则名
* @return Response
* @throws QiniuException 异常
*/
public Response deleteBucketEvent(String bucket, String ruleName) throws QiniuException {
String url = String.format("%s/events/delete?bucket=%s&name=%s", configHelper.ucHost(), bucket, ruleName);
Response res = post(url, null, ucInterceptors());
if (!res.isOK()) {
throw new QiniuException(res);
}
return res;
}
/**
* 更新事件通知规则
*
* @param bucket 空间名
* @param rule 通知规则
* @return Response
* @throws QiniuException 异常
*/
public Response updateBucketEvent(String bucket, BucketEventRule rule) throws QiniuException {
String url = String.format("%s/events/update?bucket=%s&%s",
configHelper.ucHost(), bucket, rule.asQueryString());
Response res = post(url, null, ucInterceptors());
if (!res.isOK()) {
throw new QiniuException(res);
}
return res;
}
/**
* 获取事件通知规则
*
* @param bucket 空间名
* @return 事件通知
* @throws QiniuException 异常
*/
public BucketEventRule[] getBucketEvents(String bucket) throws QiniuException {
Response res = getBucketEventsResponse(bucket);
BucketEventRule[] rules;
JsonElement element = Json.decode(res.bodyString(), JsonElement.class);
if (element instanceof JsonNull) {
rules = new BucketEventRule[0];
} else {
JsonArray array = (JsonArray) element;
rules = new BucketEventRule[array.size()];
for (int i = 0; i < array.size(); i++) {
rules[i] = Json.decode(array.get(i), BucketEventRule.class);
}
}
res.close();
return rules;
}
public Response getBucketEventsResponse(String bucket) throws QiniuException {
String url = String.format("%s/events/get?bucket=%s", configHelper.ucHost(), bucket);
Response res = post(url, null, ucInterceptors());
if (!res.isOK()) {
throw new QiniuException(res);
}
return res;
}
/**
* 设置 bucket 的 cors(跨域)规则
*
* @param bucket 空间名
* @param rules 跨域)规则
* @return Response
* @throws QiniuException 异常
*/
public Response putCorsRules(String bucket, CorsRule[] rules) throws QiniuException {
String url = String.format("%s/corsRules/set/%s", configHelper.ucHost(), bucket);
Response res = post(url, Json.encode(rules).getBytes(Constants.UTF_8), ucInterceptors());
if (!res.isOK()) {
throw new QiniuException(res);
}
return res;
}
/**
* 获取 bucket的cors(跨域)规则
*
* @param bucket 空间名
* @return 跨域规则
* @throws QiniuException 异常
*/
public CorsRule[] getCorsRules(String bucket) throws QiniuException {