Esempio n. 1
0
        public async Task<OssObject> GetObject(GetObjectRequest getObjectRequest,
            Action<HttpProcessData> downloadProcessCallback = null, CancellationToken? cancellationToken = null)
        {

            OssObject result = null;

            HttpClientHandler hand = null;
          //  ProgressMessageHandler processMessageHander = null;
            HttpClient localHttpClient = null;
            OssHttpRequestMessage httpRequestMessage = null;
            HttpResponseMessage response = null;

            try
            {
                hand = new HttpClientHandler();
           //     processMessageHander = new ProgressMessageHandler(hand);
                localHttpClient = new HttpClient();
                localHttpClient.Timeout += new TimeSpan(2 * TimeSpan.TicksPerHour); 

                httpRequestMessage = new OssHttpRequestMessage(getObjectRequest.BucketName, getObjectRequest.Key);
                getObjectRequest.ResponseHeaders.Populate(httpRequestMessage.Headers);
                getObjectRequest.Populate(httpRequestMessage.Headers);

                httpRequestMessage.Method = HttpMethod.Get;
                httpRequestMessage.Headers.Date = DateTime.UtcNow;

                OssRequestSigner.Sign(httpRequestMessage, networkCredential);
                //if (downloadProcessCallback != null)
                //{
                //    processMessageHander.HttpReceiveProgress += (sender, e) =>
                //    {
                //        downloadProcessCallback(new HttpProcessData()
                //        {
                //            TotalBytes = e.TotalBytes,
                //            BytesTransferred = e.BytesTransferred,
                //            ProgressPercentage = e.ProgressPercentage
                //        }); ;

                //    };
                //}

                if (cancellationToken != null)
                    response = await localHttpClient.SendAsync(httpRequestMessage, (CancellationToken)cancellationToken);
                else
                    response = await localHttpClient.SendAsync(httpRequestMessage);


                if (response.IsSuccessStatusCode == false)
                {
                    await ErrorResponseHandler.Handle(response);
                }

                var temp = DeserializerFactory.GetFactory().CreateGetObjectResultDeserializer(getObjectRequest);
                result = await temp.Deserialize(response);
            }
            catch (Exception ex)
            {
                throw ex;
            }
            finally
            {
                if (hand != null)
                    hand.Dispose();

                //if (processMessageHander != null)
                //    processMessageHander.Dispose();

                if (localHttpClient != null)
                    localHttpClient.Dispose();

                if (httpRequestMessage != null)
                    httpRequestMessage.Dispose();
            }


            return result;   
        }
Esempio n. 2
0
 public async Task<ObjectMetadata> GetObject(GetObjectRequest getObjectRequest, Stream output,
     Action<HttpProcessData> downloadProcessCallback = null, CancellationToken? cancellationToken = null)
 {
     OssObject ossObject = await this.GetObject(getObjectRequest, downloadProcessCallback, cancellationToken);
     using (ossObject.Content)
     {
         ossObject.Content.CopyTo(output);
     }
     return ossObject.Metadata;
 }
Esempio n. 3
0
        public async Task <OssObject> GetObject(GetObjectRequest getObjectRequest,
                                                Action <HttpProcessData> downloadProcessCallback = null, CancellationToken?cancellationToken = null)
        {
            OssObject result = null;

            HttpClientHandler      hand = null;
            ProgressMessageHandler processMessageHander = null;
            HttpClient             localHttpClient      = null;
            OssHttpRequestMessage  httpRequestMessage   = null;
            HttpResponseMessage    response             = null;

            try
            {
                hand = new HttpClientHandler();
                processMessageHander     = new ProgressMessageHandler(hand);
                localHttpClient          = new HttpClient(processMessageHander);
                localHttpClient.Timeout += new TimeSpan(2 * TimeSpan.TicksPerHour);

                httpRequestMessage = new OssHttpRequestMessage(getObjectRequest.BucketName, getObjectRequest.Key);
                getObjectRequest.ResponseHeaders.Populate(httpRequestMessage.Headers);
                getObjectRequest.Populate(httpRequestMessage.Headers);

                httpRequestMessage.Method       = HttpMethod.Get;
                httpRequestMessage.Headers.Date = DateTime.UtcNow;

                OssRequestSigner.Sign(httpRequestMessage, networkCredential);
                if (downloadProcessCallback != null)
                {
                    processMessageHander.HttpReceiveProgress += (sender, e) =>
                    {
                        downloadProcessCallback(new HttpProcessData()
                        {
                            TotalBytes         = e.TotalBytes,
                            BytesTransferred   = e.BytesTransferred,
                            ProgressPercentage = e.ProgressPercentage
                        });;
                    };
                }

                if (cancellationToken != null)
                {
                    response = await localHttpClient.SendAsync(httpRequestMessage, (CancellationToken)cancellationToken);
                }
                else
                {
                    response = await localHttpClient.SendAsync(httpRequestMessage);
                }


                if (response.IsSuccessStatusCode == false)
                {
                    await ErrorResponseHandler.Handle(response);
                }

                var temp = DeserializerFactory.GetFactory().CreateGetObjectResultDeserializer(getObjectRequest);
                result = await temp.Deserialize(response);
            }
            catch (Exception ex)
            {
                throw ex;
            }
            finally
            {
                if (hand != null)
                {
                    hand.Dispose();
                }

                if (processMessageHander != null)
                {
                    processMessageHander.Dispose();
                }

                if (localHttpClient != null)
                {
                    localHttpClient.Dispose();
                }

                if (httpRequestMessage != null)
                {
                    httpRequestMessage.Dispose();
                }
            }


            return(result);
        }