Hosting setup
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 the hosting for. - In the left-hand panel, select
Settings. - Go to the General tab.
- Allow public access to operations with the bucket.
- 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.,
pages/index.html
. - (Optional) In the Error page field, specify the absolute path to the file in the bucket to be displayed in the event of 4xx errors, e.g.,
pages/error404.html
. By default, Object Storage returns its own page.
- In the Home page field, specify the absolute path to the file in the bucket for the website home page, e.g.,
- Click Save.
Use the link in Link to check the hosting.
If you do not have the Yandex Cloud command line interface yet, install and initialize it.
The folder specified in the CLI profile is used by default. You can specify a different folder using the --folder-name
or --folder-id
parameter.
-
View a description of the CLI command to set up static website hosting in a bucket:
yc storage bucket update --help
-
Create a hosting settings file in JSON format. For example:
{ "index": "index.html", "error": "error404.html" }
Where:
index
: Absolute path to the website home page file.error
: Absolute path to the file the user will see in case of 4xx errors.
-
Run the following 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 settings 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 a key ID used for authentication in Object Storage.
-
In the configuration file, describe 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.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 folder where you created the configuration file.
-
Run a check using this command:
terraform plan
If the configuration is described correctly, the terminal will display a list of created resources and their parameters. If the configuration contains any errors, Terraform will point them out.
-
-
Deploy cloud resources.
- If the configuration does not contain any errors, run this command:
terraform apply
- Confirm that you want to create the resources.
All the resources you need will then be created in the specified folder. You can check the new resources and their settings using the management console
.
To set up hosting for a static website, use the update REST API method for the Bucket resource, the BucketService/Update gRPC API call, or the upload S3 API method.
Redirect all requests
- In the management console
, select Object Storage from the list of services and go to the bucket you want to configure the 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.
- (Optional) Protocol if the specified host accepts requests only over a specific protocol.
- Click Save.
If you do not have the Yandex Cloud command line interface yet, install and initialize it.
The folder specified in the CLI profile is used by default. You can specify a different folder using the --folder-name
or --folder-id
parameter.
-
View a description of the CLI command to set up a redirect for all requests:
yc storage bucket update --help
-
Create a file with redirect settings in JSON format, For example:
{ "redirectAllRequests": { "protocol": "PROTOCOL_HTTP", "hostname": "example.com" } }
Where:
protocol
: Data transfer protocol,PROTOCOL_HTTP
orPROTOCOL_HTTPS
. By default, the original request's protocol is used.hostname
: Domain name of the host to act as the redirect target for all requests to the current bucket.
-
Run the following 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_homepage_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's protocol is used.
For more information about the
yandex_storage_bucket
resource parameters in Terraform, see the provider documentation . -
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 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 a redirect for all 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.
Conditional request redirection
Using routing rules, you can redirect requests based on the object name prefixes or HTTP response codes. You can redirect an object request to other web pages (if the object was deleted) 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 the 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 should have responded with to a request without a redirect.
- Key prefix: Object key start in the request.
- Under Redirect, set redirect parameters:
- Protocol Protocol to use to send redirected requests.
- Domain name of the host where the requests that satisfy the condition should redirect.
- 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 command line interface yet, install and initialize it.
The folder specified in the CLI profile is used by default. You can specify a different folder using the --folder-name
or --folder-id
parameter.
-
View a description of the CLI command to set up a conditional redirect of requests:
yc storage bucket update --help
-
Create a file with conditional redirect settings in JSON format. For 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's protocol is used.replaceKeyPrefixWith
: New object key prefix.replaceKeyWith
: New object key.
-
-
Run the following 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 a conditional redirect of requests:
-
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_homepage_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 respective API method (the For conditionally redirecting requests tab).
For more information about the
yandex_storage_bucket
resource parameters in Terraform, see the provider documentation . -
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 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 a conditional redirect of 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.