public QCloudCOS(string appid, string region, string secretId, string secretKey, string bucket) { _bucket = bucket; //初始化 CosXmlConfig //string appid = "1258572948";//设置腾讯云账户的账户标识 APPID //string region = "ap-chengdu"; //设置一个默认的存储桶地域 // string secretId = "AKIDKi1vlbNKDvSqOtPONBsPkfdOyNdhGWxS"; //"云 API 密钥 SecretId"; // string secretKey = "Vq14wcIe1wt3HNK1ksCMj4LVBlEcgls3"; //"云 API 密钥 SecretKey"; config = new CosXmlConfig.Builder() .SetConnectionTimeoutMs(5000) //设置连接超时时间,单位 毫秒 ,默认 45000ms .SetReadWriteTimeoutMs(5000) //设置读写超时时间,单位 毫秒 ,默认 45000ms .IsHttps(true) //设置默认 https 请求 .SetAppid(appid) //设置腾讯云账户的账户标识 APPID .SetRegion(region) //设置一个默认的存储桶地域 .SetDebugLog(true) //显示日志 .Build(); //创建 CosXmlConfig 对象 //方式1, 永久密钥 long durationSecond = 600; //secretKey 有效时长,单位为 秒 cosCredentialProvider = new DefaultQCloudCredentialProvider(secretId, secretKey, durationSecond); //初始化 CosXmlServer cosXml = new CosXmlServer(config, cosCredentialProvider); }
/// <summary> /// 初始化腾讯云配置 /// </summary> private void CreateCosXML() { CoreEngine.Current.Logger.Info($"主程序引擎:加载COS配置文件 CosXmlConfig"); //初始化 CosXmlConfig string appid = GlobalConfig.AppID; //设置腾讯云账户的账户标识 APPID string region = GlobalConfig.Region; //设置一个默认的存储桶地域 CosXmlConfig config = new CosXmlConfig.Builder() .SetConnectionTimeoutMs(GlobalConfig.ConnectionTimeoutMs) //设置连接超时时间,单位毫秒,默认45000ms .SetReadWriteTimeoutMs(GlobalConfig.ReadWriteTimeoutMs) //设置读写超时时间,单位毫秒,默认45000ms .IsHttps(true) //设置默认 HTTPS 请求 .SetAppid(appid) //设置腾讯云账户的账户标识 APPID .SetRegion(region) //设置一个默认的存储桶地域 .SetDebugLog(true) //显示日志 .Build(); //创建 CosXmlConfig 对象 //初始化 QCloudCredentialProvider,COS SDK 中提供了3种方式:永久密钥、临时密钥、自定义 QCloudCredentialProvider cosCredentialProvider = null; //方式1, 永久密钥 string secretId = new SecretEtt().SecretId; //"云 API 密钥 SecretId"; string secretKey = new SecretEtt().SecretKey; //"云 API 密钥 SecretKey"; long durationSecond = 600; //每次请求签名有效时长,单位为秒 cosCredentialProvider = new DefaultQCloudCredentialProvider(secretId, secretKey, durationSecond); CoreEngine.Current.Logger.Info($"主程序引擎:初始化COS"); //初始化 CosXmlServer CosXml = new CosXmlServer(config, cosCredentialProvider); }
/// <summary> /// add authorization /// </summary> /// <param name="qcloudSignSource">QCloudSignSource</param> /// <param name="request"></param> /// <param name="credentialProvider"></param> private void CheckSign(IQCloudSignSource qcloudSignSource, Request request, QCloudCredentialProvider credentialProvider) { // has authorizaiton, notice: using request.Headers, otherwise, error if (request.Headers.ContainsKey(CosRequestHeaderKey.AUTHORIZAIION)) { QLog.Debug(TAG, "has add authorizaiton in headers"); return; } //has no authorization, but signSourceProvider == null if (qcloudSignSource == null) { QLog.Debug(TAG, "signSourceProvider == null"); return; } if (credentialProvider == null) { throw new ArgumentNullException("credentialsProvider == null"); } CosXmlSigner signer = new CosXmlSigner(); signer.Sign(request, qcloudSignSource, credentialProvider.GetQCloudCredentialsCompat(request)); }
/// <summary> /// excute request /// </summary> /// <param name="cosRequest"></param> /// <param name="cosResult"></param> /// <param name="credentialProvider"></param> /// <exception cref="COSXML.CosException.CosClientException">CosClientException</exception> /// <exception cref="COSXML.CosException.CosServerException">CosServerException</exception> public void Excute(CosRequest cosRequest, CosResult cosResult, QCloudCredentialProvider credentialProvider) { //HttpTask httpTask = new HttpTask(); //httpTask.cosRequest = cosRequest; //httpTask.cosResult = cosResult; //httpTask.isSchedue = false; InternalExcute(cosRequest, cosResult, credentialProvider); }
public static void Init(HttpClientConfig config, QCloudCredentialProvider credentialsProvider) { lock (sync) { HttpClient.config = config; HttpClient.credentialsProvider = credentialsProvider; // init grobal httpwebreqeust CommandTask.Init(HttpClient.config); } }
public static void Init(HttpClientConfig config, QCloudCredentialProvider credentialsProvider) { lock (sync) { if (config == null) { throw new CosClientException((int)CosClientError.INVALID_ARGUMENT, "HttpClientConfig = null"); } HttpClient.config = config; HttpClient.credentialsProvider = credentialsProvider; // init grobal httpwebreqeust CommandTask.Init(HttpClient.config); } }
/// <summary> /// 创建一个新实例 /// </summary> /// <param name="config">服务配置</param> /// <param name="qcloudCredentailProvider">凭证提供者</param> public CosXmlServer(CosXmlConfig config, QCloudCredentialProvider qcloudCredentailProvider) { if (config == null) { throw new CosClientException((int)CosClientError.InvalidArgument, "Config is null."); } this.config = config; if (this.config.IsDebugLog) { QLog.AddLogAdapter(new LogImpl()); } this.credentialProvider = qcloudCredentailProvider; httpClient = HttpClient.GetInstance(); httpClient.Init(this.config.HttpConfig); }
public CosXmlServer(CosXmlConfig config, QCloudCredentialProvider qcloudCredentailProvider) { if (config != null) { this.config = config; } else { this.config = new CosXmlConfig.Builder().Build(); } if (this.config.IsDebugLog) { QLog.AddLogAdapter(new LogImpl()); } this.qcloudCredentailProvider = qcloudCredentailProvider; HttpClient.Init(this.config.HttpConfig, this.qcloudCredentailProvider); httpClient = HttpClient.GetInstance(); }
/// <summary> /// excute request /// </summary> /// <param name="cosRequest"></param> /// <param name="cosResult"></param> /// <param name="credentialProvider"></param> /// <exception cref="COSXML.CosException.CosClientException">CosClientException</exception> /// <exception cref="COSXML.CosException.CosServerException">CosServerException</exception> public void InternalExcute(CosRequest cosRequest, CosResult cosResult, QCloudCredentialProvider credentialProvider) { try { Request request = CreateRequest(cosRequest, credentialProvider); //extern informations exchange cosResult.ExternInfo(cosRequest); Response response; if (cosRequest is GetObjectRequest) { GetObjectRequest getObjectRequest = cosRequest as GetObjectRequest; response = new CosResponse(cosResult, getObjectRequest.GetSaveFilePath(), getObjectRequest.GetLocalFileOffset(), getObjectRequest.GetCosProgressCallback()); } else if (cosRequest is GetSnapshotRequest) { GetSnapshotRequest getSnapshotRequest = cosRequest as GetSnapshotRequest; response = new CosResponse(cosResult, getSnapshotRequest.GetSaveFilePath(), 0, null); } else { response = new CosResponse(cosResult, null, -1L, null); } cosRequest.BindRequest(request); CommandTask.Excute(request, response, config); } catch (CosServerException) { throw; } catch (CosClientException) { throw; } catch (Exception ex) { throw new CosClientException((int)CosClientError.BadRequest, ex.Message, ex); } }
public WFwCosClient(CosOptions cos, ILogger <WFwCosClient> l, Func <TempCredentialResult> func) { options = cos; logger = l; CosXmlConfig config = new CosXmlConfig.Builder() .SetRegion(options.Regin) //设置一个默认的存储桶地域 .Build(); if (cos.UseTemp) { qCloudCredentialProvider = new CustomQCloudCredentialProvider(func); } else { qCloudCredentialProvider = new DefaultQCloudCredentialProvider(options.SecretId, options.SecretKey, options.DurationSec); } cosXml = new CosXmlServer(config, qCloudCredentialProvider); }
public void globalInit() { //.cssg-snippet-body-start:[global-init] //初始化 CosXmlConfig string appid = "1253653367"; //设置腾讯云账户的账户标识 APPID string region = "ap-guangzhou"; //设置一个默认的存储桶地域 CosXmlConfig config = new CosXmlConfig.Builder() .SetConnectionTimeoutMs(60000) //设置连接超时时间,单位毫秒,默认45000ms .SetReadWriteTimeoutMs(40000) //设置读写超时时间,单位毫秒,默认45000ms .IsHttps(true) //设置默认 HTTPS 请求 .SetAppid(appid) //设置腾讯云账户的账户标识 APPID .SetRegion(region) //设置一个默认的存储桶地域 .SetDebugLog(true) //显示日志 .Build(); //创建 CosXmlConfig 对象 //初始化 QCloudCredentialProvider,COS SDK 中提供了3种方式:永久密钥、临时密钥、自定义 QCloudCredentialProvider cosCredentialProvider = null; //方式1, 永久密钥 string secretId = Environment.GetEnvironmentVariable("COS_KEY"); //"云 API 密钥 SecretId"; string secretKey = Environment.GetEnvironmentVariable("COS_SECRET"); //"云 API 密钥 SecretKey"; long durationSecond = 600; //每次请求签名有效时长,单位为秒 cosCredentialProvider = new DefaultQCloudCredentialProvider(secretId, secretKey, durationSecond); //方式2, 临时密钥 string tmpSecretId = Environment.GetEnvironmentVariable("COS_KEY"); //"临时密钥 SecretId"; string tmpSecretKey = Environment.GetEnvironmentVariable("COS_SECRET"); //"临时密钥 SecretKey"; string tmpToken = "COS_TOKEN"; //"临时密钥 token"; long tmpExpireTime = 1546862502; //临时密钥有效截止时间 cosCredentialProvider = new DefaultSessionQCloudCredentialProvider(tmpSecretId, tmpSecretKey, tmpExpireTime, tmpToken); //初始化 CosXmlServer CosXmlServer cosXml = new CosXmlServer(config, cosCredentialProvider); //.cssg-snippet-body-end }
private void init(string projectId) { ucbFacade = new UcbFacade(); // ucbFacade.setHost("http://localhost"); ucbFacade.setHost("https://api.ccb.unity.cn"); ucbFacade.setPort("443"); CosInfo cosInfo = ucbFacade.GetCredential(projectId); sourceBucket = cosInfo.bucket; deltaBucket = cosInfo.deltaBucket; cosConfig = new CosXmlConfig.Builder() .SetConnectionTimeoutMs(60000) //ms .SetReadWriteTimeoutMs(40000) //ms .IsHttps(true) .SetAppid(cosInfo.appId) .SetRegion(cosInfo.region) .SetDebugLog(true) .Build(); cosProvider = new UcbSessionQCloudCredentialProvider(cosInfo.secretId, cosInfo.secretKey, cosInfo.expireTime, cosInfo.token, projectId, ucbFacade); cosXml = new CosXmlServer(cosConfig, cosProvider); transferManager = new TransferManager(cosXml, new TransferConfig()); }
public CosBuilder SetSecret(string secretId, string secretKey, long durationSecond = 600) { cosCredentialProvider = new DefaultQCloudCredentialProvider(secretId, secretKey, durationSecond); return(this); }
/// <summary> /// 获取Cos操作的CosXml类 /// </summary> /// <param name="tmpSecretId">临时密钥 SecretId</param> /// <param name="tmpSecretKey">临时密钥 SecretKey</param> /// <param name="tmpToken">临时密钥 token</param> /// <param name="tmpExpireTime">临时密钥有效截止时间,精确到秒</param> public CosXml getCosXml(string tmpSecretId, string tmpSecretKey, string tmpToken, long tmpExpireTime) { cosCredentialProvider = new DefaultSessionQCloudCredentialProvider(tmpSecretId, tmpSecretKey, tmpExpireTime, tmpToken); cosXml = new CosXmlServer(config, cosCredentialProvider); return(cosXml); }
// public void Execute(Request request, Response response) // { // try // { // CommandTask.Excute(request, response, config); // } // catch (CosServerException) // { // throw; // } // catch (CosClientException) // { // throw; // } // catch (Exception ex) // { // throw new CosClientException((int)CosClientError.BadRequest, ex.Message, ex); // } // } public void InternalSchedue(CosRequest cosRequest, CosResult cosResult, COSXML.Callback.OnSuccessCallback <CosResult> successCallback, COSXML.Callback.OnFailedCallback failCallback, QCloudCredentialProvider credentialProvider) { try { Request request = CreateRequest(cosRequest, credentialProvider); cosResult.ExternInfo(cosRequest); Response response; if (cosRequest is GetObjectRequest) { GetObjectRequest getObjectRequest = cosRequest as GetObjectRequest; response = new CosResponse(cosResult, getObjectRequest.GetSaveFilePath(), getObjectRequest.GetLocalFileOffset(), getObjectRequest.GetCosProgressCallback(), successCallback, failCallback); } else { response = new CosResponse(cosResult, null, -1L, null, successCallback, failCallback); } cosRequest.BindRequest(request); CommandTask.Schedue(request, response, config); } catch (CosServerException serverException) { //throw serverException; failCallback(null, serverException); } catch (CosClientException clientException) { //throw clientException; failCallback(clientException, null); } catch (Exception ex) { //throw new CosClientException((int)CosClientError.BAD_REQUEST, ex.Message, ex); failCallback(new CosClientException((int)CosClientError.BadRequest, ex.Message, ex), null); } }
public void Schedue(CosRequest cosRequest, CosResult cosResult, COSXML.Callback.OnSuccessCallback <CosResult> successCallback, COSXML.Callback.OnFailedCallback failCallback, QCloudCredentialProvider credentialProvider) { //HttpTask httpTask = new HttpTask(); //httpTask.cosRequest = cosRequest; //httpTask.cosResult = cosResult; //httpTask.isSchedue = true; //httpTask.successCallback = successCallback; //httpTask.failCallback = failCallback; InternalSchedue(cosRequest, cosResult, successCallback, failCallback, credentialProvider); }
private Request CreateRequest(CosRequest cosRequest, QCloudCredentialProvider credentialProvider) { cosRequest.CheckParameters(); string requestUrlWithSign = cosRequest.RequestURLWithSign; Request request = new Request(); request.Method = cosRequest.Method; if (requestUrlWithSign != null) { if (requestUrlWithSign.StartsWith("https")) { request.IsHttps = true; } else { request.IsHttps = false; } request.RequestUrlString = requestUrlWithSign; } else { request.IsHttps = (bool)cosRequest.IsHttps; request.Url = CreateUrl(cosRequest); request.Host = cosRequest.GetHost(); } request.UserAgent = config.UserAgnet; Dictionary <string, string> headers = cosRequest.GetRequestHeaders(); if (headers != null) { foreach (KeyValuePair <string, string> pair in headers) { request.AddHeader(pair.Key, pair.Value); } } request.Body = cosRequest.GetRequestBody(); // cacluate md5 if (CheckNeedMd5(request, cosRequest.IsNeedMD5) && request.Body != null) { request.AddHeader(CosRequestHeaderKey.CONTENT_MD5, request.Body.GetMD5()); } // content type header if (request.Body != null && request.Body.ContentType != null && !request.Headers.ContainsKey(CosRequestHeaderKey.CONTENT_TYPE)) { request.AddHeader(CosRequestHeaderKey.CONTENT_TYPE, request.Body.ContentType); } //cacluate sign, and add it. if (requestUrlWithSign == null) { CheckSign(cosRequest.GetSignSourceProvider(), request, credentialProvider); } return(request); }