Inheritance: System.Net.Http.HttpRequestMessage
示例#1
0
        public async Task <ObjectListing> ListObjects(ListObjectsRequest listObjectsRequest)
        {
            ObjectListing         result             = null;
            OssHttpRequestMessage httpRequestMessage = null;
            HttpResponseMessage   response           = null;

            try
            {
                Dictionary <string, string> parameters = new Dictionary <string, string>();
                if (listObjectsRequest.Prefix != null)
                {
                    parameters.Add("prefix", listObjectsRequest.Prefix);
                }
                if (listObjectsRequest.Delimiter != null)
                {
                    parameters.Add("delimiter", listObjectsRequest.Delimiter);
                }
                if (listObjectsRequest.Marker != null)
                {
                    parameters.Add("marker", listObjectsRequest.Marker);
                }
                if (listObjectsRequest.MaxKeys != null)
                {
                    parameters.Add("maxKeys", listObjectsRequest.MaxKeys.ToString());
                }

                httpRequestMessage = new OssHttpRequestMessage(listObjectsRequest.BucketName, null, parameters);

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

                OssRequestSigner.Sign(httpRequestMessage, networkCredential);
                response = await httpClient.SendAsync(httpRequestMessage);

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

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

                if (response != null)
                {
                    response.Dispose();
                }
            }
            return(result);
        }
示例#2
0
        public async Task<Bucket> CreateBucket(string bucketName)
        {
            OssHttpRequestMessage ossHttpRequestMessage = null;
            HttpResponseMessage response = null;
            try
            {

                 ossHttpRequestMessage = new OssHttpRequestMessage(bucketName, null);

                ossHttpRequestMessage.Method = HttpMethod.Put;
                ossHttpRequestMessage.Headers.Date = DateTime.UtcNow;
                OssRequestSigner.Sign(ossHttpRequestMessage, networkCredential);
                 response = await httpClient.SendAsync(ossHttpRequestMessage);

                if (response.IsSuccessStatusCode == false)
                {
                    await ErrorResponseHandler.Handle(response);
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
            finally
            {
                if (ossHttpRequestMessage != null)
                    ossHttpRequestMessage.Dispose();

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

            }
            return new Bucket(bucketName);
        }
示例#3
0
        public static void initialHttpRequestMessage(OssHttpRequestMessage hReqMes, ObjectMetadata metadata)
        {
            if (metadata.CacheControl != null)
            {
                CacheControlHeaderValue temp;
                if (CacheControlHeaderValue.TryParse(metadata.CacheControl, out temp) == true)
                {
                    hReqMes.Headers.CacheControl = temp;
                }
            }

            if (metadata.ContentDisposition != null)
            {
                ContentDispositionHeaderValue temp;
                if (ContentDispositionHeaderValue.TryParse(metadata.CacheControl, out temp) == true)
                {
                    hReqMes.Content.Headers.ContentDisposition = temp;
                }
            }

            if (metadata.ContentLength != 0)
            {
                hReqMes.Content.Headers.ContentLength = metadata.ContentLength;
            }

            if (metadata.ContentType != null)
            {
                MediaTypeHeaderValue temp;
                if (MediaTypeHeaderValue.TryParse(metadata.CacheControl, out temp) == true)
                {
                    hReqMes.Content.Headers.ContentType = temp;
                }
                else
                {
                    hReqMes.Content.Headers.ContentType = new MediaTypeHeaderValue("application/octet-stream");
                }
            }
            else
            {
                hReqMes.Content.Headers.ContentType = new MediaTypeHeaderValue("application/octet-stream");
            }

            //if (metadata.ExpirationTime != null)
            //{
            //    hReqMes.Content.Headers.Expires = metadata.ExpirationTime;
            //}

            foreach (KeyValuePair <string, string> entry in metadata.UserMetadata)
            {
                hReqMes.Headers.Pragma.Add(new NameValueHeaderValue("x-oss-meta-" + entry.Key, entry.Value));
            }
        }
示例#4
0
        public async Task <CompleteMultipartUploadResult> CompleteMultipartUpload(CompleteMultipartUploadModel completeMultipartUploadModel)
        {
            CompleteMultipartUploadResult result             = null;
            OssHttpRequestMessage         httpRequestMessage = null;
            HttpResponseMessage           response           = null;

            try
            {
                Dictionary <string, string> parameters = new Dictionary <string, string>();
                parameters.Add("uploadId", completeMultipartUploadModel.UploadId);


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

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

                XmlStreamSerializer <CompleteMultipartUploadModel> serializer = new XmlStreamSerializer <CompleteMultipartUploadModel>();
                //  FileStream fileStream = new FileStream("1.xml", FileMode.Open);

                //httpRequestMessage.Content = new StreamContent(fileStream);
                httpRequestMessage.Content = new StreamContent(serializer.Serialize(completeMultipartUploadModel));

                OssRequestSigner.Sign(httpRequestMessage, networkCredential);
                response = await httpClient.SendAsync(httpRequestMessage);

                if (response.IsSuccessStatusCode == false)
                {
                    await ErrorResponseHandler.Handle(response);
                }
                var temp = DeserializerFactory.GetFactory().CreateCompMultiUploadDeserializer();
                result = await temp.Deserialize(response);
            }
            catch (Exception ex)
            {
                throw ex;
            }
            finally
            {
                if (httpRequestMessage != null)
                {
                    httpRequestMessage.Dispose();
                }

                if (response != null)
                {
                    response.Dispose();
                }
            }
            return(result);
        }
        public static void initialHttpRequestMessage(OssHttpRequestMessage hReqMes, ObjectMetadata metadata)
        {
            if (metadata.CacheControl != null)
            {
                CacheControlHeaderValue temp;
                if (CacheControlHeaderValue.TryParse(metadata.CacheControl, out temp) == true)
                    hReqMes.Headers.CacheControl = temp;
            }

            if (metadata.ContentDisposition != null)
            {
                ContentDispositionHeaderValue temp;
                if (ContentDispositionHeaderValue.TryParse(metadata.CacheControl, out temp) == true)
                    hReqMes.Content.Headers.ContentDisposition = temp;
            }

            if (metadata.ContentLength != 0)
            {
                hReqMes.Content.Headers.ContentLength = metadata.ContentLength;
            }

            if (metadata.ContentType != null)
            {
                MediaTypeHeaderValue temp;
                if (MediaTypeHeaderValue.TryParse(metadata.CacheControl, out temp) == true)
                    hReqMes.Content.Headers.ContentType = temp;
                else
                    hReqMes.Content.Headers.ContentType = new MediaTypeHeaderValue("application/octet-stream");

            }
            else
            {
                hReqMes.Content.Headers.ContentType = new MediaTypeHeaderValue("application/octet-stream");
            }

            //if (metadata.ExpirationTime != null)
            //{
            //    hReqMes.Content.Headers.Expires = metadata.ExpirationTime;
            //}

            foreach (KeyValuePair<string, string> entry in metadata.UserMetadata)
            {
                hReqMes.Headers.Pragma.Add(new NameValueHeaderValue("x-oss-meta-" + entry.Key, entry.Value));
            }



        }
示例#6
0
        public static void Sign(OssHttpRequestMessage ossHttpRequestMessage, NetworkCredential networkCredential)
        {
            string accessKeyId = networkCredential.UserName;
            string secretAccessKey = networkCredential.Password;
            if (!string.IsNullOrEmpty(secretAccessKey))
            {
                string canonicalString = SignUtils.BuildCanonicalString(ossHttpRequestMessage);
                string signature = ServiceSignature.Create().ComputeSignature(secretAccessKey, canonicalString);
                ossHttpRequestMessage.Headers.Authorization = new System.Net.Http.Headers.AuthenticationHeaderValue("OSS", accessKeyId + ":" + signature);

            }
            else
            {
                ossHttpRequestMessage.Headers.Authorization = new System.Net.Http.Headers.AuthenticationHeaderValue("Authorization", accessKeyId);
            }
        }
示例#7
0
        public async Task <IEnumerable <Bucket> > ListBuckets()
        {
            IEnumerable <Bucket>  result             = null;
            OssHttpRequestMessage httpRequestMessage = null;
            HttpResponseMessage   response           = null;

            try
            {
                httpRequestMessage = new OssHttpRequestMessage(OssHttpRequestMessage.NONEEDBUKETNAME, null);

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

                OssRequestSigner.Sign(httpRequestMessage, networkCredential);


                response = await httpClient.SendAsync(httpRequestMessage);

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

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

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

            return(result);
        }
示例#8
0
        public async Task <ListPartsResult> ListMultiUploadParts(string buketName, string key, string uploadId)
        {
            ListPartsResult       result             = null;
            OssHttpRequestMessage httpRequestMessage = null;
            HttpResponseMessage   response           = null;

            try
            {
                Dictionary <string, string> parameters = new Dictionary <string, string>();
                parameters.Add("uploadId", uploadId);


                httpRequestMessage = new OssHttpRequestMessage(buketName, key, parameters);

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

                OssRequestSigner.Sign(httpRequestMessage, networkCredential);
                response = await httpClient.SendAsync(httpRequestMessage);

                if (response.IsSuccessStatusCode == false)
                {
                    await ErrorResponseHandler.Handle(response);
                }
                var temp = DeserializerFactory.GetFactory().CreateListPartsDeserialzer();
                result = await temp.Deserialize(response);
            }
            catch (Exception ex)
            {
                throw ex;
            }
            finally
            {
                if (httpRequestMessage != null)
                {
                    httpRequestMessage.Dispose();
                }

                if (response != null)
                {
                    response.Dispose();
                }
            }
            return(result);
        }
示例#9
0
        public static void Sign(OssHttpRequestMessage ossHttpRequestMessage, NetworkCredential networkCredential)
        {
            string accessKeyId = networkCredential.UserName;
            string secretAccessKey = networkCredential.Password;
            if (!string.IsNullOrEmpty(secretAccessKey))
            {
                string canonicalString = SignUtils.BuildCanonicalString(ossHttpRequestMessage);
                string signature = ServiceSignature.Create().ComputeSignature(secretAccessKey, canonicalString);
                ossHttpRequestMessage.Headers.Authorization = new System.Net.Http.Headers.AuthenticationHeaderValue("OSS", accessKeyId + ":" + signature);

            }
            else
            {
                ossHttpRequestMessage.Headers.Authorization = new System.Net.Http.Headers.AuthenticationHeaderValue("Authorization", accessKeyId);
            }


        }
示例#10
0
        public async Task <ObjectMetadata> GetObjectMetadata(string bucketName, string key)
        {
            ObjectMetadata        result             = null;
            OssHttpRequestMessage httpRequestMessage = null;
            HttpResponseMessage   response           = null;

            try
            {
                httpRequestMessage = new OssHttpRequestMessage(bucketName, key);

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

                OssRequestSigner.Sign(httpRequestMessage, networkCredential);
                response = await httpClient.SendAsync(httpRequestMessage);

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

                var temp = DeserializerFactory.GetFactory().CreateGetObjectMetadataResultDeserializer();
                result = temp.Deserialize(response);
            }
            catch (Exception ex)
            {
                throw ex;
            }
            finally
            {
                if (httpRequestMessage != null)
                {
                    httpRequestMessage.Dispose();
                }

                if (response != null)
                {
                    response.Dispose();
                }
            }
            return(result);
        }
示例#11
0
        public async Task DeleteMultipartUpload(MultiUploadRequestData multiUploadObject)
        {
            OssHttpRequestMessage httpRequestMessage = null;
            HttpResponseMessage   response           = null;

            try
            {
                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.Delete;
                httpRequestMessage.Headers.Date = DateTime.UtcNow;

                OssRequestSigner.Sign(httpRequestMessage, networkCredential);
                response = await httpClient.SendAsync(httpRequestMessage);

                if (response.IsSuccessStatusCode == false)
                {
                    await ErrorResponseHandler.Handle(response);
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
            finally
            {
                if (httpRequestMessage != null)
                {
                    httpRequestMessage.Dispose();
                }

                if (response != null)
                {
                    response.Dispose();
                }
            }
        }
示例#12
0
        public async Task SetBucketAcl(string bucketName, CannedAccessControlList acl)
        {
            OssHttpRequestMessage httpRequestMessage = null;
            HttpResponseMessage   response           = null;

            try
            {
                Dictionary <string, string> parameters = new Dictionary <string, string>();
                parameters.Add("acl", null);
                httpRequestMessage = new OssHttpRequestMessage(bucketName, null, parameters);

                httpRequestMessage.Method       = HttpMethod.Put;
                httpRequestMessage.Headers.Date = DateTime.UtcNow;
                httpRequestMessage.Headers.Add("x-oss-acl", acl.GetStringValue());
                OssRequestSigner.Sign(httpRequestMessage, networkCredential);
                response = await httpClient.SendAsync(httpRequestMessage);

                if (response.IsSuccessStatusCode == false)
                {
                    await ErrorResponseHandler.Handle(response);
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
            finally
            {
                if (httpRequestMessage != null)
                {
                    httpRequestMessage.Dispose();
                }

                if (response != null)
                {
                    response.Dispose();
                }
            }
        }
示例#13
0
        public async Task DeleteBucket(string bucketName)
        {
            OssHttpRequestMessage httpRequestMessage = null;
            HttpResponseMessage   response           = null;

            try
            {
                httpRequestMessage = new OssHttpRequestMessage(bucketName, null);

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

                OssRequestSigner.Sign(httpRequestMessage, networkCredential);
                response = await httpClient.SendAsync(httpRequestMessage);

                if (response.IsSuccessStatusCode == false)
                {
                    await ErrorResponseHandler.Handle(response);
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
            finally
            {
                if (httpRequestMessage != null)
                {
                    httpRequestMessage.Dispose();
                }

                if (response != null)
                {
                    response.Dispose();
                }
            }
        }
示例#14
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();
                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;

        }
示例#15
0
        public async Task DeleteObject(string bucketName, string key)
        {
            OssHttpRequestMessage httpRequestMessage = null;
            HttpResponseMessage response = null;
            try
            {
                httpRequestMessage = new OssHttpRequestMessage(bucketName, key);

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

                OssRequestSigner.Sign(httpRequestMessage, networkCredential);
                response = await httpClient.SendAsync(httpRequestMessage);

                if (response.IsSuccessStatusCode == false)
                {
                    await ErrorResponseHandler.Handle(response);
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
            finally
            {
                if (httpRequestMessage != null)
                    httpRequestMessage.Dispose();

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

        }
示例#16
0
        public async  Task<ObjectMetadata> GetObjectMetadata(string bucketName, string key)
        {
            ObjectMetadata result = null;
            OssHttpRequestMessage httpRequestMessage = null;
            HttpResponseMessage response = null;
            try
            {

                httpRequestMessage = new OssHttpRequestMessage(bucketName, key);

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

                OssRequestSigner.Sign(httpRequestMessage, networkCredential);
                response = await httpClient.SendAsync(httpRequestMessage);

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

                var temp = DeserializerFactory.GetFactory().CreateGetObjectMetadataResultDeserializer();
                result = temp.Deserialize(response);
            }
            catch (Exception ex)
            {
                throw ex;
            }
            finally
            {
                if (httpRequestMessage != null)
                    httpRequestMessage.Dispose();

                if (response != null)
                    response.Dispose();
            }
            return result;
            
        }
示例#17
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;   
        }
示例#18
0
        public async Task<ListMultipartUploadsResult> ListMultipartUploads(string bucketName)
        {
            ListMultipartUploadsResult result = null;
            OssHttpRequestMessage httpRequestMessage = null;
            HttpResponseMessage response = null;
            try
            {
                Dictionary<string, string> parameters = new Dictionary<string, string>();
                parameters.Add("uploads", null);

                httpRequestMessage = new OssHttpRequestMessage(bucketName, null, parameters);

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

                OssRequestSigner.Sign(httpRequestMessage, networkCredential);
                response = await httpClient.SendAsync(httpRequestMessage);

                if (response.IsSuccessStatusCode == false)
                {
                    await ErrorResponseHandler.Handle(response);
                }
                var temp = DeserializerFactory.GetFactory().CreateListMultipartUploadsDeserializer();
                result = await temp.Deserialize(response);
            }
            catch (Exception ex)
            {
                throw ex;
            }
            finally
            {
                if (httpRequestMessage != null)
                    httpRequestMessage.Dispose();

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

        }
示例#19
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();
                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;

        }
示例#20
0
        public async Task<IEnumerable<Bucket>> ListBuckets()
        {
            IEnumerable<Bucket> result = null;
            OssHttpRequestMessage httpRequestMessage = null;
            HttpResponseMessage response = null;
            try
            {
                 httpRequestMessage = new OssHttpRequestMessage(OssHttpRequestMessage.NONEEDBUKETNAME, null);

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

                OssRequestSigner.Sign(httpRequestMessage, networkCredential);


                 response = await httpClient.SendAsync(httpRequestMessage);

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

                var temp = DeserializerFactory.GetFactory().CreateListBucketResultDeserializer();
                result = await temp.Deserialize(response);

            }
            catch (Exception ex)
            {
                throw ex;
            }
            finally
            {
                if (httpRequestMessage != null)
                    httpRequestMessage.Dispose();

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

            return result;

        }
示例#21
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);
        }
示例#22
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);
        }
示例#23
0
        public async Task<CompleteMultipartUploadResult> CompleteMultipartUpload(CompleteMultipartUploadModel completeMultipartUploadModel)
        {
            CompleteMultipartUploadResult result = null;
            OssHttpRequestMessage httpRequestMessage = null;
            HttpResponseMessage response = null;
            try
            {

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


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

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

                XmlStreamSerializer<CompleteMultipartUploadModel> serializer = new XmlStreamSerializer<CompleteMultipartUploadModel>();
              //  FileStream fileStream = new FileStream("1.xml", FileMode.Open);

                //httpRequestMessage.Content = new StreamContent(fileStream);
                httpRequestMessage.Content = new StreamContent(serializer.Serialize(completeMultipartUploadModel));

                OssRequestSigner.Sign(httpRequestMessage, networkCredential);
                response = await httpClient.SendAsync(httpRequestMessage);

                if (response.IsSuccessStatusCode == false)
                {
                    await ErrorResponseHandler.Handle(response);
                }
                var temp = DeserializerFactory.GetFactory().CreateCompMultiUploadDeserializer();
                result = await temp.Deserialize(response);

            }
            catch (Exception ex)
            {
                throw ex;
            }
            finally
            {
                if (httpRequestMessage != null)
                    httpRequestMessage.Dispose();

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

        }
示例#24
0
        public async Task DeleteMultipartUpload(MultiUploadRequestData multiUploadObject)
        {
            OssHttpRequestMessage httpRequestMessage = null;
            HttpResponseMessage response = null;
            try
            {
                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.Delete;
                httpRequestMessage.Headers.Date = DateTime.UtcNow;

                OssRequestSigner.Sign(httpRequestMessage, networkCredential);
                response = await httpClient.SendAsync(httpRequestMessage);

                if (response.IsSuccessStatusCode == false)
                {
                    await ErrorResponseHandler.Handle(response);
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
            finally
            {
                if (httpRequestMessage != null)
                    httpRequestMessage.Dispose();

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

        }
示例#25
0
        public async Task SetBucketAcl(string bucketName, CannedAccessControlList acl)
        {
            OssHttpRequestMessage httpRequestMessage = null;
            HttpResponseMessage response = null;
            try
            {
                Dictionary<string, string> parameters = new Dictionary<string, string>();
                parameters.Add("acl", null);
                httpRequestMessage = new OssHttpRequestMessage(bucketName, null,parameters);

                httpRequestMessage.Method = HttpMethod.Put;
                httpRequestMessage.Headers.Date = DateTime.UtcNow;
                httpRequestMessage.Headers.Add("x-oss-acl", EnumUtils.GetStringValue(acl));
                OssRequestSigner.Sign(httpRequestMessage, networkCredential);
                response = await httpClient.SendAsync(httpRequestMessage);

                if (response.IsSuccessStatusCode == false)
                {
                    await ErrorResponseHandler.Handle(response);
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
            finally
            {
                if (httpRequestMessage != null)
                    httpRequestMessage.Dispose();

                if (response != null)
                    response.Dispose();
            }
        }
示例#26
0
        public async Task<ObjectListing> ListObjects(ListObjectsRequest listObjectsRequest)
        {
            ObjectListing result = null;
            OssHttpRequestMessage httpRequestMessage = null;
            HttpResponseMessage response = null;
            try
            {
                Dictionary<string, string> parameters = new Dictionary<string, string>();
                if (listObjectsRequest.Prefix != null)
                {
                    parameters.Add("prefix", listObjectsRequest.Prefix);

                }
                if (listObjectsRequest.Delimiter != null)
                {
                    parameters.Add("delimiter", listObjectsRequest.Delimiter);

                }
                if (listObjectsRequest.Marker != null)
                {
                    parameters.Add("marker", listObjectsRequest.Marker);

                }
                if (listObjectsRequest.MaxKeys != null)
                {
                    parameters.Add("maxKeys", listObjectsRequest.MaxKeys.ToString());

                }

                httpRequestMessage = new OssHttpRequestMessage(listObjectsRequest.BucketName, null, parameters);

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

                OssRequestSigner.Sign(httpRequestMessage, networkCredential);
                response = await httpClient.SendAsync(httpRequestMessage);

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

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

                if (response != null)
                    response.Dispose();
            }
            return result;
        }
示例#27
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);
        }