Skip to content

Commit

Permalink
format code
Browse files Browse the repository at this point in the history
  • Loading branch information
konakonall committed Nov 19, 2020
1 parent 1682d0b commit dbdf023
Show file tree
Hide file tree
Showing 52 changed files with 532 additions and 656 deletions.
38 changes: 25 additions & 13 deletions COSXMLDemo/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,17 +13,20 @@

namespace COSXMLDemo
{
class Program
public class Program
{

static string bucket = @"bucket-4-csharp-demo-1253653367";
internal static string bucket = @"bucket-4-csharp-demo-1253653367";

static void Main(string[] args)
public static void Main(string[] args)
{

string secretId = Environment.GetEnvironmentVariable("COS_KEY"); // 腾讯云 SecretId
string secretKey = Environment.GetEnvironmentVariable("COS_SECRET"); // 腾讯云 SecretKey
string region = "ap-guangzhou"; // 存储桶所在地域
// 腾讯云 SecretId
string secretId = Environment.GetEnvironmentVariable("COS_KEY");
// 腾讯云 SecretKey
string secretKey = Environment.GetEnvironmentVariable("COS_SECRET");
// 存储桶所在地域
string region = "ap-guangzhou";

// 普通初始化方式
CosXmlConfig config = new CosXmlConfig.Builder()
Expand All @@ -46,7 +49,8 @@ static void Main(string[] args)
// service 初始化完成
CosXmlServer cosXml = new CosXmlServer(config, qCloudCredentialProvider);

try {
try
{
// 创建存储痛
Console.WriteLine(" ======= Put Bucket ======");
putBucket(cosXml);
Expand All @@ -66,15 +70,18 @@ static void Main(string[] args)
catch (COSXML.CosException.CosServerException serverEx)
{
Console.WriteLine("CosServerException: " + serverEx.GetInfo());
} finally {
} finally
{
// 删除存储桶
Console.WriteLine(" ======= Delete Bucket ======");
deleteBucket(cosXml);
}

Console.WriteLine(" ======= Program End. ======");
}

static void putBucket(CosXmlServer cosXml) {
internal static void putBucket(CosXmlServer cosXml)
{
try
{
PutBucketRequest request = new PutBucketRequest(bucket);
Expand All @@ -89,21 +96,25 @@ static void putBucket(CosXmlServer cosXml) {
if (serverEx.statusCode != 409)
{
throw serverEx;
} else {
}
else
{
Console.WriteLine("Bucket Already exists.");
}
}
}

static void deleteBucket(CosXmlServer cosXml) {
internal static void deleteBucket(CosXmlServer cosXml)
{
DeleteBucketRequest request = new DeleteBucketRequest(bucket);

DeleteBucketResult result = cosXml.DeleteBucket(request);

Console.WriteLine(result.GetResultInfo());
}

static string putObject(CosXmlServer cosXml) {
internal static string putObject(CosXmlServer cosXml)
{
string cosKey = "cosKey";
byte[] tmpData = new byte[1024];

Expand All @@ -116,7 +127,8 @@ static string putObject(CosXmlServer cosXml) {
return cosKey;
}

static void deleteObject(CosXmlServer cosXml, string cosKey) {
internal static void deleteObject(CosXmlServer cosXml, string cosKey)
{
DeleteObjectRequest request = new DeleteObjectRequest(bucket, cosKey);

DeleteObjectResult result = cosXml.DeleteObject(request);
Expand Down
20 changes: 10 additions & 10 deletions QCloudCSharpSDK/COSXML/Auth/QCloudCredentialProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -37,20 +37,20 @@ public DefaultQCloudCredentialProvider(string secretId, string secretKey, long k

public override QCloudCredentials GetQCloudCredentials()
{
long keyStartTime = TimeUtils.GetCurrentTime(TimeUnit.SECONDS);
long keyStartTime = TimeUtils.GetCurrentTime(TimeUnit.Seconds);

long keyEndTime = keyStartTime + keyTimDuration;

string keyTime = String.Format("{0};{1}", keyStartTime, keyEndTime);

if (secretId == null)
{
throw new CosClientException((int)CosClientError.INVALID_CREDENTIALS, "secretId == null");
throw new CosClientException((int)CosClientError.InvalidCredentials, "secretId == null");
}

if (secretKey == null)
{
throw new CosClientException((int)CosClientError.INVALID_CREDENTIALS, "secretKey == null");
throw new CosClientException((int)CosClientError.InvalidCredentials, "secretKey == null");
}

string signKey = DigestUtils.GetHamcSha1ToHexString(keyTime, Encoding.UTF8, secretKey, Encoding.UTF8);
Expand All @@ -61,7 +61,7 @@ public override QCloudCredentials GetQCloudCredentials()
public override void Refresh()
{
//TODO update value
QLog.D("DefaultQCloudCredentialProvider", "need to update QCloudCredentials");
QLog.Debug("DefaultQCloudCredentialProvider", "need to update QCloudCredentials");
//invoke SetSetQCloudCredential(string secretId, string secretKey, string keyTime)
}
}
Expand All @@ -80,7 +80,7 @@ public class DefaultSessionQCloudCredentialProvider : QCloudCredentialProvider
private string token;

public DefaultSessionQCloudCredentialProvider(string tmpSecretId, string tmpSecretKey, long tmpExpiredTime, string sessionToken)
: this(tmpSecretId, tmpSecretKey, TimeUtils.GetCurrentTime(TimeUnit.SECONDS), tmpExpiredTime, sessionToken)
: this(tmpSecretId, tmpSecretKey, TimeUtils.GetCurrentTime(TimeUnit.Seconds), tmpExpiredTime, sessionToken)
{
}

Expand All @@ -102,17 +102,17 @@ public override QCloudCredentials GetQCloudCredentials()

if (tmpSecretId == null)
{
throw new CosClientException((int)CosClientError.INVALID_CREDENTIALS, "secretId == null");
throw new CosClientException((int)CosClientError.InvalidCredentials, "secretId == null");
}

if (tmpSecretKey == null)
{
throw new CosClientException((int)CosClientError.INVALID_CREDENTIALS, "secretKey == null");
throw new CosClientException((int)CosClientError.InvalidCredentials, "secretKey == null");
}

if (keyTime == null)
{
throw new CosClientException((int)CosClientError.INVALID_CREDENTIALS, "keyTime == null");
throw new CosClientException((int)CosClientError.InvalidCredentials, "keyTime == null");
}

string signKey = DigestUtils.GetHamcSha1ToHexString(keyTime, Encoding.UTF8, tmpSecretKey, Encoding.UTF8);
Expand All @@ -123,7 +123,7 @@ public override QCloudCredentials GetQCloudCredentials()
public override void Refresh()
{
//TODO update value
QLog.D("DefaultSessionQCloudCredentialProvider", "need to update QCloudCredentials");
QLog.Debug("DefaultSessionQCloudCredentialProvider", "need to update QCloudCredentials");
//invoke SetQCloudCredential(string tmpSecretId, string tmpSecretKey, string tmpkeyTime, string sessionToken)
}

Expand All @@ -140,7 +140,7 @@ public bool IsNeedUpdateNow()
long endTime = -1L;

long.TryParse(keyTime.Substring(index + 1), out endTime);
long nowTime = TimeUtils.GetCurrentTime(TimeUnit.SECONDS);
long nowTime = TimeUtils.GetCurrentTime(TimeUnit.Seconds);

if (endTime <= nowTime)
{
Expand Down
27 changes: 16 additions & 11 deletions QCloudCSharpSDK/COSXML/Auth/QCloudSigner.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,17 +14,17 @@ namespace COSXML.Auth
/// <summary>
/// 计算请求签名
/// </summary>
public interface QCloudSigner
public interface IQCloudSigner
{
void Sign(Request request, QCloudSignSource qcloudSignSource, QCloudCredentials qcloudCredentials);
void Sign(Request request, IQCloudSignSource qcloudSignSource, QCloudCredentials qcloudCredentials);
}

public interface QCloudSignSource
public interface IQCloudSignSource
{
string Source(Request request);
}

public sealed class CosXmlSignSourceProvider : QCloudSignSource
public sealed class CosXmlSignSourceProvider : IQCloudSignSource
{
private HashSet<string> parameterKeys;

Expand All @@ -47,7 +47,7 @@ public CosXmlSignSourceProvider()
this.signAll = true;
}

public void setSignAll(Boolean signAll)
public void SetSignAll(Boolean signAll)
{
this.signAll = signAll;
}
Expand Down Expand Up @@ -179,7 +179,10 @@ public string Source(Request request)
headerKeys.Add("content-length");
}
}
catch (Exception) { }
catch (Exception)
{

}
}

Dictionary<string, string> sourceParameters = request.Url.GetQueryParameters();
Expand Down Expand Up @@ -377,11 +380,14 @@ public void LowerAndSort(List<string> list)

}

public sealed class CosXmlSigner : QCloudSigner
public sealed class CosXmlSigner : IQCloudSigner
{
public CosXmlSigner() { }
public CosXmlSigner()
{

}

public void Sign(Request request, QCloudSignSource qcloudSignSource, QCloudCredentials qcloudCredentials)
public void Sign(Request request, IQCloudSignSource qcloudSignSource, QCloudCredentials qcloudCredentials)
{

if (request == null)
Expand Down Expand Up @@ -436,8 +442,7 @@ public void Sign(Request request, QCloudSignSource qcloudSignSource, QCloudCrede
}
}

public static string GenerateSign(string method, string path, Dictionary<string, string> queryParameters, Dictionary<string, string> headers,
string signTime, QCloudCredentials qcloudCredentials)
public static string GenerateSign(string method, string path, Dictionary<string, string> queryParameters, Dictionary<string, string> headers, string signTime, QCloudCredentials qcloudCredentials)
{

if (qcloudCredentials == null)
Expand Down
20 changes: 10 additions & 10 deletions QCloudCSharpSDK/COSXML/Common/CosClientError.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,33 +10,33 @@ namespace COSXML.Common
public enum CosClientError
{
[CosValue("InvalidArgument")]
INVALID_ARGUMENT = 10000,
InvalidArgument = 10000,

[CosValue("InvalidCredentials")]
INVALID_CREDENTIALS = 10001,
InvalidCredentials = 10001,

[CosValue("BadRequest")]
BAD_REQUEST = 10002,
BadRequest = 10002,

[CosValue("SinkSourceNotFound")]
SINK_SOURCE_NOT_FOUND = 10003,
SinkSourceNotFound = 10003,

[CosValue("InternalError")]
INTERNA_LERROR = 20000,
InternalError = 20000,

[CosValue("ServerError")]
SERVER_ERROR = 20001,
ServerError = 20001,

[CosValue("IOError")]
IO_ERROR = 20002,
IOError = 20002,

[CosValue("PoorNetwork")]
POOR_NETWORK = 20003,
PoorNetwork = 20003,

[CosValue("UserCancelled")]
USER_CANCELLED = 30000,
UserCancelled = 30000,

[CosValue("AlreadyFinished")]
ALREADY_FINISHED = 30001,
AlredyFinished = 30001,
}
}
Loading

0 comments on commit dbdf023

Please sign in to comment.