Ejemplo 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(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;   
        }
Ejemplo n.º 2
0
        public async Task<MultipartUploadResult> MultipartUpload(MultiUploadRequestData multiUploadObject,
            Action<HttpProcessData> uploadProcessCallback = null, CancellationToken? cancellationToken = null)
        {
            MultipartUploadResult 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); 

                Dictionary<string, string> parameters = new Dictionary<string, string>();
                parameters.Add("partNumber", multiUploadObject.PartNumber);
                parameters.Add("uploadId", multiUploadObject.UploadId);

                httpRequestMessage = new OssHttpRequestMessage(multiUploadObject.Bucket, multiUploadObject.Key, parameters);

                httpRequestMessage.Method = HttpMethod.Put;
                httpRequestMessage.Headers.Date = DateTime.UtcNow;
                httpRequestMessage.Content = new StreamContent(multiUploadObject.Content);

                if (uploadProcessCallback != null)
                {
                    processMessageHander.HttpSendProgress += (sender, e) =>
                    {
                        uploadProcessCallback(new HttpProcessData()
                        {
                            TotalBytes = e.TotalBytes,
                            BytesTransferred = e.BytesTransferred,
                            ProgressPercentage = e.ProgressPercentage
                        });

                    };
                }


                OssRequestSigner.Sign(httpRequestMessage, networkCredential);

                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 deseserializer = DeserializerFactory.GetFactory().CreateMultipartUploadDeserializer();
                result = deseserializer.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();


                if (response != null)
                    response.Dispose();
            }
            return result;

        }
Ejemplo n.º 3
0
        public async Task<PutObjectResult> PutObject(string bucketName, string key, Stream content, ObjectMetadata metadata, 
            Action<HttpProcessData> uploadProcessCallback = null, CancellationToken? cancellationToken = null)
        {
            PutObjectResult 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(bucketName, key);



                httpRequestMessage.Method = HttpMethod.Put;
                httpRequestMessage.Headers.Date = DateTime.UtcNow;
                httpRequestMessage.Content = new StreamContent(content);


                OssClientHelper.initialHttpRequestMessage(httpRequestMessage, metadata);

                

                OssRequestSigner.Sign(httpRequestMessage, networkCredential);

                if (uploadProcessCallback != null)
                {
                    processMessageHander.HttpSendProgress += (sender, e) =>
                    {
                        uploadProcessCallback(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().CreatePutObjectReusltDeserializer();
                result = temp.Deserialize(response);
                //localHttpClient.Dispose();

            }
            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();


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

            return result;

        }