Skip to content

Commit 8c1dedd

Browse files
Merge pull request #38 from tencentyun/feature_nedzzhang_2918d852
Feature nedzzhang 2918d852
2 parents c5891e9 + 9ded23e commit 8c1dedd

File tree

14 files changed

+453
-25
lines changed

14 files changed

+453
-25
lines changed

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,5 +9,7 @@ QCloudCSharpSDK/COSXMLTests/obj
99
QCloudCSharpSDK/COSXMLTests/bin
1010
QCloudCSharpSDK/COSXMLTests/TestResults
1111
QCloudCSharpSDK/COSXMLTests/coveragereport
12+
QCloudCSharpSDK/NCOS
13+
QCloudCSharpSDK/.idea
1214
*.vs
1315
.vscode

QCloudCSharpSDK/COSXML/COSXML-Netcore.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
<Deterministic>true</Deterministic>
1212

1313
<PackageId>Tencent.QCloud.Cos.Sdk</PackageId>
14-
<Version>5.4.35.0</Version>
14+
<Version>5.4.36.0</Version>
1515
<Authors>Tencent</Authors>
1616
<Company>Tencent</Company>
1717
<description>Tencent Cloud COS(Cloud Object Service) .Net SDK</description>

QCloudCSharpSDK/COSXML/Common/CosVersion.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ namespace COSXML.Common
99
{
1010
public sealed class CosVersion
1111
{
12-
private static string SDKVersion = "5.4.35.0";
12+
private static string SDKVersion = "5.4.36.0";
1313

1414
public static string GetUserAgent()
1515
{

QCloudCSharpSDK/COSXML/Model/Bucket/BucketRequest.cs

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -85,8 +85,29 @@ public override string GetHost()
8585
.Append(".myqcloud.com");
8686
}
8787
}
88-
89-
return hostBuilder.ToString();
88+
89+
String hostStr = hostBuilder.ToString();
90+
91+
if (userKeepDefaultDomain && !operationTimeOutRetry)
92+
{
93+
return hostStr;
94+
}
95+
96+
if (operationTimeOutRetry || changeDefaultDomain)
97+
{
98+
StringBuilder pattern = new StringBuilder();
99+
pattern.Append(".cos.").Append(region).Append(".myqcloud.com");
100+
String patternStr = pattern.ToString();
101+
102+
if (hostStr.EndsWith(patternStr))
103+
{
104+
StringBuilder replace = new StringBuilder();
105+
replace.Append(".cos.").Append(region).Append(".tencentcos.cn");
106+
return hostStr.Replace(patternStr, replace.ToString());
107+
}
108+
}
109+
110+
return hostStr;
90111
}
91112

92113
public override void CheckParameters()

QCloudCSharpSDK/COSXML/Model/CosRequest.cs

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,12 @@ public abstract class CosRequest
5656
/// </summary>
5757
protected bool needMD5 = true;
5858

59+
public bool userKeepDefaultDomain = false;
60+
61+
public bool changeDefaultDomain = false;
62+
63+
public bool operationTimeOutRetry = false;
64+
5965
/// <summary>
6066
/// 请求预签名URL
6167
/// </summary>
@@ -339,7 +345,8 @@ public virtual CosXmlSignSourceProvider GetSignSourceProvider()
339345
"response-content-type",
340346
"response-expires",
341347
"transfer-encoding",
342-
"versionid"
348+
"versionid",
349+
"pic-operations"
343350
});
344351

345352
foreach (KeyValuePair<string, string> pair in headers)

QCloudCSharpSDK/COSXML/Model/Object/ObjectRequest.cs

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -114,8 +114,29 @@ public override string GetHost()
114114
.Append(".myqcloud.com");
115115
}
116116
}
117-
118-
return hostBuilder.ToString();
117+
118+
String hostStr = hostBuilder.ToString();
119+
120+
if (userKeepDefaultDomain && !operationTimeOutRetry)
121+
{
122+
return hostStr;
123+
}
124+
125+
if (operationTimeOutRetry || changeDefaultDomain)
126+
{
127+
StringBuilder pattern = new StringBuilder();
128+
pattern.Append(".cos.").Append(region).Append(".myqcloud.com");
129+
String patternStr = pattern.ToString();
130+
131+
if (hostStr.EndsWith(patternStr))
132+
{
133+
StringBuilder replace = new StringBuilder();
134+
replace.Append(".cos.").Append(region).Append(".tencentcos.cn");
135+
return hostStr.Replace(patternStr, replace.ToString());
136+
}
137+
}
138+
139+
return hostStr;
119140
}
120141

121142
public override void CheckParameters()

QCloudCSharpSDK/COSXML/Model/Object/PutObjectRequest.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -212,6 +212,7 @@ public override Network.RequestBody GetRequestBody()
212212
"stream offset + contentLength greater than stream.Length");
213213
}
214214
body = new StreamRequestBody(stream, fileOffset, contentLength);
215+
body.ProgressCallback = progressCallback;
215216
}
216217

217218
return body;

QCloudCSharpSDK/COSXML/Network/CommandTask.cs

Lines changed: 23 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@
99
using System.Reflection;
1010
using System.IO;
1111
using System.Net.Cache;
12+
using COSXML.CosException;
13+
using COSXML.Model.Tag;
1214

1315

1416
namespace COSXML.Network
@@ -59,7 +61,7 @@ public static void Excute(Request request, Response response, HttpClientConfig c
5961
httpWebRequest = HttpWebRequest.Create(request.RequestUrlString) as HttpWebRequest;
6062

6163
httpWebRequest.AllowWriteStreamBuffering = false;
62-
64+
6365
//bind webRequest
6466
request.BindHttpWebRequest(httpWebRequest);
6567

@@ -77,7 +79,6 @@ public static void Excute(Request request, Response response, HttpClientConfig c
7779
}
7880
catch (WebException webEx)
7981
{
80-
8182
if (webEx.Response != null && webEx.Response is HttpWebResponse)
8283
{
8384
//notify has been got response
@@ -149,9 +150,25 @@ private static void HandleHttpWebRequest(HttpWebRequest httpWebRequest, Request
149150
private static void HandleHttpWebResponse(HttpWebResponse httpWebResponse, Response response)
150151
{
151152
HandleHttpWebResponseHeaders(response, httpWebResponse);
152-
153+
string requestId = httpWebResponse.GetResponseHeader("x-cos-request-id");
153154
//handle body
154-
response.Body.HandleResponseBody(httpWebResponse.GetResponseStream());
155+
if (requestId == String.Empty)
156+
{
157+
CosServerException cosServerException = new CosServerException((int)httpWebResponse.StatusCode, "request has error");
158+
cosServerException.requestId = requestId;
159+
throw cosServerException;
160+
}
161+
162+
try
163+
{
164+
response.Body.HandleResponseBody(httpWebResponse.GetResponseStream());
165+
}
166+
catch (Exception ex)
167+
{
168+
CosServerException cosServerException = new CosServerException((int)httpWebResponse.StatusCode, ex.Message);
169+
cosServerException.requestId = requestId;
170+
throw cosServerException;
171+
}
155172

156173
response.OnFinish(response.Code >= 200 && response.Code < 300, null);
157174

@@ -312,6 +329,7 @@ public static void AsyncResponseCallback(IAsyncResult ar)
312329
{
313330
if (requestState.retryIndex < MaxRetries)
314331
{
332+
//重试
315333
Schedue(requestState.request, requestState.response, config, requestState.retryIndex + 1);
316334
return;
317335
}
@@ -351,6 +369,7 @@ public static void AsyncResponseCallback(IAsyncResult ar)
351369
{
352370
if (requestState.retryIndex < MaxRetries)
353371
{
372+
//重试
354373
Schedue(requestState.request, requestState.response, config, requestState.retryIndex + 1);
355374
return;
356375
}

QCloudCSharpSDK/COSXML/Network/HttpClient.cs

Lines changed: 21 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -145,9 +145,13 @@ public void InternalExcute(CosRequest cosRequest, CosResult cosResult, QCloudCre
145145
}
146146
catch (CosServerException serverException)
147147
{
148-
// 服务端5xx才重试
149-
if (serverException.statusCode >= 500 && retryIndex < MaxRetry)
148+
// webCode >= 300
149+
if (retryIndex < MaxRetry && serverException.statusCode >= 300)
150150
{
151+
if (serverException.requestId == String.Empty)
152+
{
153+
cosRequest.changeDefaultDomain = true;
154+
}
151155
InternalExcute(cosRequest, cosResult, credentialProvider, retryIndex + 1);
152156
}
153157
else
@@ -157,7 +161,7 @@ public void InternalExcute(CosRequest cosRequest, CosResult cosResult, QCloudCre
157161
}
158162
catch (CosClientException)
159163
{
160-
// 客户端异常都重试
164+
// 客户端异常都重试,如本地文件path写错则报警
161165
if (retryIndex < MaxRetry)
162166
{
163167
InternalExcute(cosRequest, cosResult, credentialProvider, retryIndex + 1);
@@ -170,9 +174,17 @@ public void InternalExcute(CosRequest cosRequest, CosResult cosResult, QCloudCre
170174
}
171175
catch (Exception ex)
172176
{
173-
// 未知异常也重试
174-
if (retryIndex < MaxRetry)
177+
if (retryIndex < MaxRetry)//请求超时或者其它异常
175178
{
179+
bool isOperationTimeOu = ex.ToString().Contains("The operation has timed out");
180+
if (isOperationTimeOu)
181+
{
182+
cosRequest.operationTimeOutRetry = true;
183+
}
184+
else
185+
{
186+
cosRequest.changeDefaultDomain = true;
187+
}
176188
InternalExcute(cosRequest, cosResult, credentialProvider, retryIndex + 1);
177189
}
178190
else
@@ -204,7 +216,7 @@ public void InternalExcute(CosRequest cosRequest, CosResult cosResult, QCloudCre
204216
// }
205217
// }
206218

207-
public void InternalSchedue(CosRequest cosRequest, CosResult cosResult, COSXML.Callback.OnSuccessCallback<CosResult> successCallback, COSXML.Callback.OnFailedCallback failCallback, QCloudCredentialProvider credentialProvider)
219+
public void InternalSchedue(CosRequest cosRequest, CosResult cosResult, COSXML.Callback.OnSuccessCallback<CosResult> successCallback, COSXML.Callback.OnFailedCallback failCallback, QCloudCredentialProvider credentialProvider, int retryIndex = 0)
208220
{
209221

210222
try
@@ -230,7 +242,7 @@ public void InternalSchedue(CosRequest cosRequest, CosResult cosResult, COSXML.C
230242
}
231243
catch (CosServerException serverException)
232244
{
233-
//throw serverException;
245+
//throw clientException;
234246
failCallback(null, serverException);
235247
}
236248
catch (CosClientException clientException)
@@ -416,7 +428,6 @@ public override void HandleResponseHeader()
416428
cosResult.httpMessage = Message;
417429
cosResult.responseHeaders = Headers;
418430
cosResult.InternalParseResponseHeaders();
419-
420431
if (Code >= 300)
421432
{
422433
this.Body.ParseStream = PaserServerError;
@@ -431,9 +442,10 @@ public void PaserServerError(Stream inputStream, string contentType, long conten
431442
{
432443
CosServerException cosServerException = new CosServerException(cosResult.httpCode, cosResult.httpMessage);
433444
List<string> values;
434-
445+
435446
Headers.TryGetValue("x-cos-request-id", out values);
436447
cosServerException.requestId = (values != null && values.Count > 0) ? values[0] : null;
448+
437449
Headers.TryGetValue("x-cos-trace-id", out values);
438450
cosServerException.traceId = (values != null && values.Count > 0) ? values[0] : null;
439451

@@ -452,7 +464,6 @@ public void PaserServerError(Stream inputStream, string contentType, long conten
452464

453465
}
454466
}
455-
456467
throw cosServerException;
457468
}
458469

0 commit comments

Comments
 (0)