Setting up hosting
You can host your static website in Object Storage. A static website is based on such client-side technologies as HTML, CSS, or JavaScript. It may not contain any scripts that run on the web server side.
Object Storage buckets support:
Static website hosting
- In the management console
, select Object Storage from the list of services and go to the bucket you want to configure hosting for. - In the left-hand panel, select
Settings. - Navigate to the General tab.
- Enable public access to bucket operations.
- Click Save.
- Select the Website tab.
- Under Hosting:
-
In the Home page field, specify the absolute path to the file in the bucket for the website home page, e.g.,
index.html
.Warning
The key of the object containing your website homepage must not include the
/
character. -
Optionally, in the Error page field, specify the absolute path to the file in the bucket to show for 4xx errors, e.g.,
pages/error404.html
. By default, Object Storage returns its own page.
-
- Click Save.
Use the link in Link to check the hosting.
If you do not have the Yandex Cloud CLI yet, install and initialize it.
The folder specified in the CLI profile is used by default. You can specify a different folder through the --folder-name
or --folder-id
parameter.
-
See the description of the CLI command for setting up static website hosting in a bucket:
yc storage bucket update --help
-
Create a hosting configuration file in JSON format. Here is an example:
{ "index": "index.html", "error": "error404.html" }
Where:
-
index
: Absolute path to the website home page file.Warning
The key of the object containing your website homepage must not include the
/
character. -
error
: Absolute path to the file the user will see in case of 4xx errors.
-
-
Run this command:
yc storage bucket update --name <bucket_name> \ --website-settings-from-file <path_to_file>
Where:
--name
: Bucket name.--website-settings-from-file
: Path to the hosting configuration file.
Result:
name: my-bucket folder_id: b1gjs8dck8bv******** default_storage_class: STANDARD versioning: VERSIONING_SUSPENDED max_size: "10737418240" acl: {} created_at: "2022-12-14T08:42:16.273717Z"
To make sure the bucket description now contains the hosting settings, run this command:
yc storage --name <bucket_name> bucket get --full
Result:
website_settings:
index: index.html
error: error404.html
redirect_all_requests: {}
Note
Terraform uses a service account to interact with Object Storage. Assign to the service account the required role, e.g., storage.admin
, for the folder where you are going to create resources.
If you don't have Terraform, install it and configure the Yandex Cloud provider.
Before you start, retrieve the static access keys: a secret key and key ID used for Object Storage authentication.
-
In the configuration file, define the parameters of the resources you want to create:
provider "yandex" { token = "<OAuth>" cloud_id = "<cloud_ID>" folder_id = "<folder_ID>" zone = "ru-central1-a" } resource "yandex_iam_service_account" "sa" { name = "<service_account_name>" } // Assigning a role to a service account resource "yandex_resourcemanager_folder_iam_member" "sa-admin" { folder_id = "<folder_ID>" role = "storage.admin" member = "serviceAccount:${yandex_iam_service_account.sa.id}" } // Creating a static access key resource "yandex_iam_service_account_static_access_key" "sa-static-key" { service_account_id = yandex_iam_service_account.sa.id description = "static access key for object storage" } resource "yandex_storage_bucket" "test" { access_key = yandex_iam_service_account_static_access_key.sa-static-key.access_key secret_key = yandex_iam_service_account_static_access_key.sa-static-key.secret_key bucket = "<bucket_name>" acl = "public-read" website { index_document = "index.html" error_document = "error.html" } }
Where:
access_key
: Static access key ID.secret_key
: Secret access key value.bucket
: Bucket name.acl
: ACL access management parameters.website
: Website parameters:-
index_document
: Absolute path to the website home page file. This is a required parameter.Warning
The key of the object containing your website homepage must not include the
/
character. -
error_document
: Absolute path to the file the user will see in case of 4xx errors. This is an optional parameter.
-
-
Make sure the configuration files are correct.
-
In the command line, go to the directory where you created the configuration file.
-
Run a check using this command:
terraform plan
If you described the configuration correctly, the terminal will display a list of the resources being created and their parameters. If the configuration contains any errors, Terraform will point them out.
-
-
Deploy the cloud resources.
- If the configuration does not contain any errors, run this command:
terraform apply
- Confirm creating the resources.
This will create all the resources you need in the specified folder. You can check the new resources and their settings using the management console
.
To set up static website hosting, use the update REST API method for the Bucket resource, the BucketService/Update gRPC API call, or the upload S3 API method.
Redirects for all requests
- In the management console
, select Object Storage from the list of services and go to the bucket you want to configure request redirects for. - In the left-hand panel, select
Settings. - Select the Website tab.
- Under Redirect, specify:
- Domain name of the host to act as the redirect target for all requests to the bucket.
- Optionally, Protocol if the specified host accepts requests only over a specific protocol.
- Click Save.
If you do not have the Yandex Cloud CLI yet, install and initialize it.
The folder specified in the CLI profile is used by default. You can specify a different folder through the --folder-name
or --folder-id
parameter.
-
See the description of the CLI command for setting up redirects for all requests:
yc storage bucket update --help
-
Create a redirect configuration file in JSON format. Here is an example:
{ "redirectAllRequests": { "protocol": "PROTOCOL_HTTP", "hostname": "example.com" } }
Where:
protocol
: Data transfer protocol,PROTOCOL_HTTP
orPROTOCOL_HTTPS
. By default, the original request protocol is used.hostname
: Domain name of the host to act as the redirect target for all requests to the current bucket.
-
Run this command:
yc storage bucket update --name <bucket_name> \ --website-settings-from-file <path_to_file>
Where:
--name
: Bucket name.--website-settings-from-file
: Path to the redirect configuration file.
Result:
name: my-bucket folder_id: b1gjs8dck8bv******** default_storage_class: STANDARD versioning: VERSIONING_SUSPENDED max_size: "10737418240" acl: {} created_at: "2022-12-14T08:42:16.273717Z"
With Terraform
Terraform is distributed under the Business Source License
For more information about the provider resources, see the documentation on the Terraform
If you don't have Terraform, install it and configure the Yandex Cloud provider.
To set up a redirect for all requests:
-
Open the Terraform configuration file and add the
redirect_all_requests_to
parameter to theyandex_storage_bucket
resource description:... resource "yandex_storage_bucket" "test" { access_key = "<static_key_ID>" secret_key = "<secret_key>" bucket = "<bucket_name>" acl = "public-read" website { index_document = "<absolute_path_to_website_home_page_file>" error_document = "<absolute_path_to_error_file>" redirect_all_requests_to = "<host_name>" } } ...
Where:
access_key
: Static access key ID.secret_key
: Secret access key value.bucket
: Bucket name.acl
: ACL access management parameters.website
: Website parameters:index_document
: Absolute path to the website home page file. This is a required parameter.error_document
: Absolute path to the file the user will see in case of 4xx errors. This is an optional parameter.redirect_all_requests_to
: Domain name of the host to act as the redirect target for all requests to the current bucket. You can specify a protocol prefix (http://
orhttps://
). By default, the original request protocol is used.
For more information about the
yandex_storage_bucket
resource parameters in Terraform, see this TF provider article . -
Check the configuration using this command:
terraform validate
If the configuration is correct, you will get this message:
Success! The configuration is valid.
-
Run this command:
terraform plan
The terminal will display a list of resources with their parameters. No changes will be made at this step. If the configuration contains any errors, Terraform will point them out.
-
Apply the configuration changes:
terraform apply
-
Confirm the changes: type
yes
into the terminal and press Enter.You can use the management console
to check the request redirect settings.
To set up redirects for all requests to a bucket, use the update REST API method for the Bucket resource, the BucketService/Update gRPC API call, or the upload S3 API method.
Request redirects from HTTP to HTTPS are enabled automatically once you set up HTTPS access to a bucket. No other settings are required.
Conditional request redirects
With routing rules, you can redirect requests based on the object name prefixes or HTTP response codes. This enables you to redirect object requests to different web pages (if the object was removed) or redirect the requests that return errors.
- In the management console
, select Object Storage from the list of services and go to the bucket you want to configure conditional request redirects for. - In the left-hand panel, select
Settings. - Select the Website tab.
- In Hosting, under Redirect, click Add forwarding rule.
- Under Condition, specify at least one condition for redirects:
- Response code: HTTP code that Object Storage would have returned for the request without a redirect.
- Key prefix: Object key prefix in the request. You can learn more about keys and how static websites work here.
- Under Redirect, set the following redirect parameters:
- Protocol to use for sending redirected requests.
- Domain name of the host to which all requests meeting the specified condition will be redirected.
- Response code to determine the redirect type.
- Replace key: None, Whole key, or Prefix only, specified in the condition.
- Click Save.
If you do not have the Yandex Cloud CLI yet, install and initialize it.
The folder specified in the CLI profile is used by default. You can specify a different folder through the --folder-name
or --folder-id
parameter.
-
See the description of the CLI command for setting up conditional request redirects:
yc storage bucket update --help
-
Create a conditional redirect configuration file in JSON format. Here is an example:
{ "routingRules": [ { "condition": { "httpErrorCodeReturnedEquals": "404", "keyPrefixEquals": "key" }, "redirect": { "hostname": "example.com", "httpRedirectCode": "301", "protocol": "PROTOCOL_HTTP", "replaceKeyPrefixWith": "prefix", "replaceKeyWith": "key" } } ] }
Where:
-
condition
: Condition to trigger a redirect:httpErrorCodeReturnedEquals
: HTTP response code.keyPrefixEquals
: Object key prefix.
-
redirect
: Redirect settings:hostname
: Domain name of the host to act as the redirect target for all requests to the current bucket.httpRedirectCode
: New HTTP response code.protocol
: New data transfer protocol,PROTOCOL_HTTP
orPROTOCOL_HTTPS
. By default, the original request protocol is used.replaceKeyPrefixWith
: New object key prefix.replaceKeyWith
: New object key.
-
-
Run this command:
yc storage bucket update --name <bucket_name> \ --website-settings-from-file <path_to_file>
Where:
--name
: Bucket name.--website-settings-from-file
: Path to the conditional redirect configuration file.
Result:
name: my-bucket folder_id: b1gjs8dck8bv******** default_storage_class: STANDARD versioning: VERSIONING_SUSPENDED max_size: "10737418240" acl: {} created_at: "2022-12-14T08:42:16.273717Z"
With Terraform
Terraform is distributed under the Business Source License
For more information about the provider resources, see the documentation on the Terraform
If you don't have Terraform, install it and configure the Yandex Cloud provider.
To set up conditional request redirects:
-
Open the Terraform configuration file and add the
routing_rules
parameter to the bucket description:... resource "yandex_storage_bucket" "test" { access_key = "<static_key_ID>" secret_key = "<secret_key>" bucket = "<bucket_name>" acl = "public-read" website { index_document = "<absolute_path_to_website_home_page_file>" error_document = "<absolute_path_to_error_file>" routing_rules = <<EOF [ { "Condition": { "KeyPrefixEquals": "<object_key_prefix>", "HttpErrorCodeReturnedEquals": "<HTTP_response_code>" }, "Redirect": { "Protocol": "<new_schema>", "HostName": "<new_domain_name>", "ReplaceKeyPrefixWith": "<new_object_key_prefix>", "ReplaceKeyWith": "<new_object_key>", "HttpRedirectCode": "<new_HTTP_response_code>" } }, ... ] EOF } } ...
Where:
access_key
: Static access key ID.secret_key
: Secret access key value.bucket
: Bucket name.acl
: ACL access management parameters.website
: Website parameters:index_document
: Absolute path to the website home page file. This is a required parameter.error_document
: Absolute path to the file the user will see in case of 4xx errors. This is an optional parameter.routing_rules
: Rules for redirecting requests in JSON format. Each rule'sCondition
andRedirect
fields must contain at least onekey-value
pair. For more information about the supported fields, see the data schema of the relevant API method (the For conditionally redirecting requests tab).
For more information about the
yandex_storage_bucket
resource parameters in Terraform, see this TF provider article . -
Check the configuration using this command:
terraform validate
If the configuration is correct, you will get this message:
Success! The configuration is valid.
-
Run this command:
terraform plan
The terminal will display a list of resources with their parameters. No changes will be made at this step. If the configuration contains any errors, Terraform will point them out.
-
Apply the configuration changes:
terraform apply
-
Confirm the changes: type
yes
into the terminal and press Enter.You can use the management console
to check the settings for conditionally redirecting requests.
To set up conditional redirects for bucket requests, use the update REST API method for the Bucket resource, the BucketService/Update gRPC API call, or the upload S3 API method.
Request redirects from HTTP to HTTPS are enabled automatically once you set up HTTPS access to a bucket. No other settings are required.