public static void Main(string[] args)
        {
            // Constructs a obs client instance with your account for accessing OBS
            ObsConfig config = new ObsConfig();

            config.Endpoint = endpoint;
            client          = new ObsClient(AK, SK, config);

            // Create bucket
            DoCreateBucket();

            // Set/Get/Delete bucket cors
            DoBucketCorsOperations();

            // Create object
            DoCreateObject();

            // Get object
            DoGetObject();

            // Set/Get object acl
            DoObjectAclOperations();

            // Delete object
            DoDeleteObject();

            // Delete bucket
            //DoDeleteBucket();


            Console.ReadKey();
        }
        private void EndGetRequestStream(IAsyncResult ar)
        {
            HttpObsAsyncResult asyncResult = ar.AsyncState as HttpObsAsyncResult;
            HttpWebRequest     webRequest  = asyncResult.HttpWebRequest;
            ObsConfig          obsConfig   = asyncResult.HttpContext.ObsConfig;
            Stream             data        = asyncResult.HttpRequest.Content;

            if (data == null)
            {
                data = new MemoryStream();
            }
            try
            {
                using (Stream requestStream = webRequest.EndGetRequestStream(ar))
                {
                    ObsCallback callback = delegate()
                    {
                        asyncResult.IsTimeout = false;
                    };
                    if (!webRequest.SendChunked)
                    {
                        CommonUtil.WriteTo(data, requestStream, webRequest.ContentLength, obsConfig.BufferSize, callback);
                    }
                    else
                    {
                        CommonUtil.WriteTo(data, requestStream, obsConfig.BufferSize, callback);
                    }
                }
                asyncResult.Continue(this.EndGetResponse);
            }
            catch (Exception e)
            {
                asyncResult.Abort(e);
            }
        }
예제 #3
0
        private static void AddProxy(HttpWebRequest webRequest, ObsConfig obsConfig)
        {
            webRequest.Proxy = null;

            if (!string.IsNullOrEmpty(obsConfig.ProxyHost))
            {
                if (obsConfig.ProxyPort < 0)
                {
                    webRequest.Proxy = new WebProxy(obsConfig.ProxyHost);
                }
                else
                {
                    webRequest.Proxy = new WebProxy(obsConfig.ProxyHost, obsConfig.ProxyPort);
                }


                if (!string.IsNullOrEmpty(obsConfig.ProxyUserName))
                {
                    webRequest.Proxy.Credentials = string.IsNullOrEmpty(obsConfig.ProxyDomain) ?
                                                   new NetworkCredential(obsConfig.ProxyUserName, obsConfig.ProxyPassword ?? string.Empty) :
                                                   new NetworkCredential(obsConfig.ProxyUserName, obsConfig.ProxyPassword ?? string.Empty,
                                                                         obsConfig.ProxyDomain);
                }

                webRequest.PreAuthenticate = true;

                if (LoggerMgr.IsInfoEnabled)
                {
                    LoggerMgr.Info(string.Format("Send http request using proxy {0}:{1}", obsConfig.ProxyHost, obsConfig.ProxyPort));
                }
            }
        }
예제 #4
0
        private void CreateObsClient()
        {
            try
            {
                //初始化配置参数
                ObsConfig config = new ObsConfig();
                config.Endpoint = _endpoint;
                config.AuthType = AuthTypeEnum.OBS;
                // 创建ObsClient实例
                _obsClient = new ObsClient(_accessKey, _secretKey, config);
                // 使用访问OBS

                HeadBucketRequest request = new HeadBucketRequest
                {
                    BucketName = _bucketName,
                };
                _obsClient.HeadBucket(request);
                //if (_obsClient.HeadBucket(request) == false)
                //{
                //    CreateBucketRequest requestCreate = new CreateBucketRequest();
                //    requestCreate.BucketName = _bucketName;
                //    _obsClient.CreateBucket(requestCreate);
                //}
            }
            catch (ObsException ex)
            {
                throw new Exception("上传Obs错误:" + ex.ErrorCode + ":" + ex.Message);
            }
        }
예제 #5
0
        internal HttpClient(ObsConfig obsConfig)
        {
            ServicePointManager.DefaultConnectionLimit  = obsConfig.ConnectionLimit;
            ServicePointManager.MaxServicePointIdleTime = obsConfig.MaxIdleTime;
            ServicePointManager.Expect100Continue       = false;

            if (obsConfig.SecurityProtocolType.HasValue)
            {
                ServicePointManager.SecurityProtocol = obsConfig.SecurityProtocolType.Value;
            }
        }
예제 #6
0
        private static void SetContent(HttpWebRequest webRequest,
                                       HttpRequest httpRequest,
                                       ObsConfig obsConfig)
        {
            Stream data = httpRequest.Content;

            if (data == null)
            {
                data = new MemoryStream();
            }

            long userSetContentLength = -1;

            if (httpRequest.Headers.ContainsKey(Constants.CommonHeaders.ContentLength))
            {
                userSetContentLength = long.Parse(httpRequest.Headers[Constants.CommonHeaders.ContentLength]);
            }

            if (userSetContentLength >= 0)
            {
                webRequest.ContentLength = userSetContentLength;
                if (webRequest.ContentLength > Constants.DefaultStreamBufferThreshold)
                {
                    webRequest.AllowWriteStreamBuffering = false;
                }
            }
            else
            {
                webRequest.SendChunked = true;
                webRequest.AllowWriteStreamBuffering = false;
            }

            using (Stream requestStream = webRequest.GetRequestStream())
            {
                if (!webRequest.SendChunked)
                {
                    CommonUtil.WriteTo(data, requestStream, webRequest.ContentLength, obsConfig.BufferSize);
                }
                else
                {
                    CommonUtil.WriteTo(data, requestStream, obsConfig.BufferSize);
                }
            }
        }
예제 #7
0
        private static void AddHeaders(HttpWebRequest webRequest, HttpRequest request,
                                       ObsConfig obsConfig)
        {
            webRequest.Timeout           = obsConfig.Timeout;
            webRequest.ReadWriteTimeout  = obsConfig.ReadWriteTimeout;
            webRequest.Method            = request.Method.ToString();
            webRequest.AllowAutoRedirect = false;

            foreach (KeyValuePair <string, string> header in request.Headers)
            {
                if (header.Key.Equals(Constants.CommonHeaders.ContentLength, StringComparison.OrdinalIgnoreCase))
                {
                    continue;
                }

                webRequest.Headers.Add(header.Key, header.Value);
            }
            webRequest.UserAgent = Constants.SdkUserAgent;
        }
예제 #8
0
        internal static HttpWebRequest BuildWebRequest(HttpRequest request, HttpContext context)
        {
            if (LoggerMgr.IsDebugEnabled)
            {
                LoggerMgr.Debug("Perform http request with url:" + request.GetUrl());
            }

            string         url        = string.IsNullOrEmpty(context.RedirectLocation) ? request.GetUrl() : context.RedirectLocation;
            ObsConfig      obsConfig  = context.ObsConfig;
            HttpWebRequest webRequest = WebRequest.Create(url) as HttpWebRequest;

            AddHeaders(webRequest, request, obsConfig);
            AddProxy(webRequest, obsConfig);

            if (webRequest.RequestUri.Scheme.Equals("https") && !obsConfig.ValidateCertificate)
            {
                ServicePointManager.ServerCertificateValidationCallback = new RemoteCertificateValidationCallback(ValidateCertificate);
            }

            return(webRequest);
        }
예제 #9
0
        private static void AddHeaders(HttpWebRequest webRequest, HttpRequest request,
                                       ObsConfig obsConfig)
        {
            webRequest.Timeout           = obsConfig.Timeout;
            webRequest.ReadWriteTimeout  = obsConfig.ReadWriteTimeout;
            webRequest.Method            = request.Method.ToString();
            webRequest.AllowAutoRedirect = false;


#if donetcore
            foreach (KeyValuePair <string, string> header in request.Headers)
            {
                if (header.Key.Equals(Constants.CommonHeaders.ContentLength, StringComparison.OrdinalIgnoreCase))
                {
                    continue;
                }

                webRequest.Headers.Add(header.Key, header.Value);
            }
#else
            try
            {
                webRequest.ServicePoint.ReceiveBufferSize = obsConfig.ReceiveBufferSize;
            }
            catch (Exception)
            {
                //ignore
            }
            foreach (KeyValuePair <string, string> header in request.Headers)
            {
                if (header.Key.Equals(Constants.CommonHeaders.ContentLength, StringComparison.OrdinalIgnoreCase))
                {
                    continue;
                }
                GetAddHeaderInternal().Invoke(webRequest.Headers, new object[] { header.Key, header.Value });
            }
#endif
            webRequest.UserAgent = Constants.SdkUserAgent;
        }
예제 #10
0
 public HttpContext(SecurityProvider sp, ObsConfig obsConfig)
 {
     this.SecurityProvider = sp;
     this.ObsConfig        = obsConfig;
 }