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.
Note
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¶
PUT /?website HTTP/1.1
Host: bucketname.obs.region.example.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.
¶ 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.
¶ 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¶
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¶
PUT /?website HTTP/1.1
User-Agent: curl/7.29.0
Host: examplebucket.obs.region.example.com
Accept: */*
Date: WED, 01 Jul 2015 03:40:29 GMT
Authorization: OBS H4IPJX0TQTHTHEBQQCEC:pUK7Yp0yebnq4P6gqzVjoS7whoM=
Content-Length: 194
<WebsiteConfiguration xmlns="http://obs.example.com/doc/2015-06-30/">
<RedirectAllRequestsTo>
<HostName>www.example.com</HostName>
</RedirectAllRequestsTo>
</WebsiteConfiguration>
Sample Response: Redirecting All Requests for a Bucket to Another Bucket or URL¶
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