文档首页 > > SDK参考> .NET> 静态网站托管> 设置托管配置

设置托管配置

分享
更新时间: 2019/04/22 GMT+08:00

您可以通过ObsClient.SetBucketWebsite设置桶的托管配置。

配置默认主页和错误页面

以下代码展示了如何配置默认主页和错误页面:

// 创建ObsClient实例
ObsClient client = new ObsClient("*** Provide your Access Key ***", "*** Provide your Secret Key ***", "https://your-endpoint");
//配置默认主页和错误页面
try
{
    SetBucketWebsiteRequest request = new SetBucketWebsiteRequest();
    request.BucketName = "bucketname";
    request.Configuration = new WebsiteConfiguration();
    //配置默认主页
    request.Configuration.IndexDocument = "index.html";
    //配置错误页面
    request.Configuration.ErrorDocument = "error.html";
    SetBucketWebsiteResponse response = client.SetBucketWebsite(request);
    Console.WriteLine("Set bucket website response: {0}", response.StatusCode);
}
catch (ObsException ex)
{
    Console.WriteLine("ErrorCode: {0}", ex.ErrorCode);
    Console.WriteLine("ErrorMessage: {0}", ex.ErrorMessage);
}

配置重定向规则

以下代码展示了如何配置重定向规则:

// 创建ObsClient实例
ObsClient client = new ObsClient("*** Provide your Access Key ***", "*** Provide your Secret Key ***", "https://your-endpoint");
// 配置重定向规则
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.Add(routingRule);
    SetBucketWebsiteResponse response = client.SetBucketWebsite(request);
    Console.WriteLine("Set bucket website response: {0}", response.StatusCode);
}
catch (ObsException ex)
{
    Console.WriteLine("ErrorCode: {0}", ex.ErrorCode);
    Console.WriteLine("ErrorMessage: {0}", ex.ErrorMessage);
}

配置所有请求重定向

以下代码展示了如何配置所有请求重定向:

// 创建ObsClient实例
ObsClient client = new ObsClient("*** Provide your Access Key ***", "*** Provide your Secret Key ***", "https://your-endpoint");
// 配置所有请求重定向
try
{
    SetBucketWebsiteRequest request = new SetBucketWebsiteRequest();
    request.BucketName = "bucketname";
    request.Configuration = new WebsiteConfiguration();
    request.Configuration.RedirectAllRequestsTo = new RedirectBasic();
    request.Configuration.RedirectAllRequestsTo.HostName = "www.example.com";
    request.Configuration.RedirectAllRequestsTo.Protocol = ProtocolEnum.Https;
    SetBucketWebsiteResponse response = client.SetBucketWebsite(request);
    Console.WriteLine("Set bucket website response: {0}", response.StatusCode);
}
catch (ObsException ex)
{
    Console.WriteLine("ErrorCode: {0}", ex.ErrorCode);
    Console.WriteLine("ErrorMessage: {0}", ex.ErrorMessage);
}
分享:

    相关文档

    相关产品

文档是否有解决您的问题?

提交成功!

非常感谢您的反馈,我们会继续努力做到更好!

反馈提交失败,请稍后再试!

*必选

请至少选择或填写一项反馈信息

字符长度不能超过200

提交反馈 取消

如您有其它疑问,您也可以通过华为云社区问答频道来与我们联系探讨

跳转到云社区