/// <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); }
/// <summary> /// 列出指定容器下的对象列表 /// </summary> /// <param name="containerName"></param> /// <returns></returns> public async Task <IList <BlobFileInfo> > ListBlobs(string containerName) { if (!string.IsNullOrWhiteSpace(containerName) && !containerName.EndsWith("/")) { containerName += "/"; } var req = new GetBucketRequest(_tcConfig.BucketName); req.SetPrefix(containerName); var resp = _cosXmlServer.GetBucket(req); await resp.HandlerError("获取对象列表出错!"); var list = resp.listBucket.contentsList .Select(obj => new BlobFileInfo { Container = containerName?.Trim('/'), ETag = obj.eTag, Length = obj.size, LastModified = Convert.ToDateTime(obj.lastModified), Name = obj.key.Replace(containerName, string.Empty), Url = GetUrlByKey(obj.key), //ContentMD5 = }); return(list.ToArray()); }
public void testCustomHost() { string customHost = "www.my.custom.host.com"; // test host CosXmlConfig configWithHost = new CosXmlConfig.Builder() .setHost(customHost) .Build(); string bucket = "bucket-125000"; CosXmlServer cosXml = new CosXmlServer(configWithHost, null); GetBucketRequest bucketRequest = new GetBucketRequest(bucket); try { cosXml.GetBucket(bucketRequest); } catch (Exception e) { // ignore } Assert.AreEqual(customHost, bucketRequest.GetHost()); GetObjectRequest request = new GetObjectRequest(bucket, "aKey", null, null); try { cosXml.GetObject(request); } catch (Exception e) { // ignore } Assert.AreEqual(customHost, request.GetHost()); GetServiceRequest serviceRequest = new GetServiceRequest(); Assert.AreEqual("service.cos.myqcloud.com", serviceRequest.GetHost()); }
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> /// 查询指定路径下面的对象 /// </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 void TestHostEndpoint() { CosXmlConfig config = new CosXmlConfig.Builder() .SetConnectionLimit(512) .SetEndpointSuffix("cos.accelerate.myqcloud.com") .Build(); CosXmlServer cosXml = new CosXmlServer(config, null); string bucket = "bucket-125000"; GetObjectRequest request = new GetObjectRequest(bucket, "aKey", null, null); try { cosXml.GetObject(request); } catch (Exception) { // ignore } Assert.AreEqual(bucket + "." + config.endpointSuffix, request.GetHost()); GetBucketRequest bucketRequest = new GetBucketRequest(bucket); try { cosXml.GetBucket(bucketRequest); } catch (Exception) { // ignore } Assert.AreEqual(bucket + "." + config.endpointSuffix, bucketRequest.GetHost()); // test service request GetServiceRequest serviceRequest = new GetServiceRequest(); Assert.AreEqual("service.cos.myqcloud.com", serviceRequest.GetHost()); string serviceHost = "service.cos.csp.com"; serviceRequest.host = serviceHost; Assert.AreEqual(serviceHost, serviceRequest.GetHost()); }
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 }