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 static void AsynHeadObject(COSXML.CosXml cosXml, string bucket, string key) { QLog.D("XIAO", String.Format("currentThread id = {0}", Thread.CurrentThread.ManagedThreadId)); HeadObjectRequest request = new HeadObjectRequest(bucket, key); //设置签名有效时长 request.SetSign(TimeUtils.GetCurrentTime(TimeUnit.SECONDS), 600); cosXml.HeadObject(request, delegate(CosResult cosResult) { HeadObjectResult result = cosResult as HeadObjectResult; Console.WriteLine(result.GetResultInfo()); Console.WriteLine(String.Format("currentThread id = {0}", Thread.CurrentThread.ManagedThreadId)); }, delegate(CosClientException clientEx, CosServerException serverEx) { if (clientEx != null) { QLog.D("XIAO", clientEx.Message); Console.WriteLine("CosClientException: " + clientEx.StackTrace); } if (serverEx != null) { QLog.D("XIAO", serverEx.Message); Console.WriteLine("CosServerException: " + serverEx.GetInfo()); } Console.WriteLine(String.Format("currentThread id = {0}", Thread.CurrentThread.ManagedThreadId)); }); }
/// 获取对象信息 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 }
/// <summary> /// 查询对象元数据 /// </summary> /// <param name="key">对象在存储桶中的位置,即称对象键</param> /// <param name="bucketName">存储桶,格式:BucketName-APPID</param> public static void QueryObject(string key, string bucketName = CosConfig.DEFAULT_BUCKET) { var cosXml = InitializeCosConfig(); try { string bucket = $"{bucketName}-{AppId}"; //存储桶,格式:BucketName-APPID HeadObjectRequest request = new HeadObjectRequest(bucket, key); //设置签名有效时长 request.SetSign(TimeUtils.GetCurrentTime(TimeUnit.SECONDS), 600); //执行请求 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()); } }
private void ComputeSliceList(HeadObjectResult result) { // slice list can be not empty, if use pause&resume, skip it if (this.sliceList.Count != 0) { return; } long contentLength = result.size; rangeEnd = rangeEnd == -1L || (rangeEnd > contentLength) ? contentLength - 1 : rangeEnd; if (rangeEnd - rangeStart + 1 < this.divisionSize) { DownloadSliceStruct slice = new DownloadSliceStruct(); slice.partNumber = 1; slice.sliceStart = rangeStart; slice.sliceEnd = rangeEnd; this.sliceList.Add(slice.partNumber, slice); } else { long sliceCount = ((rangeEnd - rangeStart) / this.sliceSize) + 1; for (int i = 0; i < sliceCount; i++) { DownloadSliceStruct slice = new DownloadSliceStruct(); slice.partNumber = i + 1; slice.sliceStart = rangeStart + i * this.sliceSize; slice.sliceEnd = (slice.sliceStart + this.sliceSize > rangeEnd) ? rangeEnd : slice.sliceStart + this.sliceSize - 1; this.sliceList.Add(slice.partNumber, slice); } } }
public void testGetObjectByte() { try { HeadObjectRequest request = new HeadObjectRequest(bucket, commonKey); //执行请求 HeadObjectResult result = cosXml.HeadObject(request); long contentLength = Int64.Parse(result.responseHeaders["Content-Length"][0]); GetObjectBytesRequest getObjectBytesRequest = new GetObjectBytesRequest(bucket, commonKey); getObjectBytesRequest.SetCosProgressCallback(delegate(long completed, long total) { Console.WriteLine(String.Format("{0} progress = {1} / {2} : {3:##.##}%", DateTime.Now.ToString(), completed, total, completed * 100.0 / total)); }); GetObjectBytesResult getObjectBytesResult = cosXml.GetObject(getObjectBytesRequest); byte[] content = getObjectBytesResult.content; Assert.AreEqual(content.Length, contentLength); } catch (COSXML.CosException.CosClientException clientEx) { Console.WriteLine("CosClientException: " + clientEx); Assert.True(false); } catch (COSXML.CosException.CosServerException serverEx) { Console.WriteLine("CosServerException: " + serverEx.GetInfo()); Assert.True(false); } }
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 AsynHeadObject(COSXML.CosXml cosXml, string bucket, string key) { HeadObjectRequest request = new HeadObjectRequest(bucket, key); //设置签名有效时长 request.SetSign(TimeUtils.GetCurrentTime(TimeUnit.SECONDS), 600); cosXml.HeadObject(request, delegate(CosResult cosResult) { HeadObjectResult result = cosResult as HeadObjectResult; Console.WriteLine(result.GetResultInfo()); manualResetEvent.Set(); }, delegate(CosClientException clientEx, CosServerException serverEx) { if (clientEx != null) { Console.WriteLine("CosClientException: " + clientEx.Message); } if (serverEx != null) { Console.WriteLine("CosServerException: " + serverEx.GetInfo()); } manualResetEvent.Set(); }); }
public void TestHeadObject() { if (init()) { HeadObjectRequest headObjectRequest = new HeadObjectRequest(); headObjectRequest.BucketName = bucketName; headObjectRequest.ObjectKey = objKeyNameFileData; HeadObjectResult result = ks3Client.headObject(headObjectRequest); } }
// resolve resumable task file, continue in proper position private void ResumeDownloadInPossible(HeadObjectResult result, string localFile) { this.resumableInfo = DownloadResumableInfo.LoadFromResumableFile(resumableTaskFile); if (this.resumableInfo != null) { if ((result.crc64ecma == null || result.crc64ecma == resumableInfo.crc64ecma) && this.resumableInfo.eTag == result.eTag && this.resumableInfo.lastModified == result.lastModified && this.resumableInfo.contentLength == result.size) { // load parts downloaded if (this.resumableInfo.slicesDownloaded != null) { // process downloaded parts foreach (DownloadSliceStruct downloadedSlice in resumableInfo.slicesDownloaded) { // remove from current queue DownloadSliceStruct calculatedSlice; bool ret = this.sliceList.TryGetValue(downloadedSlice.partNumber, out calculatedSlice); if (!ret) { // resumable file broken break; } if (calculatedSlice.sliceStart == downloadedSlice.sliceStart && calculatedSlice.sliceEnd == downloadedSlice.sliceEnd) { if (this.sliceToRemove == null) { this.sliceToRemove = new HashSet <int>(); } this.sliceToRemove.Add(downloadedSlice.partNumber); } // add to merging list string tmpFileName = localDir + "." + localFileName + ".cosresumable." + downloadedSlice.partNumber; this.tmpFilePaths.Add(tmpFileName); } } else { this.resumableInfo.slicesDownloaded = new List <DownloadSliceStruct>(); } } } else { this.resumableInfo = new DownloadResumableInfo(); this.resumableInfo.contentLength = result.size; this.resumableInfo.crc64ecma = result.crc64ecma; this.resumableInfo.eTag = result.eTag; this.resumableInfo.lastModified = result.lastModified; this.resumableInfo.slicesDownloaded = new List <DownloadSliceStruct>(); resumableInfo.Persist(resumableTaskFile); } }
internal void Download() { UpdateTaskState(TaskState.Waiting); //对象是否存在 headObjectRequest = new HeadObjectRequest(bucket, key); cosXmlServer.HeadObject(headObjectRequest, delegate(CosResult cosResult) { lock (syncExit) { if (isExit) { return; } } if (UpdateTaskState(TaskState.Running)) { HeadObjectResult result = cosResult as HeadObjectResult; ComputeSliceList(result); // resolv resumeInfo if (resumable) { if (resumableTaskFile == null) { resumableTaskFile = localDir + localFileName + ".cosresumabletask"; } ResumeDownloadInPossible(result, localDir + localFileName); } // concurrent download GetObject(result.crc64ecma); } }, delegate(CosClientException clientEx, CosServerException serverEx) { lock (syncExit) { if (isExit) { return; } } if (UpdateTaskState(TaskState.Failed)) { if (failCallback != null) { failCallback(clientEx, serverEx); } } }); }
private bool CheckFileExists(string bucket, string fileKey) { try { HeadObjectRequest request = new HeadObjectRequest(bucket, fileKey); request.SetSign(TimeUtils.GetCurrentTime(TimeUnit.SECONDS), COS_REQUEST_DEFAULT_DURATION); HeadObjectResult result = cosXml.HeadObject(request); return(result.httpCode.Equals(COS_STATUS.OK)); } catch (Exception ex) { return(false); } }
internal void Download() { UpdateTaskState(TaskState.Waiting); //对象是否存在 headObjectRequest = new HeadObjectRequest(bucket, key); cosXmlServer.HeadObject(headObjectRequest, delegate(CosResult cosResult) { lock (syncExit) { if (isExit) { return; } } if (UpdateTaskState(TaskState.Running)) { HeadObjectResult result = cosResult as HeadObjectResult; //计算range //download GetObject(); } }, delegate(CosClientException clientEx, CosServerException serverEx) { lock (syncExit) { if (isExit) { return; } } if (UpdateTaskState(TaskState.Failed)) { if (failCallback != null) { failCallback(clientEx, serverEx); } } }); }
internal void Download() { UpdateTaskState(TaskState.WAITTING); //对象是否存在 headObjectRequest = new HeadObjectRequest(bucket, key); headObjectRequest.SetSign(TimeUtils.GetCurrentTime(TimeUnit.SECONDS), 600); cosXmlServer.HeadObject(headObjectRequest, delegate(CosResult cosResult) { lock (syncExit) { if (isExit) { return; } } if (UpdateTaskState(TaskState.RUNNING)) { HeadObjectResult result = cosResult as HeadObjectResult; //计算range //download GetObject(); } }, delegate(CosClientException clientEx, CosServerException serverEx) { lock (syncExit) { if (isExit) { return; } } if (UpdateTaskState(TaskState.FAILED)) { if (failCallback != null) { failCallback(clientEx, serverEx); } } }); }
public static bool headObject() { try { Console.WriteLine("--- headObject: ---"); HeadObjectRequest headObjectRequest = new HeadObjectRequest(); headObjectRequest.BucketName = bucketName; headObjectRequest.ObjectKey = objKeyNameFileData; HeadObjectResult result = ks3Client.headObject(headObjectRequest); long length = result.ObjectMetadata.getContentLength(); Console.WriteLine("---------------------\n"); } catch (System.Exception e) { Console.WriteLine(e.ToString()); return(false); } return(true); }
public void HeadObject() { try { HeadObjectRequest request = new HeadObjectRequest(bucket, commonKey); //执行请求 HeadObjectResult result = cosXml.HeadObject(request); Console.WriteLine(result.GetResultInfo()); } catch (COSXML.CosException.CosClientException clientEx) { Console.WriteLine("CosClientException: " + clientEx.StackTrace); Assert.True(false); } catch (COSXML.CosException.CosServerException serverEx) { Console.WriteLine("CosServerException: " + serverEx.GetInfo()); Assert.True(false); } }
public static void HeadObject(COSXML.CosXml cosXml, string bucket, string key) { try { HeadObjectRequest request = new HeadObjectRequest(bucket, key); //设置签名有效时长 request.SetSign(TimeUtils.GetCurrentTime(TimeUnit.SECONDS), 600); //执行请求 HeadObjectResult result = cosXml.HeadObject(request); Console.WriteLine(result.GetResultInfo()); } catch (COSXML.CosException.CosClientException clientEx) { QLog.D("XIAO", clientEx.Message); Console.WriteLine("CosClientException: " + clientEx.StackTrace); } catch (COSXML.CosException.CosServerException serverEx) { QLog.D("XIAO", serverEx.Message); Console.WriteLine("CosServerException: " + serverEx.GetInfo()); } }
internal void Copy() { UpdateTaskState(TaskState.WAITTING); //源对象是否存在 if (copySource == null) { lock (syncExit) { if (isExit) { return; } } if (UpdateTaskState(TaskState.FAILED)) { if (failCallback != null) { failCallback(new CosClientException((int)CosClientError.INVALID_ARGUMENT, "copySource = null"), null); } } //error return; } headObjectRequest = new HeadObjectRequest(copySource.bucket, copySource.key); headObjectRequest.Region = copySource.region; cosXmlServer.HeadObject(headObjectRequest, delegate(CosResult cosResult) { lock (syncExit) { if (isExit) { return; } } if (UpdateTaskState(TaskState.RUNNING)) { HeadObjectResult result = cosResult as HeadObjectResult; //源对象的长度 sourceSize = result.size; if (sourceSize > divisionSize) { MultiPartCopy(); } else { SimpleCopy(); } } }, delegate(CosClientException clientEx, CosServerException serverEx) { lock (syncExit) { if (isExit) { return; } } if (UpdateTaskState(TaskState.FAILED)) { if (failCallback != null) { failCallback(clientEx, serverEx); } } }); }