Skip to content

Commit a8b5f34

Browse files
authored
Merge pull request #573 from zhangzqs/src_domain
KODO-17355: add getDefaultIoSrcHost() to BucketManager class
2 parents 8f71829 + 877ac6a commit a8b5f34

File tree

6 files changed

+131
-8
lines changed

6 files changed

+131
-8
lines changed

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

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -193,6 +193,15 @@ String getIovipHost(RegionReqInfo regionReqInfo) throws QiniuException {
193193
return region.getIovipHost(regionReqInfo);
194194
}
195195

196+
@Override
197+
String getIoSrcHost(RegionReqInfo regionReqInfo) throws QiniuException {
198+
if (regionReqInfo == null) {
199+
return "";
200+
}
201+
Region region = queryRegionInfo(regionReqInfo);
202+
return region.getIoSrcHost(regionReqInfo);
203+
}
204+
196205
/**
197206
* 获取资源管理域名
198207
*/
@@ -296,6 +305,7 @@ private class ServerRets {
296305
ServerRet uc;
297306
ServerRet api;
298307
ServerRet io;
308+
ServerRet io_src;
299309

300310
Region createRegion() {
301311
long timestamp = ttl + System.currentTimeMillis() / 1000;
@@ -311,6 +321,11 @@ Region createRegion() {
311321
iovipHost = io.getOneHost();
312322
}
313323

324+
String ioSrcHost = null;
325+
if (io_src != null) {
326+
ioSrcHost = io_src.getOneHost();
327+
}
328+
314329
String rsHost = null;
315330
if (rs != null) {
316331
rsHost = rs.getOneHost();
@@ -337,7 +352,8 @@ Region createRegion() {
337352
regionId = "";
338353
}
339354

340-
return new Region(timestamp, regionId, srcUpHosts, accUpHosts, iovipHost, rsHost, rsfHost, apiHost, ucHost);
355+
return new Region(timestamp, regionId, srcUpHosts, accUpHosts, iovipHost,
356+
ioSrcHost, rsHost, rsfHost, apiHost, ucHost);
341357
}
342358
}
343359

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

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1079,6 +1079,16 @@ public Response getBucketQuotaResponse(String bucket) throws QiniuException {
10791079
return res;
10801080
}
10811081

1082+
/**
1083+
* 获取 Bucket 的默认源站域名
1084+
* @param bucket 空间名
1085+
* @return 源站域名
1086+
* @throws QiniuException 异常
1087+
*/
1088+
public String getDefaultIoSrcHost(String bucket) throws QiniuException {
1089+
return configHelper.ioSrcHost(auth.accessKey, bucket);
1090+
}
1091+
10821092
/*
10831093
* 相关请求的方法列表
10841094
* */

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,11 @@ public String ioHost(String ak, String bucket) throws QiniuException {
4545
return getScheme() + config.region.getIovipHost(regionReqInfo);
4646
}
4747

48+
public String ioSrcHost(String ak, String bucket) throws QiniuException {
49+
RegionReqInfo regionReqInfo = new RegionReqInfo(ak, bucket);
50+
return config.region.getIoSrcHost(regionReqInfo);
51+
}
52+
4853
public String apiHost(String ak, String bucket) throws QiniuException {
4954
RegionReqInfo regionReqInfo = new RegionReqInfo(ak, bucket);
5055
return getScheme() + config.region.getApiHost(regionReqInfo);

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

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ public class Region implements Cloneable {
2020
private List<String> srcUpHosts;
2121
private List<String> accUpHosts;
2222
private String iovipHost;
23+
private String ioSrcHost;
2324

2425
/*
2526
* 资源管理,资源列表,资源处理类域名
@@ -35,12 +36,13 @@ public class Region implements Cloneable {
3536
}
3637

3738
Region(long timestamp, String region, List<String> srcUpHosts, List<String> accUpHosts, String iovipHost,
38-
String rsHost, String rsfHost, String apiHost, String ucHost) {
39+
String ioSrcHost, String rsHost, String rsfHost, String apiHost, String ucHost) {
3940
this.timestamp = timestamp;
4041
this.region = region;
4142
this.srcUpHosts = srcUpHosts;
4243
this.accUpHosts = accUpHosts;
4344
this.iovipHost = iovipHost;
45+
this.ioSrcHost = ioSrcHost;
4446
this.rsHost = rsHost;
4547
this.rsfHost = rsfHost;
4648
this.apiHost = apiHost;
@@ -314,6 +316,10 @@ String getIovipHost(RegionReqInfo regionReqInfo) throws QiniuException {
314316
return iovipHost;
315317
}
316318

319+
String getIoSrcHost(RegionReqInfo regionReqInfo) throws QiniuException {
320+
return ioSrcHost;
321+
}
322+
317323
String getRsHost(RegionReqInfo regionReqInfo) throws QiniuException {
318324
return rsHost;
319325
}
@@ -345,6 +351,7 @@ public Object clone() {
345351
newRegion.srcUpHosts = srcUpHosts;
346352
newRegion.accUpHosts = accUpHosts;
347353
newRegion.iovipHost = iovipHost;
354+
newRegion.ioSrcHost = ioSrcHost;
348355
newRegion.rsHost = rsHost;
349356
newRegion.rsfHost = rsfHost;
350357
newRegion.apiHost = apiHost;
@@ -368,6 +375,7 @@ public Builder(Region originRegion) {
368375
region.srcUpHosts = originRegion.srcUpHosts;
369376
region.accUpHosts = originRegion.accUpHosts;
370377
region.iovipHost = originRegion.iovipHost;
378+
region.ioSrcHost = originRegion.ioSrcHost;
371379
region.rsHost = originRegion.rsHost;
372380
region.rsfHost = originRegion.rsfHost;
373381
region.apiHost = originRegion.apiHost;
@@ -397,6 +405,11 @@ public Builder iovipHost(String iovipHost) {
397405
return this;
398406
}
399407

408+
public Builder ioSrcHost(String ioSrcHost) {
409+
this.region.ioSrcHost = ioSrcHost;
410+
return this;
411+
}
412+
400413
public Builder rsHost(String rsHost) {
401414
this.region.rsHost = rsHost;
402415
return this;

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

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,15 @@ String getIovipHost(RegionReqInfo regionReqInfo) throws QiniuException {
7777
}
7878
}
7979

80+
@Override
81+
String getIoSrcHost(RegionReqInfo regionReqInfo) throws QiniuException {
82+
if (currentRegion == null) {
83+
return null;
84+
} else {
85+
return currentRegion.getIoSrcHost(regionReqInfo);
86+
}
87+
}
88+
8089
@Override
8190
String getRsHost(RegionReqInfo regionReqInfo) throws QiniuException {
8291
if (currentRegion == null) {

src/test/java/test/com/qiniu/storage/BucketTest2.java

Lines changed: 76 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
package test.com.qiniu.storage;
22

33
import com.qiniu.common.QiniuException;
4-
import com.qiniu.common.Zone;
4+
import com.qiniu.http.Client;
55
import com.qiniu.http.Response;
6-
import com.qiniu.storage.BucketManager;
7-
import com.qiniu.storage.Configuration;
6+
import com.qiniu.storage.*;
87
import com.qiniu.storage.model.*;
98
import com.qiniu.util.Json;
9+
import com.qiniu.util.StringMap;
1010
import com.qiniu.util.StringUtils;
1111
import okhttp3.Call;
1212
import okhttp3.OkHttpClient;
@@ -26,17 +26,17 @@
2626
import static org.junit.jupiter.api.Assertions.assertTrue;
2727
import static org.junit.jupiter.api.Assertions.fail;
2828

29+
import java.io.ByteArrayInputStream;
2930
import java.io.IOException;
3031
import java.text.SimpleDateFormat;
3132
import java.util.*;
3233

33-
import static org.junit.jupiter.api.Assertions.*;
34-
3534
public class BucketTest2 {
3635

3736
List<Integer> batchStatusCode = Arrays.asList(200, 298);
3837
private BucketManager bucketManager;
3938
private BucketManager dummyBucketManager;
39+
private UploadManager uploadManager;
4040

4141
/**
4242
* 初始化
@@ -45,9 +45,11 @@ public class BucketTest2 {
4545
*/
4646
@BeforeEach
4747
public void setUp() throws Exception {
48-
Configuration cfg = new Configuration(Zone.zone0());
48+
Configuration cfg = new Configuration();
49+
cfg.region = Region.autoRegion();
4950
// cfg.useHttpsDomains = false;
5051
this.bucketManager = new BucketManager(TestConfig.testAuth, cfg);
52+
this.uploadManager = new UploadManager(cfg);
5153
this.dummyBucketManager = new BucketManager(TestConfig.dummyAuth, new Configuration());
5254
}
5355

@@ -527,6 +529,74 @@ public void testBucketInfo() {
527529
}
528530
}
529531

532+
/**
533+
* 测试能够获取bucket源站域名并能用于下载
534+
*/
535+
@Test
536+
@Tag("IntegrationTest")
537+
public void testDefaultIoSrcDomainDownload() {
538+
String bucket = TestConfig.testBucket_z0;
539+
String key = "test_bucket_domain_file_key";
540+
541+
// 获取默认源站域名
542+
String domain = "";
543+
try {
544+
domain = bucketManager.getDefaultIoSrcHost(bucket);
545+
assertNotNull(domain, "domain is not null.");
546+
} catch (QiniuException e) {
547+
fail(e);
548+
}
549+
550+
// 构造测试数据
551+
byte[] uploadData = new byte[1024];
552+
new Random().nextBytes(uploadData);
553+
554+
// 将测试数据上传到测试文件
555+
try {
556+
StringMap map = new StringMap();
557+
map.put("insertOnly", "1");
558+
uploadManager.put(
559+
new ByteArrayInputStream(uploadData),
560+
key, TestConfig.testAuth.uploadToken(bucket), map, null);
561+
} catch (QiniuException e) {
562+
fail(e);
563+
}
564+
565+
// 测试文件是否存在
566+
try {
567+
FileInfo info = bucketManager.stat(bucket, key);
568+
assertNotNull(info, "info is not null.");
569+
} catch (QiniuException e) {
570+
fail(e);
571+
}
572+
573+
// 构造下载链接
574+
String url = "";
575+
try {
576+
url = new DownloadUrl(domain, false, key).buildURL();
577+
url = TestConfig.testAuth.privateDownloadUrl(url, 3600);
578+
} catch (QiniuException e) {
579+
fail(e);
580+
}
581+
582+
// 下载测试文件
583+
byte[] downloadData;
584+
Client client = new Client();
585+
try {
586+
downloadData = client.get(url).body();
587+
} catch (QiniuException e) {
588+
throw new RuntimeException(e);
589+
}
590+
assertArrayEquals(uploadData, downloadData);
591+
592+
// 删除测试文件
593+
try {
594+
bucketManager.delete(bucket, key);
595+
} catch (QiniuException e) {
596+
fail(e);
597+
}
598+
}
599+
530600
/**
531601
* 测试设置空间referer防盗链
532602
*/

0 commit comments

Comments
 (0)