private IEnumerable <Picture> GetPicturesByBucket(string bucket) { var request = new GetBucketRequest(bucket); //设置签名有效时长 request.SetSign(TimeUtils.GetCurrentTime(TimeUnit.SECONDS), _cosConfig.DurationSecond); var response = _cosXml.GetBucket(request); var result = response.listBucket.contentsList .Select(it => { var picture = new Picture { Url = $"{_cosConfig.BaseUrl}/{HttpUtility.HtmlEncode(it.key)}", Size = it.size, ETag = it.eTag }; DateTime.TryParse(it.lastModified, out var time); picture.LastModified = time; return(picture); }) .OrderByDescending(it => it.LastModified); return(result); }
public async Task <ResponseModel> SelectObjectList() { try { string bucket = _buketName + "-" + _appid; //存储桶名称 格式:BucketName-APPID GetBucketRequest request = new GetBucketRequest(bucket); //设置签名有效时长 request.SetSign(TimeUtils.GetCurrentTime(TimeUnit.SECONDS), 600); //执行请求 GetBucketResult result = await Task.FromResult(_cosXml.GetBucket(request)); return(new ResponseModel { Code = 200, Data = result.GetResultInfo() }); } catch (CosClientException clientEx) { return(new ResponseModel { Code = 0, Data = "CosClientException: " + clientEx.Message }); } catch (CosServerException serverEx) { return(new ResponseModel { Code = 0, Data = "CosServerException: " + serverEx.GetInfo() }); } }
/// <summary> /// 获取bukect中的所有文件 /// </summary> /// <param name="bucketName"></param> /// <param name="config"></param> /// <param name="cosCredentialProvider"></param> /// <returns></returns> public static List <CloudFile> ListObjects() { if (BosConfig == null) { return(null); } //初始化 CosXmlServer CosXmlServer cosXml = getCosXmlServer(); GetBucketRequest request = new GetBucketRequest(BosConfig.BucketName); //设置签名有效时长 request.SetSign(TimeUtils.GetCurrentTime(TimeUnit.SECONDS), 600); request.SetMaxKeys(1 + ""); //执行请求 GetBucketResult result = cosXml.GetBucket(request); ListBucket listBucket = result.listBucket; List <ListBucket.Contents> contents = listBucket.contentsList; List <CloudFile> cloudFiles = new List <CloudFile>(); foreach (ListBucket.Contents content in contents) { CloudFile cloudFile = new CloudFile { Key = content.key, Size = content.size, LastModified = content.lastModified }; cloudFiles.Add(cloudFile); } while (result.listBucket.nextMarker != null) { request.SetMarker(result.listBucket.nextMarker); //执行请求 result = cosXml.GetBucket(request); foreach (ListBucket.Contents content in result.listBucket.contentsList) { CloudFile cloudFile = new CloudFile { Key = content.key, Size = content.size, LastModified = content.lastModified }; cloudFiles.Add(cloudFile); } } Console.WriteLine(cloudFiles); return(cloudFiles); }
public static void test(CosXml cosXml) { List <string> keys = new List <string>(); string bucket = "android-demo-ap-guangzhou"; GetBucketRequest getBucket = new GetBucketRequest(bucket); getBucket.Region = EnumUtils.GetValue(CosRegion.AP_Guangzhou); getBucket.SetSign(COSXML.Utils.TimeUtils.GetCurrentTime(TimeUnit.SECONDS), 600); GetBucketResult result = cosXml.GetBucket(getBucket); Console.WriteLine(result.GetResultInfo()); foreach (ListBucket.Contents content in result.listBucket.contentsList) { if (content != null) { keys.Add(content.key); } } if (keys.Count > 0) { COSXML.Model.Object.DeleteMultiObjectRequest deleteMults = new COSXML.Model.Object.DeleteMultiObjectRequest(bucket); deleteMults.SetSign(COSXML.Utils.TimeUtils.GetCurrentTime(TimeUnit.SECONDS), 600); deleteMults.Region = EnumUtils.GetValue(CosRegion.AP_Guangzhou); deleteMults.SetObjectKeys(keys); COSXML.Model.Object.DeleteMultiObjectResult deleteMultisResult = cosXml.DeleteMultiObjects(deleteMults); Console.WriteLine(deleteMultisResult.GetResultInfo()); } ListMultiUploadsRequest listMultis = new ListMultiUploadsRequest(bucket); listMultis.Region = EnumUtils.GetValue(CosRegion.AP_Guangzhou); listMultis.SetSign(TimeUtils.GetCurrentTime(TimeUnit.SECONDS), 600); ListMultiUploadsResult listMultisResult = cosXml.ListMultiUploads(listMultis); Console.WriteLine(listMultisResult.GetResultInfo()); if (listMultisResult.listMultipartUploads.uploads.Count > 0) { foreach (ListMultipartUploads.Upload upload in listMultisResult.listMultipartUploads.uploads) { if (upload == null) { continue; } string key = upload.key; string uploadId = upload.uploadID; COSXML.Model.Object.AbortMultiUploadRequest abort = new COSXML.Model.Object.AbortMultiUploadRequest(bucket, key, uploadId); abort.Region = EnumUtils.GetValue(CosRegion.AP_Guangzhou); abort.SetSign(COSXML.Utils.TimeUtils.GetCurrentTime(TimeUnit.SECONDS), 600); COSXML.Model.Object.AbortMultiUploadResult abortResult = cosXml.AbortMultiUpload(abort); } } }
public void TestSetSign() { GetBucketRequest request = new GetBucketRequest(QCloudServer.Instance().bucketForObjectTest); request.SetSign(TimeUtils.GetCurrentTime(TimeUnit.Seconds), 600); ///执行请求 GetBucketResult result = QCloudServer.Instance().cosXml.GetBucket(request); Assert.AreEqual(result.httpCode, 200); }
/// <summary> /// 查询指定路径下面的对象 /// </summary> /// <param name="tEST"></param> /// <returns></returns> public ResultModel <ListBucket> GetBucketResult(string tEST) { CosXmlConfig config = new CosXmlConfig.Builder() .SetConnectionTimeoutMs(SetConnectionTimeoutMs) //设置连接超时时间,单位毫秒,默认45000ms .SetReadWriteTimeoutMs(SetReadWriteTimeoutMs) //设置读写超时时间,单位毫秒,默认45000ms .IsHttps(IsHttps) //设置默认 HTTPS 请求 .SetAppid(SetAppid) //设置腾讯云账户的账户标识 APPID .SetRegion(SetRegion) //设置一个默认的存储桶地域 .Build(); string secretId = SecretId; //云 API 密钥 SecretId string secretKey = SecretKey; //云 API 密钥 SecretKey long durationSecond = 600; //每次请求签名有效时长,单位为秒 QCloudCredentialProvider qCloudCredentialProvider = new DefaultQCloudCredentialProvider(secretId, secretKey, durationSecond); CosXml cosXml = new CosXmlServer(config, qCloudCredentialProvider); ResultModel <ListBucket> resu = new ResultModel <ListBucket>(); try { string bucket = "yuanguhl"; //格式:BucketName-APPID GetBucketRequest request = new GetBucketRequest(bucket); request.Region = "ap-beijing"; //设置签名有效时长 request.SetSign(TimeUtils.GetCurrentTime(TimeUnit.SECONDS), 600); //获取 a/ 下的对象 if (!string.IsNullOrEmpty(tEST)) { request.SetPrefix(tEST); } //执行请求 GetBucketResult result = cosXml.GetBucket(request); //请求成功 resu.date = result.listBucket; resu.success = true; return(resu); } catch (COSXML.CosException.CosClientException clientEx) { //请求失败 resu.success = false; resu.message = clientEx.InnerException.ToString(); return(resu); } catch (COSXML.CosException.CosServerException serverEx) { //请求失败 resu.success = false; resu.message = serverEx.InnerException.ToString(); return(resu); } }
public IEnumerable <IFileEntry> List() { bool isTruncated = true; string filename = null; while (isTruncated) { cosXml = GetCosXml(); string bucket = _cosOptions.Bucket; string prefix = _cosOptions.Path; GetBucketRequest request = new GetBucketRequest(bucket); request.SetSign(TimeUtils.GetCurrentTime(TimeUnit.SECONDS), 600); if (!string.IsNullOrEmpty(filename)) { request.SetMarker(filename); } if (!string.IsNullOrEmpty(prefix)) { request.SetPrefix(prefix); } GetBucketResult result = cosXml.GetBucket(request); ListBucket info = result.listBucket; isTruncated = result.listBucket.isTruncated; filename = result.listBucket.nextMarker; foreach (var item in info.contentsList) { var last = DateTime.Parse(item.lastModified); var fileName = item.key; if (!string.IsNullOrWhiteSpace(prefix)) { fileName = fileName.Substring(prefix.Length); if (fileName.StartsWith("/", StringComparison.Ordinal)) { fileName = fileName.Trim('/'); } } yield return(new FileEntry(fileName, item.size, last, last)); } } }
public void getBucket() { //.cssg-snippet-body-start:[get-bucket] CosXmlConfig config = new CosXmlConfig.Builder() .SetConnectionTimeoutMs(60000) //设置连接超时时间,单位毫秒,默认45000ms .SetReadWriteTimeoutMs(40000) //设置读写超时时间,单位毫秒,默认45000ms .IsHttps(true) //设置默认 HTTPS 请求 .SetAppid("1253653367") //设置腾讯云账户的账户标识 APPID .SetRegion("ap-guangzhou") //设置一个默认的存储桶地域 .Build(); string secretId = Environment.GetEnvironmentVariable("COS_KEY"); //云 API 密钥 SecretId string secretKey = Environment.GetEnvironmentVariable("COS_SECRET"); //云 API 密钥 SecretKey long durationSecond = 600; //每次请求签名有效时长,单位为秒 QCloudCredentialProvider qCloudCredentialProvider = new DefaultQCloudCredentialProvider(secretId, secretKey, durationSecond); CosXml cosXml = new CosXmlServer(config, qCloudCredentialProvider); try { string bucket = "bucket-cssg-test-dotnet-1253653367"; //格式:BucketName-APPID GetBucketRequest request = new GetBucketRequest(bucket); //设置签名有效时长 request.SetSign(TimeUtils.GetCurrentTime(TimeUnit.SECONDS), 600); //获取 a/ 下的对象 request.SetPrefix("a/"); //执行请求 GetBucketResult result = cosXml.GetBucket(request); //bucket的相关信息 ListBucket info = result.listBucket; } catch (COSXML.CosException.CosClientException clientEx) { //请求失败 Console.WriteLine("CosClientException: " + clientEx); Assert.Null(clientEx); } catch (COSXML.CosException.CosServerException serverEx) { //请求失败 Console.WriteLine("CosServerException: " + serverEx.GetInfo()); Assert.Null(serverEx); } //.cssg-snippet-body-end }
public static void AsynGetBucket(COSXML.CosXml cosXml, string bucket) { GetBucketRequest request = new GetBucketRequest(bucket); request.SetPrefix("a/bed/d"); List <string> queryParameters = new List <string>(); queryParameters.Add("prefix"); queryParameters.Add("max-keys"); //设置签名有效时长 request.SetSign(TimeUtils.GetCurrentTime(TimeUnit.SECONDS), 600, null, queryParameters); ////设置签名有效时长 //request.SetSign(TimeUtils.GetCurrentTime(TimeUnit.SECONDS), 600); ///执行请求 cosXml.GetBucket(request, delegate(CosResult cosResult) { GetBucketResult result = cosResult as GetBucketResult; Console.WriteLine(result.GetResultInfo()); }, delegate(CosClientException clientEx, CosServerException serverEx) { if (clientEx != null) { QLog.D("XIAO", clientEx.Message); Console.WriteLine("CosClientException: " + clientEx.Message); } if (serverEx != null) { QLog.D("XIAO", serverEx.Message); Console.WriteLine("CosServerException: " + serverEx.GetInfo()); } }); }
public static void GetBucket(COSXML.CosXml cosXml, string bucket) { GetBucketRequest request = new GetBucketRequest(bucket); //设置签名有效时长 //request.SetSign(TimeUtils.GetCurrentTime(TimeUnit.SECONDS), 600); request.SetPrefix("a/中文/d"); List <string> headerKeys = new List <string>(); headerKeys.Add("Host"); List <string> queryParameters = new List <string>(); queryParameters.Add("prefix"); queryParameters.Add("max-keys"); request.SetSign(TimeUtils.GetCurrentTime(TimeUnit.SECONDS), 600, headerKeys, queryParameters); //执行请求 GetBucketResult result = cosXml.GetBucket(request); Console.WriteLine(result.GetResultInfo()); //try //{ // GetBucketRequest request = new GetBucketRequest(bucket); // //设置签名有效时长 // //request.SetSign(TimeUtils.GetCurrentTime(TimeUnit.SECONDS), 600); // request.SetPrefix("a/中文/d"); // List<string> headerKeys = new List<string>(); // headerKeys.Add("Host"); // List<string> queryParameters = new List<string>(); // queryParameters.Add("prefix"); // queryParameters.Add("max-keys"); // request.SetSign(TimeUtils.GetCurrentTime(TimeUnit.SECONDS), 600, headerKeys, queryParameters); // //执行请求 // GetBucketResult result = cosXml.GetBucket(request); // Console.WriteLine(result.GetResultInfo()); //} //catch (COSXML.CosException.CosClientException clientEx) //{ // QLog.D("XIAO", clientEx.Message); // Console.WriteLine("CosClientException: " + clientEx.Message); //} //catch (COSXML.CosException.CosServerException serverEx) //{ // QLog.D("XIAO", serverEx.Message); // Console.WriteLine("CosServerException: " + serverEx.GetInfo()); //} }
/// <summary> /// 查找新版本更新文件路径 /// </summary> /// <returns></returns> public Task <(string path, string version)> FindNewUpdate() { CoreEngine.Current.Logger.Info($"检查更新:查找新版本"); var tcsResult = new TaskCompletionSource <(string s, string ss)>(); if (null == SuperEngine.Current.GlobalConfig) { CoreEngine.Current.Logger.Info($"检查更新失败:更新配置为空"); tcsResult.SetResult((null, null)); return(tcsResult.Task); } string fileHeadPath = $@"https://{SuperEngine.Current.GlobalConfig.BucketName}.cos.{SuperEngine.Current.GlobalConfig.Region}.myqcloud.com/"; CoreEngine.Current.Logger.Info($"检查更新:版本更新文件路径:{fileHeadPath}"); try { string bucket = SuperEngine.Current.GlobalConfig.BucketName; //格式:BucketName-APPID GetBucketRequest request = new GetBucketRequest(bucket); //设置签名有效时长 request.SetSign(TimeUtils.GetCurrentTime(TimeUnit.SECONDS), 600); //获取 a/ 下的对象 request.SetPrefix(SuperEngine.Current.GlobalConfig.UpdatePath); if (null == SuperEngine.Current.CosXml) { tcsResult.SetResult((null, null)); return(tcsResult.Task); } request.SetMaxKeys("10"); CoreEngine.Current.Logger.Info($"检查更新:请求网络"); //执行请求 GetBucketResult result = SuperEngine.Current.CosXml.GetBucket(request); //请求成功 var entityList = result.listBucket.contentsList; CoreEngine.Current.Logger.Info($"检查更新:请求成功,返回条目数量:{entityList.Count}"); var list = entityList.OrderByDescending(a => a.lastModified).ToList(); foreach (var ett in list) { var fullPath = Path.Combine(fileHeadPath, ett.key); var extension = Path.GetExtension(fullPath); if (extension == ".bwp") { var fileName = Path.GetFileNameWithoutExtension(fullPath); try { if (SuperEngine.Current.IsVersionNewer(fileName)) { //return fullPath; tcsResult.SetResult((fullPath, fileName)); return(tcsResult.Task); } } catch { break; } } } tcsResult.SetResult((null, null)); return(tcsResult.Task); } catch (COSXML.CosException.CosClientException clientEx) { //请求失败 CoreEngine.Current.Logger.Error(clientEx, $"检查更新失败:COS客户端异常"); Console.WriteLine("CosClientException: " + clientEx); } catch (COSXML.CosException.CosServerException serverEx) { //请求失败 CoreEngine.Current.Logger.Error(serverEx, $"检查更新失败:COS服务端异常"); Console.WriteLine("CosServerException: " + serverEx.GetInfo()); } catch (Exception e) { CoreEngine.Current.Logger.Error(e, $"检查更新失败:其他异常"); } tcsResult.SetResult((null, null)); return(tcsResult.Task); }