public void SetBucketWebsiteTest()
 {
     try
     {
         const string indexPage  = "index.html";
         const string errorPage  = "NotFound.html";
         var          sbwRequest = new SetBucketWebsiteRequest(_bucketName, indexPage, errorPage);
         _ossClient.SetBucketWebsite(sbwRequest);
         OssTestUtils.WaitForCacheExpire();
         var swResult = _ossClient.GetBucketWebsite(_bucketName);
         Assert.AreEqual(swResult.IndexDocument, indexPage);
         Assert.AreEqual(swResult.ErrorDocument, errorPage);
     }
     finally
     {
         _ossClient.DeleteBucketWebsite(_bucketName);
         try
         {
             _ossClient.GetBucketWebsite(_bucketName);
             Assert.Fail("Get bucket website should not pass when it was not set or deleted");
         }
         catch (OssException e)
         {
             Assert.AreEqual(OssErrorCode.NoSuchWebsiteConfiguration, e.ErrorCode);
         }
     }
 }
Ejemplo n.º 2
0
        private static void SetBucketWebsite()
        {
            try
            {
                SetBucketWebsiteRequest request = new SetBucketWebsiteRequest();
                request.BucketName    = bucketName;
                request.Configuration = new WebsiteConfiguration();

                request.Configuration.IndexDocument = "index.html";

                request.Configuration.ErrorDocument = "error.html";

                RoutingRule routingRule = new RoutingRule();
                routingRule.Redirect                              = new Redirect();
                routingRule.Redirect.HostName                     = "www.example.com";
                routingRule.Redirect.HttpRedirectCode             = "305";
                routingRule.Redirect.Protocol                     = ProtocolEnum.Http;
                routingRule.Redirect.ReplaceKeyPrefixWith         = "replacekeyprefix";
                routingRule.Condition                             = new Condition();
                routingRule.Condition.HttpErrorCodeReturnedEquals = "404";
                routingRule.Condition.KeyPrefixEquals             = "keyprefix";
                request.Configuration.RoutingRules                = new List <RoutingRule>();
                request.Configuration.RoutingRules.Add(routingRule);

                SetBucketWebsiteResponse response = client.SetBucketWebsiteConfiguration(request);
                Console.WriteLine("Set bucket website response: {0}", response.StatusCode);
            }
            catch (ObsException ex)
            {
                Console.WriteLine("Exception errorcode: {0}, when set bucket website.", ex.ErrorCode);
                Console.WriteLine("Exception errormessage: {0}", ex.ErrorMessage);
            }
        }
        public void SetBucketWebsiteInvalidInputTest()
        {
            var InvalidPageNamesList = new List <string>
            {
                "a", ".html"
            };

            foreach (var invalidPageName in InvalidPageNamesList)
            {
                try
                {
                    var sbwRequest = new SetBucketWebsiteRequest(_bucketName, invalidPageName, invalidPageName);
                    _ossClient.SetBucketWebsite(sbwRequest);
                    Assert.Fail("Set Bucket website should fail with invalid page names");
                }
                catch (ArgumentException)
                {
                    Assert.IsTrue(true);
                }
            }

            InvalidPageNamesList = new List <string>
            {
                string.Empty, null
            };

            foreach (var invalidPageName in InvalidPageNamesList)
            {
                try
                {
                    var sbwRequest = new SetBucketWebsiteRequest(_bucketName, invalidPageName, invalidPageName);
                    _ossClient.SetBucketWebsite(sbwRequest);
                    Assert.Fail("Set Bucket website should fail with invalid page names");
                }
                catch (OssException)
                {
                    Assert.IsTrue(true);
                }
            }

            try
            {
                var sbwRequest = new SetBucketWebsiteRequest(_bucketName, "index.html", ".html");
                _ossClient.SetBucketWebsite(sbwRequest);
                Assert.Fail("Set Bucket website should fail with invalid page names");
            }
            catch (ArgumentException)
            {
                Assert.IsTrue(true);
            }
        }
 public void SetBucketWebsiteInvalidInputTest()
 {
     foreach (var invalidPageName in OssTestUtils.InvalidPageNamesList)
     {
         try
         {
             var sbwRequest = new SetBucketWebsiteRequest(_bucketName, invalidPageName, invalidPageName);
             _ossClient.SetBucketWebsite(sbwRequest);
             Assert.Fail("Set Bucket website should fail with invalid page names");
         }
         catch (ArgumentException)
         {
             Assert.IsTrue(true);
         }
     }
 }
        private SetBucketWebsiteCommand(IServiceClient client, Uri endpoint, ExecutionContext context,
                                    string bucketName, SetBucketWebsiteRequest setBucketWebsiteRequest)
            : base(client, endpoint, context)
        {
            OssUtils.CheckBucketName(bucketName);
            if (string.IsNullOrEmpty(setBucketWebsiteRequest.IndexDocument))
                throw new ArgumentException("index document must not be empty");
            if (!OssUtils.IsWebpageValid(setBucketWebsiteRequest.IndexDocument))
                throw new ArgumentException("Invalid index document, must be end with .html");
            if (!string.IsNullOrEmpty(setBucketWebsiteRequest.ErrorDocument)
                && !OssUtils.IsWebpageValid(setBucketWebsiteRequest.ErrorDocument))
                throw new ArgumentException("Invalid error document, must be end with .html");

            _bucketName = bucketName;
            _setBucketWebsiteRequest = setBucketWebsiteRequest;
        }
        private SetBucketWebsiteCommand(IServiceClient client, Uri endpoint, ExecutionContext context,
                                        SetBucketWebsiteRequest request)
            : base(client, endpoint, context)
        {
            OssUtils.CheckBucketName(request.BucketName);
            if (!string.IsNullOrEmpty(request.IndexDocument) &&
                !OssUtils.IsWebpageValid(request.IndexDocument))
            {
                throw new ArgumentException("Invalid index document, must be end with .html");
            }
            if (!string.IsNullOrEmpty(request.ErrorDocument) &&
                !OssUtils.IsWebpageValid(request.ErrorDocument))
            {
                throw new ArgumentException("Invalid error document, must be end with .html");
            }

            _request = request;
        }
        public static void SetBucketWebsite(string bucketName)
        {
            try
            {
                var request = new SetBucketWebsiteRequest(bucketName, "index.html", "error.html");

                client.SetBucketWebsite(request);

                Console.WriteLine("Set bucket:{0} Wetbsite succeeded ", bucketName);
            }
            catch (OssException ex)
            {
                Console.WriteLine("Failed with error info: {0}; Error info: {1}. \nRequestID:{2}\tHostID:{3}", 
                    ex.ErrorCode, ex.Message, ex.RequestId, ex.HostId);
            }
            catch (Exception ex)
            {
                Console.WriteLine("Failed with error info: {0}", ex.Message);
            }
        }
        public static void SetBucketWebsite(string bucketName)
        {
            try
            {
                var request = new SetBucketWebsiteRequest(bucketName, "index.html", "error.html");

                client.SetBucketWebsite(request);

                Console.WriteLine("Set bucket:{0} Wetbsite succeeded ", bucketName);
            }
            catch (OssException ex)
            {
                Console.WriteLine("Failed with error info: {0}; Error info: {1}. \nRequestID:{2}\tHostID:{3}",
                                  ex.ErrorCode, ex.Message, ex.RequestId, ex.HostId);
            }
            catch (Exception ex)
            {
                Console.WriteLine("Failed with error info: {0}", ex.Message);
            }
        }
        public void SetBucketWebsiteWithXmlDataTest()
        {
            try
            {
                string config =
                    @" 
                    <WebsiteConfiguration>
                        <IndexDocument>
                            <Suffix>index1.html</Suffix>
                            <SupportSubDir>true</SupportSubDir>
                            <Type>0</Type>
                        </IndexDocument>
                        <ErrorDocument>
                            <Key>NotFound.html</Key>
                        </ErrorDocument>
                    </WebsiteConfiguration>
                    ";

                const string indexPage  = "index1.html";
                const string errorPage  = "NotFound.html";
                var          sbwRequest = new SetBucketWebsiteRequest(_bucketName, config);
                _ossClient.SetBucketWebsite(sbwRequest);
                OssTestUtils.WaitForCacheExpire();
                var swResult = _ossClient.GetBucketWebsite(_bucketName);
                Assert.AreEqual(swResult.IndexDocument, indexPage);
                Assert.AreEqual(swResult.ErrorDocument, errorPage);
            }
            finally
            {
                _ossClient.DeleteBucketWebsite(_bucketName);
                try
                {
                    _ossClient.GetBucketWebsite(_bucketName);
                    Assert.Fail("Get bucket website should not pass when it was not set or deleted");
                }
                catch (OssException e)
                {
                    Assert.AreEqual(OssErrorCode.NoSuchWebsiteConfiguration, e.ErrorCode);
                }
            }
        }
Ejemplo n.º 10
0
 /// <summary>
 /// 设置桶的Website(托管)配置。
 /// </summary>
 /// <param name="request">设置桶Website(托管)配置的请求参数。</param>
 /// <returns>设置桶Website(托管)配置的响应结果。</returns>
 public SetBucketWebsiteResponse SetBucketWebsiteConfiguration(SetBucketWebsiteRequest request)
 {
     return(this.DoRequest <SetBucketWebsiteRequest, SetBucketWebsiteResponse>(request));
 }
 public static SetBucketWebsiteCommand Create(IServiceClient client, Uri endpoint,
                                          ExecutionContext context,
                                          string bucketName, SetBucketWebsiteRequest setBucketWebsiteRequest)
 {
     return new SetBucketWebsiteCommand(client, endpoint, context, bucketName, setBucketWebsiteRequest);
 }
 public static SetBucketWebsiteCommand Create(IServiceClient client, Uri endpoint,
                                              ExecutionContext context,
                                              string bucketName, SetBucketWebsiteRequest request)
 {
     return(new SetBucketWebsiteCommand(client, endpoint, context, request));
 }
 /// <summary>
 /// Start the asynchronous request for configuring bucket website hosting.
 /// </summary>
 /// <param name="request">Parameters in a request for configuring bucket website hosting</param>
 /// <param name="callback">Asynchronous request callback function</param>
 /// <param name="state">Asynchronous request status object</param>
 /// <returns>Response to the asynchronous request</returns>
 public IAsyncResult BeginSetBucketWebsiteConfiguration(SetBucketWebsiteRequest request, AsyncCallback callback, object state)
 {
     return(this.BeginDoRequest <SetBucketWebsiteRequest>(request, callback, state));
 }