public async Task <ItemMeta> GetObjectMetadataAsync(string bucketName, string objectName, string versionID = null, string matchEtag = null, DateTime?modifiedSince = null) { if (string.IsNullOrEmpty(bucketName)) { throw new ArgumentNullException(nameof(bucketName)); } if (string.IsNullOrEmpty(objectName)) { throw new ArgumentNullException(nameof(objectName)); } bucketName = ConvertBucketName(bucketName); HeadObjectRequest request = new HeadObjectRequest(bucketName, objectName); if (!string.IsNullOrEmpty(versionID)) { request.SetVersionId(versionID); } //执行请求 HeadObjectResult result = _client.HeadObject(request); if (!result.IsSuccessful()) { throw new Exception("Query object meta data failed."); } ItemMeta metaData = new ItemMeta() { ObjectName = objectName, Size = result.size, ETag = result.eTag, IsEnableHttps = Options.IsEnableHttps, }; return(await Task.FromResult(metaData)); }
public Task <bool> ObjectsExistsAsync(string bucketName, string objectName) { if (string.IsNullOrEmpty(bucketName)) { throw new ArgumentNullException(nameof(bucketName)); } objectName = FormatObjectName(objectName); bucketName = ConvertBucketName(bucketName); try { //build request HeadObjectRequest request = new HeadObjectRequest(bucketName, objectName); //执行请求 HeadObjectResult result = _client.HeadObject(request); if (result.IsSuccessful()) { return(Task.FromResult(true)); } else { return(Task.FromResult(false)); } } catch (CosServerException ex) { if (ex.statusCode == 404) { return(Task.FromResult(false)); } throw; } }
/// 获取对象信息 public void HeadObject() { //.cssg-snippet-body-start:[head-object] try { string bucket = "examplebucket-1250000000"; //存储桶,格式:BucketName-APPID string key = "exampleobject"; //对象键 HeadObjectRequest request = new HeadObjectRequest(bucket, key); //执行请求 HeadObjectResult result = cosXml.HeadObject(request); //请求成功 Console.WriteLine(result.GetResultInfo()); } catch (COSXML.CosException.CosClientException clientEx) { //请求失败 Console.WriteLine("CosClientException: " + clientEx); } catch (COSXML.CosException.CosServerException serverEx) { //请求失败 Console.WriteLine("CosServerException: " + serverEx.GetInfo()); } //.cssg-snippet-body-end }