Skip to content

Commit bacbea9

Browse files
author
YangSen-qn
committed
Merge branch 'master' of YangSen-qn:qiniu/java-sdk
2 parents c86c02c + f784a07 commit bacbea9

File tree

12 files changed

+456
-60
lines changed

12 files changed

+456
-60
lines changed

src/main/java/com/qiniu/storage/AutoRegion.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ private UCRet queryRegionInfoFromServerIfNeeded(RegionIndex index) throws QiniuE
8383
}
8484

8585
String[] ucHosts = getUcHostArray();
86-
String address = getUcServer() + "/v3/query?ak=" + index.accessKey + "&bucket=" + index.bucket;
86+
String address = UrlUtils.setHostScheme(getUcServer(), true) + "/v3/query?ak=" + index.accessKey + "&bucket=" + index.bucket;
8787
Api api = new Api(client, new Api.Config.Builder()
8888
.setSingleHostRetryMax(retryMax)
8989
.setRetryInterval(retryInterval)

src/main/java/com/qiniu/storage/BucketManager.java

Lines changed: 172 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -389,7 +389,7 @@ public Response changeHeaders(String bucket, String key, Map<String, String> hea
389389
*
390390
* @param bucket 空间名称
391391
* @param key 文件名称
392-
* @param type type=0 表示普通存储,type=1 表示低频存存储, type=2 表示归档存储, type=3 表示深度归档存储
392+
* @param type 存储类型
393393
* @return Response
394394
* @throws QiniuException 异常
395395
*/
@@ -1251,15 +1251,53 @@ public BatchOperations() {
12511251
* @return BatchOperations
12521252
*/
12531253
public BatchOperations addChgmOp(String bucket, String key, String newMimeType) {
1254-
String resource = encodedEntry(bucket, key);
1255-
String encodedMime = UrlSafeBase64.encodeToString(newMimeType);
1256-
ops.add(String.format("/chgm/%s/mime/%s", resource, encodedMime));
1254+
return addChgmOp(bucket, key, newMimeType, null, null);
1255+
}
1256+
1257+
/**
1258+
* 添加 chgm 指令
1259+
* <a href="http://developer.qiniu.com/kodo/api/chgm"> 相关链接 </a>
1260+
* newMimeType 和 metaData 必须有其一
1261+
*
1262+
* @param bucket 空间名
1263+
* @param key 文件的 key
1264+
* @param newMimeType 修改后的 MimeType [可选]
1265+
* @param metas 需要修改的 metas,只包含需要更改的 metas,可增加 [可选]
1266+
* 服务接口中 key 必须包含 x-qn-meta- 前缀,SDK 会对 metas 中的 key 进行检测
1267+
* - key 如果包含了 x-qn-meta- 前缀,则直接使用 key
1268+
* - key 如果不包含了 x-qn-meta- 前缀,则内部会为 key 拼接 x-qn-meta- 前缀
1269+
* @param condition 自定义条件信息;只有条件匹配才会执行修改操作 [可选]
1270+
* @return BatchOperations
1271+
*/
1272+
public BatchOperations addChgmOp(String bucket, String key, String newMimeType, Map<String, String> metas, Condition condition) {
1273+
StringBuilder builder = new StringBuilder()
1274+
.append("/chgm/").append(encodedEntry(bucket, key));
1275+
if (newMimeType != null && !newMimeType.isEmpty()) {
1276+
builder.append("/mime/").append(UrlSafeBase64.encodeToString(newMimeType));
1277+
}
1278+
1279+
if (metas != null) {
1280+
for (String k : metas.keySet()) {
1281+
if (k.startsWith("x-qn-meta-")) {
1282+
builder.append("/").append(k);
1283+
} else {
1284+
builder.append("/x-qn-meta-").append(k);
1285+
}
1286+
builder.append("/").append(UrlSafeBase64.encodeToString(metas.get(k)));
1287+
}
1288+
}
1289+
1290+
if (condition != null && condition.encodedString() != null) {
1291+
builder.append("/cond/").append(condition.encodedString());
1292+
}
1293+
ops.add(builder.toString());
12571294
setExecBucket(bucket);
12581295
return this;
12591296
}
12601297

12611298
/**
12621299
* 添加 copy 指令
1300+
* 如果目标文件名已被占用,则返回错误码 614,且不做任何覆盖操作;
12631301
*
12641302
* @param fromBucket 源空间名
12651303
* @param fromFileKey 源文件的 key
@@ -1275,6 +1313,26 @@ public BatchOperations addCopyOp(String fromBucket, String fromFileKey, String t
12751313
return this;
12761314
}
12771315

1316+
/**
1317+
* 添加 copy 指令
1318+
*
1319+
* @param fromBucket 源空间名
1320+
* @param fromFileKey 源文件的 key
1321+
* @param toBucket 目标空间名
1322+
* @param toFileKey 目标文件的 key
1323+
* @param force 当目标文件已存在时,是否木盖目标文件
1324+
* false: 如果目标文件名已被占用,则返回错误码 614,且不做任何覆盖操作;
1325+
* true: 如果目标文件名已被占用,会强制覆盖目标文件
1326+
* @return BatchOperations
1327+
*/
1328+
public BatchOperations addCopyOp(String fromBucket, String fromFileKey, String toBucket, String toFileKey, boolean force) {
1329+
String from = encodedEntry(fromBucket, fromFileKey);
1330+
String to = encodedEntry(toBucket, toFileKey);
1331+
ops.add(String.format("copy/%s/%s/force/%s", from, to, force));
1332+
setExecBucket(fromBucket);
1333+
return this;
1334+
}
1335+
12781336
/**
12791337
* 添加重命名指令
12801338
*
@@ -1287,6 +1345,21 @@ public BatchOperations addRenameOp(String fromBucket, String fromFileKey, String
12871345
return addMoveOp(fromBucket, fromFileKey, fromBucket, toFileKey);
12881346
}
12891347

1348+
/**
1349+
* 添加重命名指令
1350+
*
1351+
* @param fromBucket 源空间名
1352+
* @param fromFileKey 源文件的 key
1353+
* @param toFileKey 目标文件的 key
1354+
* @param force 当目标文件已存在时,是否木盖目标文件
1355+
* false: 如果目标文件名已被占用,则返回错误码 614,且不做任何覆盖操作;
1356+
* true: 如果目标文件名已被占用,会强制覆盖目标文件
1357+
* @return BatchOperations
1358+
*/
1359+
public BatchOperations addRenameOp(String fromBucket, String fromFileKey, String toFileKey, boolean force) {
1360+
return addMoveOp(fromBucket, fromFileKey, fromBucket, toFileKey, force);
1361+
}
1362+
12901363
/**
12911364
* 添加move指令
12921365
*
@@ -1304,6 +1377,26 @@ public BatchOperations addMoveOp(String fromBucket, String fromKey, String toBuc
13041377
return this;
13051378
}
13061379

1380+
/**
1381+
* 添加move指令
1382+
*
1383+
* @param fromBucket 源空间名
1384+
* @param fromKey 源文件的 keys
1385+
* @param toBucket 目标空间名
1386+
* @param toKey 目标文件的 keys
1387+
* @param force 当目标文件已存在时,是否木盖目标文件
1388+
* false: 如果目标文件名已被占用,则返回错误码 614,且不做任何覆盖操作;
1389+
* true: 如果目标文件名已被占用,会强制覆盖目标文件
1390+
* @return BatchOperations
1391+
*/
1392+
public BatchOperations addMoveOp(String fromBucket, String fromKey, String toBucket, String toKey, boolean force) {
1393+
String from = encodedEntry(fromBucket, fromKey);
1394+
String to = encodedEntry(toBucket, toKey);
1395+
ops.add(String.format("move/%s/%s/force/%s", from, to, force));
1396+
setExecBucket(fromBucket);
1397+
return this;
1398+
}
1399+
13071400
/**
13081401
* 添加delete指令
13091402
*
@@ -1429,6 +1522,81 @@ public int size() {
14291522
}
14301523
}
14311524

1525+
public static final class Condition {
1526+
private final String hash;
1527+
private final String mime;
1528+
private final Long fSize;
1529+
private final Long putTime;
1530+
1531+
private Condition(String hash, String mime, Long fSize, Long putTime) {
1532+
this.hash = hash;
1533+
this.mime = mime;
1534+
this.fSize = fSize;
1535+
this.putTime = putTime;
1536+
}
1537+
1538+
String encodedString() {
1539+
StringBuilder builder = new StringBuilder();
1540+
if (hash != null && !hash.isEmpty()) {
1541+
builder.append("hash=" + hash + "&");
1542+
}
1543+
if (mime != null && !mime.isEmpty()) {
1544+
builder.append("mime=" + mime + "&");
1545+
}
1546+
if (fSize != null) {
1547+
builder.append("fsize=" + fSize + "&");
1548+
}
1549+
if (putTime != null) {
1550+
builder.append("putTime=" + putTime + "&");
1551+
}
1552+
1553+
String encoded = builder.toString();
1554+
if (encoded.isEmpty()) {
1555+
return null;
1556+
}
1557+
1558+
if (encoded.endsWith("&")) {
1559+
encoded = encoded.substring(0, encoded.length() - 1);
1560+
}
1561+
1562+
return UrlSafeBase64.encodeToString(encoded);
1563+
}
1564+
1565+
public static final class Builder {
1566+
private String hash;
1567+
private String mime;
1568+
private Long fileSize;
1569+
private Long putTime;
1570+
1571+
public Builder() {
1572+
}
1573+
1574+
public Builder setHash(String hash) {
1575+
this.hash = hash;
1576+
return this;
1577+
}
1578+
1579+
public Builder setMime(String mime) {
1580+
this.mime = mime;
1581+
return this;
1582+
}
1583+
1584+
public Builder setFileSize(Long fileSize) {
1585+
this.fileSize = fileSize;
1586+
return this;
1587+
}
1588+
1589+
public Builder setPutTime(Long putTime) {
1590+
this.putTime = putTime;
1591+
return this;
1592+
}
1593+
1594+
public Condition build() {
1595+
return new Condition(hash, mime, fileSize, putTime);
1596+
}
1597+
}
1598+
}
1599+
14321600
/**
14331601
* 创建文件列表迭代器
14341602
*/

src/main/java/com/qiniu/storage/Region.java

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -371,7 +371,7 @@ String getApiHost(RegionReqInfo regionReqInfo) throws QiniuException {
371371
}
372372

373373
String getUcHost(RegionReqInfo regionReqInfo) throws QiniuException {
374-
if (ucHosts == null || ucHosts.size() == 0) {
374+
if (ucHosts == null || ucHosts.isEmpty()) {
375375
return "";
376376
}
377377
return ucHosts.get(0);
@@ -470,6 +470,13 @@ public Builder apiHost(String apiHost) {
470470
return this;
471471
}
472472

473+
public Builder ucHost(String... ucHosts) {
474+
if (ucHosts.length > 0) {
475+
this.region.ucHosts = Arrays.asList(ucHosts);
476+
}
477+
return this;
478+
}
479+
473480
/**
474481
* 自动选择,其它参数设置无效
475482
*

src/main/java/com/qiniu/storage/model/BucketLifeCycleRule.java

Lines changed: 38 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,15 @@ public class BucketLifeCycleRule {
4141
@SerializedName("to_line_after_days")
4242
int toLineAfterDays;
4343

44+
/**
45+
* 指定文件上传多少天后转归档直读存储。
46+
* 0 表示不转归档直读存储,
47+
* < 0 表示上传的文件立即变归档直读存储
48+
* > 0 表示多少天后转归档直读存储
49+
*/
50+
@SerializedName("to_archive_ir_after_days")
51+
int toArchiveIRAfterDays;
52+
4453
/**
4554
* 指定文件上传多少天后转归档存储。
4655
* 0 表示不转归档存储,
@@ -141,17 +150,36 @@ public int getToLineAfterDays() {
141150
/**
142151
* 在多少天后转低频存储<br>
143152
*
144-
* @param toLineAfterDays
145-
* 0 - 表示不转低频<br>
146-
* 小于 0 表示上传的文件立即使用低频存储<br>
147-
* 大于 0 表示转低频的天数
153+
* @param toLineAfterDays 0 - 表示不转低频<br>
154+
* 大于 0 表示转低频的天数
148155
* @return 规则信息
149156
*/
150157
public BucketLifeCycleRule setToLineAfterDays(int toLineAfterDays) {
151158
this.toLineAfterDays = toLineAfterDays;
152159
return this;
153160
}
154161

162+
/**
163+
* 获得在多少天后转归档直读存储
164+
*
165+
* @return 多少天后转归档直读存储
166+
*/
167+
public int getToArchiveIRAfterDays() {
168+
return toArchiveIRAfterDays;
169+
}
170+
171+
/**
172+
* 在多少天后转归档直读存储<br>
173+
*
174+
* @param toArchiveIRAfterDays 0 - 表示不转归档直读存储<br>
175+
* 大于 0 表示多少天后转归档直读存储
176+
* @return 规则信息
177+
*/
178+
public BucketLifeCycleRule setToArchiveIRAfterDays(int toArchiveIRAfterDays) {
179+
this.toArchiveIRAfterDays = toArchiveIRAfterDays;
180+
return this;
181+
}
182+
155183
/**
156184
* 获得在多少天后转归档存储
157185
*
@@ -164,10 +192,8 @@ public int getToArchiveAfterDays() {
164192
/**
165193
* 在多少天后转归档存储<br>
166194
*
167-
* @param toArchiveAfterDays
168-
* 0 - 表示不转归档存储<br>
169-
* 小于 0 表示上传的文件立即使用归档存储<br>
170-
* 大于 0 表示多少天后转归档存储
195+
* @param toArchiveAfterDays 0 - 表示不转归档存储<br>
196+
* 大于 0 表示多少天后转归档存储
171197
* @return 规则信息
172198
*/
173199
public BucketLifeCycleRule setToArchiveAfterDays(int toArchiveAfterDays) {
@@ -187,10 +213,8 @@ public int getToDeepArchiveAfterDays() {
187213
/**
188214
* 在多少天后转深度归档存储<br>
189215
*
190-
* @param toDeepArchiveAfterDays
191-
* 0 - 表示不转深度归档存储<br>
192-
* 小于 0 表示上传的文件立即使用深度归档存储<br>
193-
* 大于 0 表示多少天后转深度归档存储
216+
* @param toDeepArchiveAfterDays 0 - 表示不转深度归档存储<br>
217+
* 大于 0 表示多少天后转深度归档存储
194218
* @return 规则信息
195219
*/
196220
public BucketLifeCycleRule setToDeepArchiveAfterDays(int toDeepArchiveAfterDays) {
@@ -204,11 +228,12 @@ public BucketLifeCycleRule setToDeepArchiveAfterDays(int toDeepArchiveAfterDays)
204228
* @return query
205229
*/
206230
public String asQueryString() {
207-
return String.format("name=%s&prefix=%s&delete_after_days=%d&to_line_after_days=%d&to_archive_after_days=%d&to_deep_archive_after_days=%d",
231+
return String.format("name=%s&prefix=%s&delete_after_days=%d&to_line_after_days=%d&to_archive_ir_after_days=%d&to_archive_after_days=%d&to_deep_archive_after_days=%d",
208232
null == name ? "" : name,
209233
null == prefix ? "" : prefix,
210234
deleteAfterDays,
211235
toLineAfterDays,
236+
toArchiveIRAfterDays,
212237
toArchiveAfterDays,
213238
toDeepArchiveAfterDays
214239
);

0 commit comments

Comments
 (0)