/// 设置存储桶静态网站 public void PutBucketWebsite() { //.cssg-snippet-body-start:[put-bucket-website] try { string bucket = "examplebucket-1250000000"; //格式:BucketName-APPID PutBucketWebsiteRequest putRequest = new PutBucketWebsiteRequest(bucket); putRequest.SetIndexDocument("index.html"); putRequest.SetErrorDocument("eroror.html"); putRequest.SetRedirectAllRequestTo("index.html"); PutBucketWebsiteResult putResult = cosXml.putBucketWebsite(putRequest); //请求成功 Console.WriteLine(putResult.GetResultInfo()); } catch (COSXML.CosException.CosClientException clientEx) { //请求失败 Console.WriteLine("CosClientException: " + clientEx); } catch (COSXML.CosException.CosServerException serverEx) { //请求失败 Console.WriteLine("CosServerException: " + serverEx.GetInfo()); } //.cssg-snippet-body-end }
public void TestBucketWebsite() { try { PutBucketWebsiteRequest putRequest = new PutBucketWebsiteRequest(bucket); putRequest.SetIndexDocument("index.html"); putRequest.SetErrorDocument("eroror.html"); putRequest.SetRedirectAllRequestTo("https"); var rule = new WebsiteConfiguration.RoutingRule(); rule.contidion = new WebsiteConfiguration.Contidion(); // HttpErrorCodeReturnedEquals 与 KeyPrefixEquals 必选其一 // 只支持配置4XX返回码,例如403或404 rule.contidion.httpErrorCodeReturnedEquals = 404; // rule.contidion.keyPrefixEquals = "test.html"; rule.redirect = new WebsiteConfiguration.Redirect(); rule.redirect.protocol = "https"; // ReplaceKeyWith 与 ReplaceKeyPrefixWith 必选其一 // rule.redirect.replaceKeyPrefixWith = "aaa"; rule.redirect.replaceKeyWith = "bbb"; putRequest.SetRoutingRules(new List <WebsiteConfiguration.RoutingRule>() { rule }); PutBucketWebsiteResult putResult = cosXml.PutBucketWebsite(putRequest); Assert.IsTrue(putResult.httpCode == 200); QCloudServer.TestWithServerFailTolerance(() => { GetBucketWebsiteRequest getRequest = new GetBucketWebsiteRequest(bucket); GetBucketWebsiteResult getResult = cosXml.GetBucketWebsite(getRequest); // Console.WriteLine(getResult.GetResultInfo()); Assert.IsNotEmpty((getResult.GetResultInfo())); WebsiteConfiguration configuration = getResult.websiteConfiguration; Assert.NotNull(configuration); Assert.NotNull(configuration.indexDocument); Assert.NotNull(configuration.indexDocument.suffix); Assert.NotNull(configuration.errorDocument); Assert.NotNull(configuration.redirectAllRequestTo); Assert.NotNull(configuration.redirectAllRequestTo.protocol); Assert.NotZero(configuration.routingRules.Count); Assert.NotNull(configuration.routingRules[0].contidion); Assert.NotNull(configuration.routingRules[0].contidion.httpErrorCodeReturnedEquals); // Assert.NotNull(configuration.routingRules[0].contidion.keyPrefixEquals); Assert.NotNull(configuration.routingRules[0].redirect); Assert.NotNull(configuration.routingRules[0].redirect.protocol); // Assert.NotNull(configuration.routingRules[0].redirect.replaceKeyPrefixWith); Assert.NotNull(configuration.routingRules[0].redirect.replaceKeyWith); DeleteBucketWebsiteRequest deleteRequest = new DeleteBucketWebsiteRequest(bucket); DeleteBucketWebsiteResult deleteResult = cosXml.DeleteBucketWebsite(deleteRequest); Assert.IsTrue(deleteResult.IsSuccessful()); } ); } catch (COSXML.CosException.CosClientException clientEx) { Console.WriteLine("CosClientException: " + clientEx.Message); Assert.Fail(); } catch (COSXML.CosException.CosServerException serverEx) { Console.WriteLine("CosServerException: " + serverEx.GetInfo()); Assert.Fail(); } }