หน้านี้ยังไม่พร้อมใช้งานในภาษาท้องถิ่นของคุณ เรากำลังพยายามอย่างหนักเพื่อเพิ่มเวอร์ชันภาษาอื่น ๆ เพิ่มเติม ขอบคุณสำหรับการสนับสนุนเสมอมา

Compute
Elastic Cloud Server
Huawei Cloud Flexus
Bare Metal Server
Auto Scaling
Image Management Service
Dedicated Host
FunctionGraph
Cloud Phone Host
Huawei Cloud EulerOS
Networking
Virtual Private Cloud
Elastic IP
Elastic Load Balance
NAT Gateway
Direct Connect
Virtual Private Network
VPC Endpoint
Cloud Connect
Enterprise Router
Enterprise Switch
Global Accelerator
Management & Governance
Cloud Eye
Identity and Access Management
Cloud Trace Service
Resource Formation Service
Tag Management Service
Log Tank Service
Config
OneAccess
Resource Access Manager
Simple Message Notification
Application Performance Management
Application Operations Management
Organizations
Optimization Advisor
IAM Identity Center
Cloud Operations Center
Resource Governance Center
Migration
Server Migration Service
Object Storage Migration Service
Cloud Data Migration
Migration Center
Cloud Ecosystem
KooGallery
Partner Center
User Support
My Account
Billing Center
Cost Center
Resource Center
Enterprise Management
Service Tickets
HUAWEI CLOUD (International) FAQs
ICP Filing
Support Plans
My Credentials
Customer Operation Capabilities
Partner Support Plans
Professional Services
Analytics
MapReduce Service
Data Lake Insight
CloudTable Service
Cloud Search Service
Data Lake Visualization
Data Ingestion Service
GaussDB(DWS)
DataArts Studio
Data Lake Factory
DataArts Lake Formation
IoT
IoT Device Access
Others
Product Pricing Details
System Permissions
Console Quick Start
Common FAQs
Instructions for Associating with a HUAWEI CLOUD Partner
Message Center
Security & Compliance
Security Technologies and Applications
Web Application Firewall
Host Security Service
Cloud Firewall
SecMaster
Anti-DDoS Service
Data Encryption Workshop
Database Security Service
Cloud Bastion Host
Data Security Center
Cloud Certificate Manager
Edge Security
Blockchain
Blockchain Service
Web3 Node Engine Service
Media Services
Media Processing Center
Video On Demand
Live
SparkRTC
MetaStudio
Storage
Object Storage Service
Elastic Volume Service
Cloud Backup and Recovery
Storage Disaster Recovery Service
Scalable File Service Turbo
Scalable File Service
Volume Backup Service
Cloud Server Backup Service
Data Express Service
Dedicated Distributed Storage Service
Containers
Cloud Container Engine
SoftWare Repository for Container
Application Service Mesh
Ubiquitous Cloud Native Service
Cloud Container Instance
Databases
Relational Database Service
Document Database Service
Data Admin Service
Data Replication Service
GeminiDB
GaussDB
Distributed Database Middleware
Database and Application Migration UGO
TaurusDB
Middleware
Distributed Cache Service
API Gateway
Distributed Message Service for Kafka
Distributed Message Service for RabbitMQ
Distributed Message Service for RocketMQ
Cloud Service Engine
Multi-Site High Availability Service
EventGrid
Dedicated Cloud
Dedicated Computing Cluster
Business Applications
Workspace
ROMA Connect
Message & SMS
Domain Name Service
Edge Data Center Management
Meeting
AI
Face Recognition Service
Graph Engine Service
Content Moderation
Image Recognition
Optical Character Recognition
ModelArts
ImageSearch
Conversational Bot Service
Speech Interaction Service
Huawei HiLens
Video Intelligent Analysis Service
Developer Tools
SDK Developer Guide
API Request Signing Guide
Terraform
Koo Command Line Interface
Content Delivery & Edge Computing
Content Delivery Network
Intelligent EdgeFabric
CloudPond
Intelligent EdgeCloud
Solutions
SAP Cloud
High Performance Computing
Developer Services
ServiceStage
CodeArts
CodeArts PerfTest
CodeArts Req
CodeArts Pipeline
CodeArts Build
CodeArts Deploy
CodeArts Artifact
CodeArts TestPlan
CodeArts Check
CodeArts Repo
Cloud Application Engine
MacroVerse aPaaS
KooMessage
KooPhone
KooDrive
Help Center/ Terraform/ User Guide/ Object Storage Service (OBS)/ Configuring Static Website Hosting

Configuring Static Website Hosting

Updated on 2024-08-08 GMT+08:00

Application Scenario

OBS allows static websites to be hosted on buckets and supports index page, error page display, and page redirection. You can upload the content files of the static website to your bucket on OBS and configure a read permission to anonymous users for these files, and then configure the static website hosting mode for your bucket to host your static websites on OBS.

Procedure

  1. Create an OBS bucket and configure static website hosting.

    1. Create the main.tf file, enter the following information, and save the file:
      resource "huaweicloud_obs_bucket" "mywebsite" {
        bucket = "mywebsite"
        website {
          index_document = "index.html"
          error_document = "error.html"
        }
      }
    2. Run terraform init to initialize the environment.
    3. Run terraform plan to view resources.
    4. After you confirm that the resource information is correct, run terraform apply to start OBS bucket creation.
    5. Run terraform show to view the created OBS bucket.

  2. Configure a bucket policy to allow anonymous users to access objects in the bucket.

    Add the following information to the main.tf file:

    # Grant the Read-Only permission to anonymous users.
    resource "huaweicloud_obs_bucket_policy" "policy" {
      bucket = huaweicloud_obs_bucket.mywebsite.bucket
      policy = <<POLICY
    {
      "Statement": [
        {
          "Sid": "AddPerm",
          "Effect": "Allow",
          "Principal": {"ID": "*"},
          "Action": ["GetObject"],
          "Resource": "mywebsite/*"
        } 
      ]
    }
    POLICY
    }

  3. Upload static website files.

    1. Edit the index.html and error.html files in the current directory.
    2. Add the following information to the main.tf file and upload the files to the OBS bucket:
      # put index.html
      resource "huaweicloud_obs_bucket_object" "index" {
        bucket = huaweicloud_obs_bucket.mywebsite.bucket
        key    = "index.html"
        source = "index.html"
      }
      # put error.html
      resource "huaweicloud_obs_bucket_object" "error" {
        bucket = huaweicloud_obs_bucket.mywebsite.bucket
        key    = "error.html"
        source = "error.html"
      }
    3. Run terraform plan to view resources.
    4. After you confirm that the resource information is correct, run terraform apply to start file uploading.

  4. Verify the configuration.

    Use a browser to access https://mywebsite.obs-website.cn-north-4.myhuaweicloud.com, that is, to access index.html. mywebsite indicates the OBS bucket name, and cn-north-4 indicates the region to which the bucket belongs.

Table 1 Parameter description

Resource Name

Parameter

Description

huaweicloud_obs_bucket

bucket

(Mandatory) OBS bucket name.

An OBS bucket name:

  • Must be globally unique in OBS.
  • Contains 3 to 63 characters, including lowercase letters, digits, hyphens (-), and periods (.).
  • Cannot start or end with a period (.) or hyphen (-).
  • Cannot contain two consecutive periods (.) or adjacent periods and hyphens (.- or -.).
  • Cannot be an IP address.

website

index_document

(Mandatory) The index page that is returned when you access a static website, that is, the homepage.

error_document

(Optional) The 404 error page that is returned when an incorrect static website path is accessed.

routing_rules

(Optional) Rule for redirecting the static website.

huaweicloud_obs_bucket_policy

bucket

(Mandatory) Bucket name.

policy_format

(Optional) Policy format. The value can be obs or s3. The default value is obs.

policy

(Mandatory) Policy content. For details, see Policy Format.

huaweicloud_obs_bucket_object

bucket

(Mandatory) Bucket name.

key

(Mandatory) Object name.

source

(Optional) Path to the source file of the object.

Follow-up Operation

You can bind a user-defined domain name to the access domain name of an OBS bucket so that you can access files stored in OBS through the user-defined domain name. With the domain name management of OBS, you can also use CDN for service acceleration. For details, see Using a User-Defined Domain Name to Host a Static Website.

เราใช้คุกกี้เพื่อปรับปรุงไซต์และประสบการณ์การใช้ของคุณ การเรียกดูเว็บไซต์ของเราต่อแสดงว่าคุณยอมรับนโยบายคุกกี้ของเรา เรียนรู้เพิ่มเติม

Feedback

Feedback

Feedback

0/500

Selected Content

Submit selected content with the feedback