Help Center/ Object Storage Service/ API Reference/ APIs/ Static Website Hosting/ Configuring Static Website Hosting for a Bucket
Updated on 2024-09-29 GMT+08:00

Configuring Static Website Hosting for a Bucket

Functions

OBS allows you to store static web page resources such as HTML web pages, flash files, videos, and audios in a bucket. When a client accesses these resources from the website endpoint of the bucket, the browser can directly resolve and present the resources to the client. This operation is applicable to:

  • Redirecting all requests to a website endpoint.
  • Adding routing rules that redirect specific requests.

You can perform this operation to create or update the website configuration of a bucket.

To perform this operation, you must have the PutBucketWebsite permission. By default, only the bucket owner can perform this operation. The bucket owner can grant the permission to other users by configuring the bucket policy or user policy.

Avoid using periods (.) in the destination bucket name. Otherwise, failures in client authentication certificate may occur when users use HTTPS for access.

The maximum size of a network configuration request for a bucket is 10 KB.

Request Syntax

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
PUT /?website HTTP/1.1 
Host: bucketname.obs.region.myhuaweicloud.com 
Content-Length: length
Date: date
Authorization: authorization
<WebsiteConfiguration> 
    <RedirectAllRequestsTo> 
        <HostName>hostName</HostName> 
    </RedirectAllRequestsTo> 
</WebsiteConfiguration>

Request Parameters

This request contains no parameters.

Request Headers

This request uses common headers. For details, see Table 3.

Request Elements

This request contains elements to specify the website configuration in XML format.

  • To redirect all website requests sent to the bucket's website endpoint, add the elements as described in Table 1.
    Table 1 Elements for redirecting all website requests

    Element

    Description

    Mandatory

    WebsiteConfiguration

    Root node configured on the website

    Type: container

    Parent: none

    Yes

    RedirectAllRequestsTo

    Describes the redirection behavior for every request to this bucket's website endpoint. If this element is present, no other siblings are allowed.

    Type: container

    Parent: WebsiteConfiguration

    Yes

    HostName

    Name of the host where requests will be redirected

    Type: string

    Parent: RedirectAllRequestsTo

    Yes

    Protocol

    The HTTP or HTTPS protocol used in redirecting requests. The default protocol is HTTP.

    Type: string

    Parent: RedirectAllRequestsTo

    No

  • To configure redirection rules, add the elements as described in Table 2.
    Table 2 Elements for adding rules that redirect requests

    Element

    Description

    Mandatory

    WebsiteConfiguration

    Root element for the website configuration

    Type: container

    Parent: none

    Yes

    IndexDocument

    Suff element

    Type: container

    Parent: WebsiteConfiguration

    Yes

    Suffix

    Suffix that is appended to a request initiated for a directory on the website endpoint. For example, if the suffix is index.html and you request for samplebucket/images/, the data that is returned will be for the object with the key name images/index.html in the samplebucket bucket. Suffix cannot be empty or contain slashes (/).

    Type: string

    Parent: IndexDocument

    Yes

    ErrorDocument

    Key element

    Type: container

    Parent: WebsiteConfiguration

    No

    Key

    Object key that is used when a 4XX error occurs. This element identifies the page that is returned when a 4XX error occurs.

    Type: string

    Parent: ErrorDocument

    Condition: Required when ErrorDocument is specified.

    No

    RoutingRules

    Routing element

    Type: container

    Parent: WebsiteConfiguration

    No

    RoutingRule

    Element of a redirection rule. A redirection rule contains a Condition and a Redirect. When the Condition is matched, Redirect takes effect.

    Type: container

    Parent: RoutingRules

    At least the RoutingRule element is required.

    Yes

    Condition

    Element for describing a condition that must be met for the specified redirection to apply.

    Type: container

    Parent: RoutingRule

    No

    KeyPrefixEquals

    Object key name prefix when the redirection is applied.

    Example:

    • To redirect the request for object ExamplePage.html, the KeyPrefixEquals is set to ExamplePage.html.

    Type: string

    Parent: Condition

    Condition: Required when the ancestor element Condition is specified and sibling HttpErrorCodeReturnedEquals is not specified. If two conditions are specified, both conditions must be true for the Redirect to be applied.

    No

    HttpErrorCodeReturnedEquals

    HTTP error code returned after the Redirect has taken effect. The specified Redirect is applied only when the error code returned equals this value.

    Example:

    • If you want to redirect requests to NotFound.html when HTTP error code 404 is returned, set HttpErrorCodeReturnedEquals to 404 in Condition, and set ReplaceKeyWith to NotFound.html in Redirect.

    Type: string

    Parent: Condition

    Condition: Required when ancestor element Condition is specified and sibling KeyPrefixEquals is not specified. If multiple conditions are specified, the Redirect takes effect only after all conditions are met.

    No

    Redirect

    Element for redirection information. You can redirect requests to another host, to another web page, or with another protocol. You can specify an error code to be returned after an error.

    Type: container

    Parent: RoutingRule

    Yes

    Protocol

    Protocol used in the redirection request

    Type: string

    Parent: Redirect

    Value options: http, https

    Condition: Not required if one of the siblings is present.

    No

    HostName

    Host name used in the redirection request.

    Type: string

    Parent: Redirect

    Condition: Not required if one of the siblings is present.

    No

    ReplaceKeyPrefixWith

    The object name prefix used in the redirection request. OBS replaces the value of KeyPrefixEquals with the value you specified here for ReplaceKeyPrefixWith.

    Example:

    To redirect all requests for docs (objects in the docs directory) to documents (objects in the documents directory), set KeyPrefixEquals to docs under Condition and ReplaceKeyPrefixWith to documents under Redirect. This way, requests for object docs/a.html will be redirected to documents/a.html.

    Type: string

    Parent: Redirect

    Condition: Not required if one of the siblings is present. Can be present only if ReplaceKeyWith is not provided.

    No

    ReplaceKeyWith

    The object name used in the redirection request. OBS replaces the entire object name in the request with the value you specified here for ReplaceKeyWith.

    Example:

    To redirect requests for all objects in the docs directory to documents/error.html, set KeyPrefixEquals to docs under Condition and ReplaceKeyWith to documents/error.html under Redirect. This way, requests for both objects docs/a.html and docs/b.html will be redirected to documents/error.html.

    Type: string

    Parent: Redirect

    Condition: Not required if one of the siblings is present. Can be present only if ReplaceKeyPrefixWith is not provided.

    No

    HttpRedirectCode

    HTTP status code returned after the redirection request

    Type: string

    Parent: Redirect

    Condition: Not required if one of the siblings is present.

    No

Response Syntax

1
2
3
HTTP/1.1 status_code
Date: date
Content-Length: length

Response Headers

The response to the request uses common headers. For details, see Table 1.

Response Elements

This response contains no elements.

Error Responses

No special error responses are returned. For details about error responses, see Table 2.

Sample Request: Redirecting All Requests for a Bucket to Another Bucket or URL

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
PUT /?website HTTP/1.1
User-Agent: curl/7.29.0
Host: examplebucket.obs.region.myhuaweicloud.com
Accept: */*
Date: WED, 01 Jul 2015 03:40:29 GMT
Authorization: OBS H4IPJX0TQTHTHEBQQCEC:pUK7Yp0yebnq4P6gqzVjoS7whoM=
Content-Length: 194

<WebsiteConfiguration xmlns="http://obs.ap-southeast-1.myhuaweicloud.com/doc/2015-06-30/">
  <RedirectAllRequestsTo>
    <HostName>www.huaweicloud.com</HostName>
  </RedirectAllRequestsTo>
</WebsiteConfiguration>

Sample Response: Redirecting All Requests for a Bucket to Another Bucket or URL

1
2
3
4
5
6
HTTP/1.1 200 OK
Server: OBS
x-obs-request-id: BF2600000164360D144670B9D02AABC6
x-obs-id-2: 32AAAQAAEAABSAAgAAEAABAAAQAAEAABCSItqMZ/AoFUX97l1xx8s67V3cCQtXWk
Date: WED, 01 Jul 2015 03:40:29 GMT
Content-Length: 0

Sample Request: Configuring a Bucket to Host a Static Website

PUT /?website HTTP/1.1
Authorization: OBS H4IPJX0TQTHTHEBQQCEC:iqSPeUBl66PwXDApxjRKk6hlcN4=
User-Agent: curl/7.29.0
Host: examplebucket.obs.region.myhuaweicloud.com
Date: WED, 01 Jul 2015 02:37:22 GMT
Content-Type: application/xml
 
<WebsiteConfiguration  xmlns='http://obs.region.myhuaweicloud.com/doc/2015-06-30/'>
    <IndexDocument>
        <Suffix>index.html</Suffix>
    </IndexDocument>
    <ErrorDocument>
        <Key>SomeErrorDocument.html</Key>
    </ErrorDocument>
</WebsiteConfiguration>

Sample Response: Configuring a Bucket to Host a Static Website

x-obs-id-2: 32AAAQAAEAABSAAgAAEAABAAAQAAEAABCSfxlr+FrXuJzYpLod1lrLK45tVx+GPR
x-obs-request-id: 0000018A39F07D0DD3888442DC29719E
Server: OBS
Content-Length: 0
Date: WED, 01 Jul 2015 02:37:22 GMT

Sample Request: Configuring a Bucket to Host a Static Website, with an Optional Redirection Rule Specified

PUT /?website HTTP/1.1
Authorization: OBS H4IPJX0TQTHTHEBQQCEC:iqSPeUBl66PwXDApxjRKk6hlcN4=
User-Agent: curl/7.29.0
Host: examplebucket.obs.region.myhuaweicloud.com
Date: WED, 01 Jul 2015 02:37:22 GMT
Content-Type: application/xml
 
<WebsiteConfiguration  xmlns='http://obs.region.myhuaweicloud.com/doc/2015-06-30/'>
    <IndexDocument>
        <Suffix>index.html</Suffix>
    </IndexDocument>
    <ErrorDocument>
        <Key>Error.html</Key>
    </ErrorDocument>
    <RoutingRules>
        <RoutingRule>
            <Condition>
                <KeyPrefixEquals>docs/</KeyPrefixEquals>
            </Condition>
            <Redirect>
                <ReplaceKeyPrefixWith>documents/</ReplaceKeyPrefixWith>
            </Redirect>
        </RoutingRule>
    </RoutingRules>
</WebsiteConfiguration>

Sample Response: Configuring a Bucket to Host a Static Website, with an Optional Redirection Rule Specified

x-obs-id-2: 32AAAQAAEAABSAAgAAEAABAAAQAAEAABCSxixo46vLYhGrY/zwgqlM0fMNpeFthi
x-obs-request-id: 0000018A39F2E328D3888F46DB9BB5A2
Server: OBS
Content-Length: 0
Date: WED, 01 Jul 2015 02:37:22 GMT

Sample Request: Configuring a Bucket to Host a Static Website and Redirecting Errors

PUT /?website HTTP/1.1
Authorization: OBS H4IPJX0TQTHTHEBQQCEC:iqSPeUBl66PwXDApxjRKk6hlcN4=
User-Agent: curl/7.29.0
Host: examplebucket.obs.region.myhuaweicloud.com
Date: WED, 01 Jul 2015 02:37:22 GMT
Content-Type: application/xml
 
<WebsiteConfiguration  xmlns='http://obs.region.myhuaweicloud.com/doc/2015-06-30/'>
    <IndexDocument>
        <Suffix>index.html</Suffix>
    </IndexDocument>
    <ErrorDocument>
        <Key>Error.html</Key>
    </ErrorDocument>
    <RoutingRules>
        <RoutingRule>
            <Condition>
                <HttpErrorCodeReturnedEquals>404</HttpErrorCodeReturnedEquals >
            </Condition>
            <Redirect>
                <HostName>www.huaweicloud.com</HostName>
                <ReplaceKeyPrefixWith>report-404/</ReplaceKeyPrefixWith>
            </Redirect>
        </RoutingRule>
    </RoutingRules>
</WebsiteConfiguration>

Sample Response: Configuring a Bucket to Host a Static Website and Redirecting Errors

x-obs-id-2: 32AAAQAAEAABSAAgAAEAABAAAQAAEAABCS/xBBLGZwRUiL439eWMw1v/vphFB6JY
x-obs-request-id: 0000018A3A06C048D38610C04366B2F5
Server: OBS
Content-Length: 0
Date: WED, 01 Jul 2015 02:37:22 GMT

Sample Request: Configuring a Bucket to Host a Static Website and Redirecting Requests for Folders to Another Page

PUT /?website HTTP/1.1
Authorization: OBS H4IPJX0TQTHTHEBQQCEC:iqSPeUBl66PwXDApxjRKk6hlcN4=
User-Agent: curl/7.29.0
Host: examplebucket.obs.region.myhuaweicloud.com
Date: WED, 01 Jul 2015 02:37:22 GMT
Content-Type: application/xml
 
<WebsiteConfiguration  xmlns='http://obs.region.myhuaweicloud.com/doc/2015-06-30/'>
    <IndexDocument>
        <Suffix>index.html</Suffix>
    </IndexDocument>
    <ErrorDocument>
        <Key>Error.html</Key>
    </ErrorDocument>
    <RoutingRules>
        <RoutingRule>
            <Condition>
                <KeyPrefixEquals>images/</KeyPrefixEquals>
            </Condition>
            <Redirect>
                <ReplaceKeyWith>errorpage.html</ReplaceKeyWith>
            </Redirect>
        </RoutingRule>
    </RoutingRules>
</WebsiteConfiguration>

Sample Response: Configuring a Bucket to Host a Static Website and Redirecting Requests for Folders to Another Page

x-obs-id-2: 32AAAQAAEAABSAAgAAEAABAAAQAAEAABCSLjAKDDyha8LY/HcoFNfbLBeAKCAYcv
x-obs-request-id: 0000018A3A09D30CD306902FC7572429
Server: OBS
Content-Length: 0
Date: WED, 01 Jul 2015 02:37:22 GMT