Skip to content

Commit 0794f2b

Browse files
committed
add support for setting uc host
1 parent 0e18284 commit 0794f2b

File tree

5 files changed

+69
-11
lines changed

5 files changed

+69
-11
lines changed

src/Qiniu/Storage/BucketManager.cs

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -83,8 +83,7 @@ public BucketsResult Buckets(bool shared)
8383

8484
try
8585
{
86-
string scheme = this.config.UseHttps ? "https://" : "http://";
87-
string ucHost = string.Format("{0}{1}", scheme, Config.DefaultUcHost);
86+
string ucHost = this.config.UcHost();
8887
string sharedStr = "false";
8988
if (shared)
9089
{
@@ -518,8 +517,8 @@ public DomainsResult Domains(string bucket)
518517

519518
try
520519
{
521-
string ucHost = Config.DefaultUcHost;
522-
string domainsUrl = string.Format("https://{0}{1}", ucHost, "/v2/domains");
520+
string ucHost = this.config.UcHost();
521+
string domainsUrl = string.Format("{0}{1}", ucHost, "/v2/domains");
523522
string body = string.Format("tbl={0}", bucket);
524523

525524
HttpResult hr = httpManager.PostForm(domainsUrl, null, body, auth);

src/Qiniu/Storage/Config.cs

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,19 @@ public class Config
5454
/// </summary>
5555
/// 默认值应与 <see cref="PutExtra.MaxRetryTimes"/> 一致
5656
public int MaxRetryTimes { set; get; } = 3;
57+
58+
private string _ucHost = DefaultUcHost;
59+
60+
public void SetUcHost(string val)
61+
{
62+
_ucHost = val;
63+
}
64+
65+
public string UcHost()
66+
{
67+
string scheme = UseHttps ? "https://" : "http://";
68+
return string.Format("{0}{1}", scheme, _ucHost);
69+
}
5770

5871
/// <summary>
5972
/// 获取资源管理域名
@@ -67,7 +80,7 @@ public string RsHost(string ak, string bucket)
6780
Zone z = this.Zone;
6881
if (z == null)
6982
{
70-
z = ZoneHelper.QueryZone(ak, bucket);
83+
z = ZoneHelper.QueryZone(ak, bucket, scheme);
7184
}
7285
return string.Format("{0}{1}", scheme, z.RsHost);
7386
}
@@ -84,7 +97,7 @@ public string RsfHost(string ak, string bucket)
8497
Zone z = this.Zone;
8598
if (z == null)
8699
{
87-
z = ZoneHelper.QueryZone(ak, bucket);
100+
z = ZoneHelper.QueryZone(ak, bucket, scheme);
88101
}
89102
return string.Format("{0}{1}", scheme, z.RsfHost);
90103
}
@@ -101,7 +114,7 @@ public string ApiHost(string ak, string bucket)
101114
Zone z = this.Zone;
102115
if (z == null)
103116
{
104-
z = ZoneHelper.QueryZone(ak, bucket);
117+
z = ZoneHelper.QueryZone(ak, bucket, scheme);
105118
}
106119
return string.Format("{0}{1}", scheme, z.ApiHost);
107120
}
@@ -118,7 +131,7 @@ public string IovipHost(string ak, string bucket)
118131
Zone z = this.Zone;
119132
if (z == null)
120133
{
121-
z = ZoneHelper.QueryZone(ak, bucket);
134+
z = ZoneHelper.QueryZone(ak, bucket, scheme);
122135
}
123136
return string.Format("{0}{1}", scheme, z.IovipHost);
124137
}
@@ -135,7 +148,7 @@ public string UpHost(string ak, string bucket)
135148
Zone z = this.Zone;
136149
if (z == null)
137150
{
138-
z = ZoneHelper.QueryZone(ak, bucket);
151+
z = ZoneHelper.QueryZone(ak, bucket, scheme);
139152
}
140153
string upHost = z.SrcUpHosts[0];
141154
if (this.UseCdnDomains)

src/Qiniu/Storage/ZoneHelper.cs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ public class ZoneHelper
2525
/// </summary>
2626
/// <param name="accessKey">AccessKek</param>
2727
/// <param name="bucket">空间名称</param>
28-
public static Zone QueryZone(string accessKey, string bucket)
28+
public static Zone QueryZone(string accessKey, string bucket, string scheme = "https://")
2929
{
3030
ZoneCacheValue zoneCacheValue = null;
3131
string cacheKey = string.Format("{0}:{1}", accessKey, bucket);
@@ -53,7 +53,8 @@ public static Zone QueryZone(string accessKey, string bucket)
5353
HttpResult hr = null;
5454
try
5555
{
56-
string queryUrl = string.Format("https://{0}/v2/query?ak={1}&bucket={2}",
56+
string queryUrl = string.Format("{0}{1}/v2/query?ak={2}&bucket={3}",
57+
scheme,
5758
Config.DefaultUcHost,
5859
accessKey,
5960
bucket
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
using NUnit.Framework;
2+
using Qiniu.Tests;
3+
4+
namespace Qiniu.Storage.Tests
5+
{
6+
[TestFixture]
7+
public class ConfigTests : TestEnv
8+
{
9+
[Test]
10+
public void UcHostTest()
11+
{
12+
Config config = new Config();
13+
string ucHost = config.UcHost();
14+
Assert.AreEqual("http://" + Config.DefaultUcHost, ucHost);
15+
config.SetUcHost("uc.example.com");
16+
ucHost = config.UcHost();
17+
Assert.AreEqual("http://uc.example.com", ucHost);
18+
19+
config = new Config();
20+
config.UseHttps = true;
21+
ucHost = config.UcHost();
22+
Assert.AreEqual("https://" + Config.DefaultUcHost, ucHost);
23+
config.SetUcHost("uc.example.com");
24+
ucHost = config.UcHost();
25+
Assert.AreEqual("https://uc.example.com", ucHost);
26+
}
27+
}
28+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
using NUnit.Framework;
2+
using Qiniu.Tests;
3+
4+
namespace Qiniu.Storage.Tests
5+
{
6+
[TestFixture]
7+
public class ZoneHelperTests : TestEnv
8+
{
9+
[Test]
10+
public void QueryZoneTest()
11+
{
12+
Zone zone = ZoneHelper.QueryZone(AccessKey, Bucket);
13+
14+
Assert.NotNull(zone);
15+
}
16+
}
17+
}

0 commit comments

Comments
 (0)