public void Get(string remotename, Stream stream) { try { cosXml = GetCosXml(); string bucket = _cosOptions.Bucket; string key = GetFullKey(remotename); GetObjectBytesRequest request = new GetObjectBytesRequest(bucket, key); request.SetSign(TimeUtils.GetCurrentTime(TimeUnit.SECONDS), KEY_DURATION_SECOND); GetObjectBytesResult result = cosXml.GetObject(request); byte[] bytes = result.content; Stream ms = new MemoryStream(bytes); Utility.Utility.CopyStream(ms, stream); } catch (COSXML.CosException.CosClientException clientEx) { Logging.Log.WriteErrorMessage(LOGTAG, "Get", clientEx, "Get failed: {0}", remotename); throw; } catch (COSXML.CosException.CosServerException serverEx) { Logging.Log.WriteErrorMessage(LOGTAG, "Get", serverEx, "Get failed: {0}, {1}", remotename, serverEx.GetInfo()); throw; } }
/// 下载返回 bytes 数据 public void downloadToMem() { try { string bucket = "examplebucket-1250000000"; //存储桶,格式:BucketName-APPID string key = "exampleobject"; //对象键 GetObjectBytesRequest request = new GetObjectBytesRequest(bucket, key); //设置进度回调 request.SetCosProgressCallback(delegate(long completed, long total) { Console.WriteLine(String.Format("progress = {0:##.##}%", completed * 100.0 / total)); }); //执行请求 GetObjectBytesResult result = cosXml.GetObject(request); //获取内容 byte[] content = result.content; //请求成功 Console.WriteLine(result.GetResultInfo()); } catch (COSXML.CosException.CosClientException clientEx) { //请求失败 Console.WriteLine("CosClientException: " + clientEx); } catch (COSXML.CosException.CosServerException serverEx) { //请求失败 Console.WriteLine("CosServerException: " + serverEx.GetInfo()); } }
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); } }
/// <summary> /// /// </summary> /// <param name="bucket"></param> /// <param name="key"></param> /// <returns></returns> public byte[] Get(string bucket, string key) { try { GetObjectBytesRequest request = new GetObjectBytesRequest(bucket, key); //设置进度回调 //执行请求 GetObjectBytesResult result = cosXml.GetObject(request); //获取内容 byte[] content = result.content; //请求成功 return(content); } catch (COSXML.CosException.CosClientException clientEx) { //请求失败 Console.WriteLine("CosClientException:======================= " + clientEx); Debug.WriteLine("CosClientException:======================= " + clientEx); return(new byte[] { }); } catch (COSXML.CosException.CosServerException serverEx) { //请求失败 Console.WriteLine("CosServerException:======================= " + serverEx.GetInfo()); Debug.WriteLine("CosServerException:======================= " + serverEx.GetInfo()); return(new byte[] { }); } }
public async Task GetObjectAsync(string bucketName, string objectName, Action <Stream> callback, CancellationToken cancellationToken = default) { if (string.IsNullOrEmpty(bucketName)) { throw new ArgumentNullException(nameof(bucketName)); } if (string.IsNullOrEmpty(objectName)) { throw new ArgumentNullException(nameof(objectName)); } if (!await ObjectsExistsAsync(bucketName, objectName)) { throw new Exception($"Object '{objectName}' not in bucket '{bucketName}'"); } bucketName = ConvertBucketName(bucketName); await Task.Run(() => { GetObjectBytesRequest request = new GetObjectBytesRequest(bucketName, objectName); //执行请求 GetObjectBytesResult result = _client.GetObject(request); //获取内容 byte[] content = result.content; if (content != null && content.Length > 0) { MemoryStream ms = new MemoryStream(content); callback(ms); } else { throw new Exception("Get object bytes is null."); } }, cancellationToken); }
/// <summary> /// 获取文件的流信息 /// </summary> /// <param name="containerName"></param> /// <param name="blobName"></param> /// <returns></returns> public async Task <Stream> GetBlobStream(string containerName, string blobName) { var request = new GetObjectBytesRequest(_tcConfig.BucketName, $"{containerName}/{blobName}"); var response = _cosXmlServer.GetObject(request); await response.HandlerError("下载文件出错!"); byte[] content = response.content; return(new MemoryStream(content)); }
public void GetObject(GetObjectBytesRequest request, OnSuccessCallback <CosResult> successCallback, OnFailedCallback failCallback) { Schedue(request, new GetObjectBytesResult(), successCallback, failCallback); }
public GetObjectBytesResult GetObject(GetObjectBytesRequest request) { return((Model.Object.GetObjectBytesResult)Excute(request, new Model.Object.GetObjectBytesResult())); }