Setting Website Hosting

If you have any questions during development, post them on the Issues page of GitHub. For details about parameters and usage of each API, see the API Reference

You can call ObsClient.SetBucketWebsite to set website hosting on a bucket.

Configuring the Default Homepage and Error Pages

Sample code:

// Create an instance of ObsClient.
ObsClient client = new ObsClient("*** Provide your Access Key ***", "*** Provide your Secret Key ***", "https://your-endpoint");
//Configure the homepage and error pages.
try
{
    SetBucketWebsiteRequest request = new SetBucketWebsiteRequest();
    request.BucketName = "bucketname";
    request.Configuration = new WebsiteConfiguration();
    //Configure the default homepage.
    request.Configuration.IndexDocument = "index.html";
    //Configure the error pages.
    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);
}

Configuring the Redirection Rules

Sample code:

// Create an instance of ObsClient.
ObsClient client = new ObsClient("*** Provide your Access Key ***", "*** Provide your Secret Key ***", "https://your-endpoint");
// Set the redirection rule.
try
{
    SetBucketWebsiteRequest request = new SetBucketWebsiteRequest();
    request.BucketName = "bucketname";
    request.Configuration = new WebsiteConfiguration(); //Configure the default homepage.
    request.Configuration.IndexDocument= "index.html";
    //Configure the error pages.
    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);
}

Configuring Redirection for All Requests

Sample code:

// Create an instance of ObsClient.
ObsClient client = new ObsClient("*** Provide your Access Key ***", "*** Provide your Secret Key ***", "https://your-endpoint");
// Configure that all requests will be redirected.
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);
}