Skip to content

Commit

Permalink
Merge pull request #38 from tencentyun/feature_nedzzhang_2918d852
Browse files Browse the repository at this point in the history
Feature nedzzhang 2918d852
  • Loading branch information
learningZhang authored Apr 12, 2024
2 parents c5891e9 + 9ded23e commit 8c1dedd
Show file tree
Hide file tree
Showing 14 changed files with 453 additions and 25 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,7 @@ QCloudCSharpSDK/COSXMLTests/obj
QCloudCSharpSDK/COSXMLTests/bin
QCloudCSharpSDK/COSXMLTests/TestResults
QCloudCSharpSDK/COSXMLTests/coveragereport
QCloudCSharpSDK/NCOS
QCloudCSharpSDK/.idea
*.vs
.vscode
2 changes: 1 addition & 1 deletion QCloudCSharpSDK/COSXML/COSXML-Netcore.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
<Deterministic>true</Deterministic>

<PackageId>Tencent.QCloud.Cos.Sdk</PackageId>
<Version>5.4.35.0</Version>
<Version>5.4.36.0</Version>
<Authors>Tencent</Authors>
<Company>Tencent</Company>
<description>Tencent Cloud COS(Cloud Object Service) .Net SDK</description>
Expand Down
2 changes: 1 addition & 1 deletion QCloudCSharpSDK/COSXML/Common/CosVersion.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ namespace COSXML.Common
{
public sealed class CosVersion
{
private static string SDKVersion = "5.4.35.0";
private static string SDKVersion = "5.4.36.0";

public static string GetUserAgent()
{
Expand Down
25 changes: 23 additions & 2 deletions QCloudCSharpSDK/COSXML/Model/Bucket/BucketRequest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -85,8 +85,29 @@ public override string GetHost()
.Append(".myqcloud.com");
}
}

return hostBuilder.ToString();

String hostStr = hostBuilder.ToString();

if (userKeepDefaultDomain && !operationTimeOutRetry)
{
return hostStr;
}

if (operationTimeOutRetry || changeDefaultDomain)
{
StringBuilder pattern = new StringBuilder();
pattern.Append(".cos.").Append(region).Append(".myqcloud.com");
String patternStr = pattern.ToString();

if (hostStr.EndsWith(patternStr))
{
StringBuilder replace = new StringBuilder();
replace.Append(".cos.").Append(region).Append(".tencentcos.cn");
return hostStr.Replace(patternStr, replace.ToString());
}
}

return hostStr;
}

public override void CheckParameters()
Expand Down
9 changes: 8 additions & 1 deletion QCloudCSharpSDK/COSXML/Model/CosRequest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,12 @@ public abstract class CosRequest
/// </summary>
protected bool needMD5 = true;

public bool userKeepDefaultDomain = false;

public bool changeDefaultDomain = false;

public bool operationTimeOutRetry = false;

/// <summary>
/// 请求预签名URL
/// </summary>
Expand Down Expand Up @@ -339,7 +345,8 @@ public virtual CosXmlSignSourceProvider GetSignSourceProvider()
"response-content-type",
"response-expires",
"transfer-encoding",
"versionid"
"versionid",
"pic-operations"
});

foreach (KeyValuePair<string, string> pair in headers)
Expand Down
25 changes: 23 additions & 2 deletions QCloudCSharpSDK/COSXML/Model/Object/ObjectRequest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -114,8 +114,29 @@ public override string GetHost()
.Append(".myqcloud.com");
}
}

return hostBuilder.ToString();

String hostStr = hostBuilder.ToString();

if (userKeepDefaultDomain && !operationTimeOutRetry)
{
return hostStr;
}

if (operationTimeOutRetry || changeDefaultDomain)
{
StringBuilder pattern = new StringBuilder();
pattern.Append(".cos.").Append(region).Append(".myqcloud.com");
String patternStr = pattern.ToString();

if (hostStr.EndsWith(patternStr))
{
StringBuilder replace = new StringBuilder();
replace.Append(".cos.").Append(region).Append(".tencentcos.cn");
return hostStr.Replace(patternStr, replace.ToString());
}
}

return hostStr;
}

public override void CheckParameters()
Expand Down
1 change: 1 addition & 0 deletions QCloudCSharpSDK/COSXML/Model/Object/PutObjectRequest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -212,6 +212,7 @@ public override Network.RequestBody GetRequestBody()
"stream offset + contentLength greater than stream.Length");
}
body = new StreamRequestBody(stream, fileOffset, contentLength);
body.ProgressCallback = progressCallback;
}

return body;
Expand Down
27 changes: 23 additions & 4 deletions QCloudCSharpSDK/COSXML/Network/CommandTask.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
using System.Reflection;
using System.IO;
using System.Net.Cache;
using COSXML.CosException;
using COSXML.Model.Tag;


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

httpWebRequest.AllowWriteStreamBuffering = false;

//bind webRequest
request.BindHttpWebRequest(httpWebRequest);

Expand All @@ -77,7 +79,6 @@ public static void Excute(Request request, Response response, HttpClientConfig c
}
catch (WebException webEx)
{

if (webEx.Response != null && webEx.Response is HttpWebResponse)
{
//notify has been got response
Expand Down Expand Up @@ -149,9 +150,25 @@ private static void HandleHttpWebRequest(HttpWebRequest httpWebRequest, Request
private static void HandleHttpWebResponse(HttpWebResponse httpWebResponse, Response response)
{
HandleHttpWebResponseHeaders(response, httpWebResponse);

string requestId = httpWebResponse.GetResponseHeader("x-cos-request-id");
//handle body
response.Body.HandleResponseBody(httpWebResponse.GetResponseStream());
if (requestId == String.Empty)
{
CosServerException cosServerException = new CosServerException((int)httpWebResponse.StatusCode, "request has error");
cosServerException.requestId = requestId;
throw cosServerException;
}

try
{
response.Body.HandleResponseBody(httpWebResponse.GetResponseStream());
}
catch (Exception ex)
{
CosServerException cosServerException = new CosServerException((int)httpWebResponse.StatusCode, ex.Message);
cosServerException.requestId = requestId;
throw cosServerException;
}

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

Expand Down Expand Up @@ -312,6 +329,7 @@ public static void AsyncResponseCallback(IAsyncResult ar)
{
if (requestState.retryIndex < MaxRetries)
{
//重试
Schedue(requestState.request, requestState.response, config, requestState.retryIndex + 1);
return;
}
Expand Down Expand Up @@ -351,6 +369,7 @@ public static void AsyncResponseCallback(IAsyncResult ar)
{
if (requestState.retryIndex < MaxRetries)
{
//重试
Schedue(requestState.request, requestState.response, config, requestState.retryIndex + 1);
return;
}
Expand Down
31 changes: 21 additions & 10 deletions QCloudCSharpSDK/COSXML/Network/HttpClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -145,9 +145,13 @@ public void InternalExcute(CosRequest cosRequest, CosResult cosResult, QCloudCre
}
catch (CosServerException serverException)
{
// 服务端5xx才重试
if (serverException.statusCode >= 500 && retryIndex < MaxRetry)
// webCode >= 300
if (retryIndex < MaxRetry && serverException.statusCode >= 300)
{
if (serverException.requestId == String.Empty)
{
cosRequest.changeDefaultDomain = true;
}
InternalExcute(cosRequest, cosResult, credentialProvider, retryIndex + 1);
}
else
Expand All @@ -157,7 +161,7 @@ public void InternalExcute(CosRequest cosRequest, CosResult cosResult, QCloudCre
}
catch (CosClientException)
{
// 客户端异常都重试
// 客户端异常都重试,如本地文件path写错则报警
if (retryIndex < MaxRetry)
{
InternalExcute(cosRequest, cosResult, credentialProvider, retryIndex + 1);
Expand All @@ -170,9 +174,17 @@ public void InternalExcute(CosRequest cosRequest, CosResult cosResult, QCloudCre
}
catch (Exception ex)
{
// 未知异常也重试
if (retryIndex < MaxRetry)
if (retryIndex < MaxRetry)//请求超时或者其它异常
{
bool isOperationTimeOu = ex.ToString().Contains("The operation has timed out");
if (isOperationTimeOu)
{
cosRequest.operationTimeOutRetry = true;
}
else
{
cosRequest.changeDefaultDomain = true;
}
InternalExcute(cosRequest, cosResult, credentialProvider, retryIndex + 1);
}
else
Expand Down Expand Up @@ -204,7 +216,7 @@ public void InternalExcute(CosRequest cosRequest, CosResult cosResult, QCloudCre
// }
// }

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

try
Expand All @@ -230,7 +242,7 @@ public void InternalSchedue(CosRequest cosRequest, CosResult cosResult, COSXML.C
}
catch (CosServerException serverException)
{
//throw serverException;
//throw clientException;
failCallback(null, serverException);
}
catch (CosClientException clientException)
Expand Down Expand Up @@ -416,7 +428,6 @@ public override void HandleResponseHeader()
cosResult.httpMessage = Message;
cosResult.responseHeaders = Headers;
cosResult.InternalParseResponseHeaders();

if (Code >= 300)
{
this.Body.ParseStream = PaserServerError;
Expand All @@ -431,9 +442,10 @@ public void PaserServerError(Stream inputStream, string contentType, long conten
{
CosServerException cosServerException = new CosServerException(cosResult.httpCode, cosResult.httpMessage);
List<string> values;

Headers.TryGetValue("x-cos-request-id", out values);
cosServerException.requestId = (values != null && values.Count > 0) ? values[0] : null;

Headers.TryGetValue("x-cos-trace-id", out values);
cosServerException.traceId = (values != null && values.Count > 0) ? values[0] : null;

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

}
}

throw cosServerException;
}

Expand Down
Loading

0 comments on commit 8c1dedd

Please sign in to comment.