Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Batch more params #596

Merged
merged 35 commits into from
Dec 11, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
35 commits
Select commit Hold shift + click to select a range
a5b7adb
version to 7.14.0
Jul 17, 2023
5331fcc
Merge branches 'master' and 'master' of YangSen-qn:qiniu/java-sdk
Oct 18, 2023
e477179
Region support create by region id
Oct 27, 2023
ba3073b
test case: exception don't print
Oct 27, 2023
915e96c
change test case
Oct 27, 2023
f4a48e2
change test case
Oct 30, 2023
e95a91f
change test case
Oct 30, 2023
18ba83b
uc query cache support disk
Nov 2, 2023
a73fb17
change test case
Nov 2, 2023
34fbd2c
region create by id handle z0
Nov 3, 2023
d389e5c
Merge branch 'region-create-by-region-id' into uc-query-disk-cache
Nov 6, 2023
a198120
optimize api content-type
Nov 6, 2023
e56e194
optimize test case
Nov 6, 2023
c6c6585
cache remove flushcount
Nov 7, 2023
36d4607
optimize response
Nov 7, 2023
cca65c5
change cache test case
Nov 7, 2023
789cdef
add condition
Nov 9, 2023
8b13d71
batch add some operation
Nov 10, 2023
1e22059
batch optimize some code
Nov 13, 2023
3fe2c7a
region host support contain scheme
Nov 14, 2023
c9b1ac5
Merge branch 'uc-query-disk-cache' of YangSen-qn:YangSen-qn/java-sdk …
Nov 14, 2023
cba5907
remove useless code
Nov 14, 2023
c6e3c29
optimize some code
Nov 14, 2023
2238f6f
change test case
Nov 14, 2023
76c8940
handle url setHostScheme
Nov 15, 2023
a5b22e5
change download url
Nov 15, 2023
4c8963c
change test case
Nov 15, 2023
c935d1e
Merge branch 'uc-query-disk-cache' into batch-more-params
Nov 15, 2023
9a99b56
Merge branch 'master' into uc-query-disk-cache
Nov 15, 2023
2470d90
Merge branch 'uc-query-disk-cache' into batch-more-params
Nov 15, 2023
38942b7
change test case
Nov 17, 2023
b1a2229
format
Nov 17, 2023
0860d6c
Merge branch 'master' of YangSen-qn:qiniu/java-sdk into batch-more-pa…
Nov 17, 2023
9823f99
change test case
Nov 20, 2023
0ea1d36
region support set uc host
Nov 21, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
174 changes: 171 additions & 3 deletions src/main/java/com/qiniu/storage/BucketManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -1251,15 +1251,53 @@ public BatchOperations() {
* @return BatchOperations
*/
public BatchOperations addChgmOp(String bucket, String key, String newMimeType) {
String resource = encodedEntry(bucket, key);
String encodedMime = UrlSafeBase64.encodeToString(newMimeType);
ops.add(String.format("/chgm/%s/mime/%s", resource, encodedMime));
return addChgmOp(bucket, key, newMimeType, null, null);
}

/**
* 添加 chgm 指令
* <a href="http://developer.qiniu.com/kodo/api/chgm"> 相关链接 </a>
* newMimeType 和 metaData 必须有其一
*
* @param bucket 空间名
* @param key 文件的 key
* @param newMimeType 修改后的 MimeType [可选]
* @param metas 需要修改的 metas,只包含需要更改的 metas,可增加 [可选]
* 服务接口中 key 必须包含 x-qn-meta- 前缀,SDK 会对 metas 中的 key 进行检测
* - key 如果包含了 x-qn-meta- 前缀,则直接使用 key
* - key 如果不包含了 x-qn-meta- 前缀,则内部会为 key 拼接 x-qn-meta- 前缀
* @param condition 自定义条件信息;只有条件匹配才会执行修改操作 [可选]
* @return BatchOperations
*/
public BatchOperations addChgmOp(String bucket, String key, String newMimeType, Map<String, String> metas, Condition condition) {
StringBuilder builder = new StringBuilder()
.append("/chgm/").append(encodedEntry(bucket, key));
if (newMimeType != null && !newMimeType.isEmpty()) {
builder.append("/mime/").append(UrlSafeBase64.encodeToString(newMimeType));
}

if (metas != null) {
for (String k : metas.keySet()) {
if (k.startsWith("x-qn-meta-")) {
builder.append("/").append(k);
} else {
builder.append("/x-qn-meta-").append(k);
}
builder.append("/").append(UrlSafeBase64.encodeToString(metas.get(k)));
}
}

if (condition != null && condition.encodedString() != null) {
builder.append("/cond/").append(condition.encodedString());
}
ops.add(builder.toString());
setExecBucket(bucket);
return this;
}

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

/**
* 添加 copy 指令
*
* @param fromBucket 源空间名
* @param fromFileKey 源文件的 key
* @param toBucket 目标空间名
* @param toFileKey 目标文件的 key
* @param force 当目标文件已存在时,是否木盖目标文件
* false: 如果目标文件名已被占用,则返回错误码 614,且不做任何覆盖操作;
* true: 如果目标文件名已被占用,会强制覆盖目标文件
* @return BatchOperations
*/
public BatchOperations addCopyOp(String fromBucket, String fromFileKey, String toBucket, String toFileKey, boolean force) {
String from = encodedEntry(fromBucket, fromFileKey);
String to = encodedEntry(toBucket, toFileKey);
ops.add(String.format("copy/%s/%s/force/%s", from, to, force));
setExecBucket(fromBucket);
return this;
}

/**
* 添加重命名指令
*
Expand All @@ -1287,6 +1345,21 @@ public BatchOperations addRenameOp(String fromBucket, String fromFileKey, String
return addMoveOp(fromBucket, fromFileKey, fromBucket, toFileKey);
}

/**
* 添加重命名指令
*
* @param fromBucket 源空间名
* @param fromFileKey 源文件的 key
* @param toFileKey 目标文件的 key
* @param force 当目标文件已存在时,是否木盖目标文件
* false: 如果目标文件名已被占用,则返回错误码 614,且不做任何覆盖操作;
* true: 如果目标文件名已被占用,会强制覆盖目标文件
* @return BatchOperations
*/
public BatchOperations addRenameOp(String fromBucket, String fromFileKey, String toFileKey, boolean force) {
return addMoveOp(fromBucket, fromFileKey, fromBucket, toFileKey, force);
}

/**
* 添加move指令
*
Expand All @@ -1304,6 +1377,26 @@ public BatchOperations addMoveOp(String fromBucket, String fromKey, String toBuc
return this;
}

/**
* 添加move指令
*
* @param fromBucket 源空间名
* @param fromKey 源文件的 keys
* @param toBucket 目标空间名
* @param toKey 目标文件的 keys
* @param force 当目标文件已存在时,是否木盖目标文件
* false: 如果目标文件名已被占用,则返回错误码 614,且不做任何覆盖操作;
* true: 如果目标文件名已被占用,会强制覆盖目标文件
* @return BatchOperations
*/
public BatchOperations addMoveOp(String fromBucket, String fromKey, String toBucket, String toKey, boolean force) {
String from = encodedEntry(fromBucket, fromKey);
String to = encodedEntry(toBucket, toKey);
ops.add(String.format("move/%s/%s/force/%s", from, to, force));
setExecBucket(fromBucket);
return this;
}

/**
* 添加delete指令
*
Expand Down Expand Up @@ -1429,6 +1522,81 @@ public int size() {
}
}

public static final class Condition {
private final String hash;
private final String mime;
private final Long fSize;
private final Long putTime;

private Condition(String hash, String mime, Long fSize, Long putTime) {
this.hash = hash;
this.mime = mime;
this.fSize = fSize;
this.putTime = putTime;
}

String encodedString() {
StringBuilder builder = new StringBuilder();
if (hash != null && !hash.isEmpty()) {
builder.append("hash=" + hash + "&");
}
if (mime != null && !mime.isEmpty()) {
builder.append("mime=" + mime + "&");
}
if (fSize != null) {
builder.append("fsize=" + fSize + "&");
}
if (putTime != null) {
builder.append("putTime=" + putTime + "&");
}

String encoded = builder.toString();
if (encoded.isEmpty()) {
return null;
}

if (encoded.endsWith("&")) {
encoded = encoded.substring(0, encoded.length() - 1);
}

return UrlSafeBase64.encodeToString(encoded);
}

public static final class Builder {
private String hash;
private String mime;
private Long fileSize;
private Long putTime;

public Builder() {
}

public Builder setHash(String hash) {
this.hash = hash;
return this;
}

public Builder setMime(String mime) {
this.mime = mime;
return this;
}

public Builder setFileSize(Long fileSize) {
this.fileSize = fileSize;
return this;
}

public Builder setPutTime(Long putTime) {
this.putTime = putTime;
return this;
}

public Condition build() {
return new Condition(hash, mime, fileSize, putTime);
}
}
}

/**
* 创建文件列表迭代器
*/
Expand Down
9 changes: 8 additions & 1 deletion src/main/java/com/qiniu/storage/Region.java
Original file line number Diff line number Diff line change
Expand Up @@ -371,7 +371,7 @@ String getApiHost(RegionReqInfo regionReqInfo) throws QiniuException {
}

String getUcHost(RegionReqInfo regionReqInfo) throws QiniuException {
if (ucHosts == null || ucHosts.size() == 0) {
if (ucHosts == null || ucHosts.isEmpty()) {
return "";
}
return ucHosts.get(0);
Expand Down Expand Up @@ -470,6 +470,13 @@ public Builder apiHost(String apiHost) {
return this;
}

public Builder ucHost(String... ucHosts) {
if (ucHosts.length > 0) {
this.region.ucHosts = Arrays.asList(ucHosts);
}
return this;
}

/**
* 自动选择,其它参数设置无效
*
Expand Down
48 changes: 48 additions & 0 deletions src/test/java/com/qiniu/storage/BatchTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
package com.qiniu.storage;

import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Tag;
import org.junit.jupiter.api.Test;

public class BatchTest {

@Test
@Tag("UnitTest")
public void testBatchCondition() {

BucketManager.Condition condition = new BucketManager.Condition.Builder()
.build();
String encodedString = condition.encodedString();
Assertions.assertNull(encodedString);

condition = new BucketManager.Condition.Builder()
.setHash("hash")
.build();
encodedString = condition.encodedString();
Assertions.assertEquals(encodedString, "aGFzaD1oYXNo");

condition = new BucketManager.Condition.Builder()
.setHash("hash")
.setPutTime(1993232L)
.build();
encodedString = condition.encodedString();
Assertions.assertEquals(encodedString, "aGFzaD1oYXNoJnB1dFRpbWU9MTk5MzIzMg==");

condition = new BucketManager.Condition.Builder()
.setHash("hash")
.setMime("application/txt")
.setPutTime(1993232L)
.build();
encodedString = condition.encodedString();
Assertions.assertEquals(encodedString, "aGFzaD1oYXNoJm1pbWU9YXBwbGljYXRpb24vdHh0JnB1dFRpbWU9MTk5MzIzMg==");

condition = new BucketManager.Condition.Builder()
.setHash("hash")
.setMime("application/txt")
.setFileSize(100L)
.setPutTime(1993232L)
.build();
encodedString = condition.encodedString();
Assertions.assertEquals(encodedString, "aGFzaD1oYXNoJm1pbWU9YXBwbGljYXRpb24vdHh0JmZzaXplPTEwMCZwdXRUaW1lPTE5OTMyMzI=");
}
}
34 changes: 31 additions & 3 deletions src/test/java/test/com/qiniu/TestConfig.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,9 @@ public final class TestConfig {
// test: ak, sk, auth
public static final String testAccessKey = System.getenv("QINIU_ACCESS_KEY");
public static final String testSecretKey = System.getenv("QINIU_SECRET_KEY");
public static final String testBucket = System.getenv("BUCKET");
public static final String testBucketDomain = System.getenv("BUCKET_DOMAIN");

// 内部测试环境 AK/SK
public static final String innerAccessKey = System.getenv("testAK");
public static final String innerSecretKey = System.getenv("testSK");
Expand All @@ -46,7 +49,10 @@ public final class TestConfig {
public static final String testPipeline = "sdktest";

// z1
public static final String testBucket_z1 = "sdk-z1";
public static final String testBucket_z1 = "javasdk-z1";
public static final String testKey_z1 = "do_not_delete/1.png";
public static final String testDomain_z1 = "javasdk-z1.qiniupkg.com";
public static final String testDomain_z1_timeStamp = "javasdk-timestamp.peterpy.cn";

// na0
public static final String testBucket_na0 = "java-sdk-na0";
Expand Down Expand Up @@ -111,6 +117,17 @@ public static TestFile[] getTestFileArray(String fileSaveKey, String fileMimeTyp
z0.regionId = "z0";
z0.region = Region.createWithRegionId("z0");

TestFile z1 = new TestFile();
z1.key = fileSaveKey;
z1.mimeType = fileMimeType;
z1.bucketName = testBucket_z1;
z1.testDomain = testDomain_z1;
z1.testUrl = "http://" + z1.testDomain + "/" + fileSaveKey;
z1.testDomainTimeStamp = testDomain_z0_timeStamp;
z1.testUrlTimeStamp = "http://" + testDomain_z0_timeStamp + "/" + fileSaveKey;
z1.regionId = "z1";
z1.region = Region.createWithRegionId("z1");

TestFile z0_auto = new TestFile();
z0_auto.key = fileSaveKey;
z0_auto.mimeType = fileMimeType;
Expand All @@ -122,7 +139,18 @@ public static TestFile[] getTestFileArray(String fileSaveKey, String fileMimeTyp
z0_auto.regionId = "z0";
z0_auto.region = Region.region0();

return new TestFile[]{z0};
TestFile file = new TestFile();
file.key = fileSaveKey;
file.mimeType = fileMimeType;
file.bucketName = (testBucket != null && !testBucket.isEmpty()) ? testBucket : testBucket_z0;
file.testDomain = (testBucketDomain != null && !testBucketDomain.isEmpty()) ? testBucketDomain : testDomain_z0;
file.testUrl = "http://" + file.testDomain + "/" + fileSaveKey;
file.testDomainTimeStamp = testDomain_z0_timeStamp;
file.testUrlTimeStamp = "http://" + testDomain_z0_timeStamp + "/" + fileSaveKey;
file.regionId = "z0";
file.region = testBucket_z1.equals(testBucket) ? Region.region1() : Region.region0();

return new TestFile[]{file};
}

public static TestFile[] getAllRegionTestFileArray() {
Expand Down Expand Up @@ -221,7 +249,7 @@ public static TestFile[] getRetryTestFileArray() {
regionGroup.addRegion(region00);
regionGroup.addRegion(region01);
na0.region = regionGroup;
return new TestFile[] { na0 };
return new TestFile[]{na0};
}

private static Region toRegion(Zone zone) {
Expand Down
Loading
Loading