/// <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 void GetBucket() { try { GetBucketRequest request = new GetBucketRequest(QCloudServer.Instance().bucketForObjectTest); request.SetDelimiter("/"); request.SetEncodingType("url"); request.SetMaxKeys("1000"); List <string> headerKeys = new List <string>(); headerKeys.Add("Host"); List <string> queryParameters = new List <string>(); queryParameters.Add("prefix"); queryParameters.Add("max-keys"); //执行请求 GetBucketResult result = QCloudServer.Instance().cosXml.GetBucket(request); Assert.True(result.httpCode == 200); // Console.WriteLine(result.GetResultInfo()); Assert.IsNotEmpty((result.GetResultInfo())); var listObjects = result.listBucket; Assert.NotNull(listObjects.delimiter); Assert.NotNull(listObjects.encodingType); Assert.NotNull(listObjects.name); Assert.NotZero(listObjects.maxKeys); Assert.IsFalse(listObjects.isTruncated); Assert.That(listObjects.prefix, Is.Null.Or.Empty); Assert.That(listObjects.marker, Is.Null.Or.Empty); Assert.That(listObjects.nextMarker, Is.Null.Or.Empty); Assert.NotZero(listObjects.commonPrefixesList.Count); foreach (var commonPrefix in listObjects.commonPrefixesList) { Assert.NotNull(commonPrefix.prefix); } Assert.NotZero(listObjects.contentsList.Count); foreach (var content in listObjects.contentsList) { Assert.NotNull(content.eTag); Assert.NotNull(content.owner); Assert.NotNull(content.owner.id); Assert.NotNull(content.key); Assert.NotNull(content.lastModified); Assert.NotNull(content.size); Assert.NotNull(content.storageClass); } } catch (COSXML.CosException.CosClientException clientEx) { Console.WriteLine("CosClientException: " + clientEx.Message); Assert.Fail(); } catch (COSXML.CosException.CosServerException serverEx) { Console.WriteLine("CosServerException: " + serverEx.GetInfo()); Assert.Fail(); } }
/// <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); }