@@ -1251,15 +1251,53 @@ public BatchOperations() {
1251
1251
* @return BatchOperations
1252
1252
*/
1253
1253
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 ());
1257
1294
setExecBucket (bucket );
1258
1295
return this ;
1259
1296
}
1260
1297
1261
1298
/**
1262
1299
* 添加 copy 指令
1300
+ * 如果目标文件名已被占用,则返回错误码 614,且不做任何覆盖操作;
1263
1301
*
1264
1302
* @param fromBucket 源空间名
1265
1303
* @param fromFileKey 源文件的 key
@@ -1275,6 +1313,26 @@ public BatchOperations addCopyOp(String fromBucket, String fromFileKey, String t
1275
1313
return this ;
1276
1314
}
1277
1315
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
+
1278
1336
/**
1279
1337
* 添加重命名指令
1280
1338
*
@@ -1287,6 +1345,21 @@ public BatchOperations addRenameOp(String fromBucket, String fromFileKey, String
1287
1345
return addMoveOp (fromBucket , fromFileKey , fromBucket , toFileKey );
1288
1346
}
1289
1347
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
+
1290
1363
/**
1291
1364
* 添加move指令
1292
1365
*
@@ -1304,6 +1377,26 @@ public BatchOperations addMoveOp(String fromBucket, String fromKey, String toBuc
1304
1377
return this ;
1305
1378
}
1306
1379
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
+
1307
1400
/**
1308
1401
* 添加delete指令
1309
1402
*
@@ -1429,6 +1522,81 @@ public int size() {
1429
1522
}
1430
1523
}
1431
1524
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
+
1432
1600
/**
1433
1601
* 创建文件列表迭代器
1434
1602
*/
0 commit comments