文档

静态网站托管

更新时间:

静态网站是指所有的网页都由静态内容构成,包括客户端执行的脚本(例如JavaScript)。您可以通过静态网站托管功能将您的静态网站托管到云盒Bucket,并使用该Bucket的访问域名访问这个网站。

前提条件

  • 华东1(杭州)、华南1(深圳)、华南2(河源)、华北2(北京)、西南1(成都)地域支持使用OSS ON云盒服务。

  • 已购买云盒。具体操作,请参见购买云盒

使用说明

配置静态网站托管时,您需要指定网站的默认首页和默认404页:

  • 默认首页是您通过浏览器访问静态网站域名时,OSS返回的网站首页。

    您为默认首页指定的文件必须是Bucket根目录下允许被匿名访问的文件。如果您还开通了子目录首页,则子目录下也应存在此文件。

  • 默认404页是您通过浏览器访问Bucket内文件出现404错误时,OSS返回的错误页面。

    您为默认404页指定的文件必须是Bucket根目录下允许被匿名访问的文件。

您可以通过将默认首页或者默认404页中指定文件的读写权限ACL设置为public-read,确保该文件允许匿名访问。有关设置文件读写权限ACL的具体步骤,请参见设置Object ACL

配置示例

为云盒Bucket开启静态网站托管后,您需要将与默认首页名称相同的文件(例如index.html)上传至目标Bucket,如果Bucket中包含了目录结构subdir/,则目录层级下也必须包含index.html文件。此外,您还需要将与默认404页名称相同的文件(例如error.html)上传至云盒Bucket。云盒Bucket的文件结构如下所示:

Bucket
 ├── index.html
 ├── error.html
 ├── example.txt
 └── subdir/
      └── index.html

操作步骤

使用OSS控制台

  1. 设置静态网站页面。

    • 未开通子目录首页

      结合以上配置示例可知,当您希望访问子目录subdir/时,不支持跳转至子目录下的index.html页面,而是跳转至根目录下的index.html页面。此外,当访问Bucket内不存在的文件时,返回默认错误页面。具体配置步骤如下:

      1. 登录OSS管理控制台

      2. 在左侧导航栏,选择数据服务 > 云盒Bucket,然后在Bucket列表中单击目标Bucket。

      3. 在左侧导航栏,选择数据管理 > 静态页面

      4. 静态页面,单击设置,按如下说明配置各项参数。静态页面

        参数

        说明

        默认首页

        默认首页是您通过浏览器访问静态网站域名时,OSS返回的网站首页。此处设置为index.html

        子目录首页

        选择不开通,此时访问静态网站根域名或者根域名下任何一个以正斜线(/)结尾的URL都会返回根目录默认首页。

        默认404页

        访问Bucket内文件出现404错误时,OSS返回的错误页面。默认404页仅支持根目录下的文件。此处设置为error.html

        错误文档响应码

        您可以配置返回错误文档时的HTTP响应码为404200

      5. 单击保存

    • 已开通子目录首页

      结合以上配置示例可知,您希望访问子目录subdir/时,支持直接跳转至子目录下的index.html页面。此外,当访问Bucket内不存在的文件时,返回默认错误页面,并通过文件404规则指定访问不存在文件时的返回结果。具体配置步骤如下:

      1. 在左侧导航栏,单击云盒Bucket,然后单击目标Bucket名称。

      2. 在左侧导航栏,选择数据管理 > 静态页面

      3. 静态页面,单击设置,按如下说明配置各项参数。静态页面2

        参数

        说明

        默认首页

        默认首页是您通过浏览器访问静态网站域名时,OSS返回的网站首页。此处设置为index.html

        子目录首页

        选择开通。开通子目录首页后,访问静态网站根域名时,返回根目录默认首页。访问根域名下以正斜线(/)结尾的URL时会返回对应目录的默认首页。例如,访问示例中的https://examplebucket.oss-cn-hangzhou.aliyuncs.com/subdir/时,则返回subdir/目录下的默认首页文件index.html

        文件404规则

        开通子目录首页后,通过文件404规则决定访问不存在的Object时的返回结果。例如,访问https://examplebucket.oss-cn-hangzhou.aliyuncs.com/exampledir,因示例中不存在exampledir文件,则根据设置的文件404规则返回对应信息:

        • Redirect(默认值):检查exampledir/index.html是否存在。

          • 如果文件存在则返回302,并将访问请求重定向为https://examplebucket.oss-cn-hangzhou.aliyuncs.com/exampledir/index.html

          • 如果文件不存在则返回404,并继续检查https://examplebucket.oss-cn-hangzhou.aliyuncs.com/error.html。如果error.html页面也不存在该文件,则返回404状态码。

        • NoSuchKey:直接返回404,并继续检查https://examplebucket.oss-cn-hangzhou.aliyuncs.com/error.html

        • Index:检查exampledir/index.html是否存在。

          • 如果文件存在则返回200,并直接返回文件内容。

          • 如果文件不存在,则继续检查https://examplebucket.oss-cn-hangzhou.aliyuncs.com/error.html

        默认404页

        访问Bucket内文件出现404错误时,OSS返回的错误页面。默认404页仅支持根目录下的文件。此处设置为error.html

        错误文档响应码

        您可以配置返回错误文档时的HTTP响应码为404200

      4. 单击保存

  2. 创建并上传默认首页。

    当您为examplebucket配置静态网站托管时指定的默认首页为index.html,您需要将与默认首页名称相同的文件上传至examplebucket根目录下。由于examplebucket中包含了子目录subdir/,则子目录subdir/下也必须包含index.html文件。

    1. 创建index.html文件。index.html文件配置示例如下:

      <html>
      <head>
          <title>My Website Home Page</title>
          <meta charset="utf-8">
      </head>
      <body>  
        <p>Now hosted on OSS.</p>
      </body>
      </html>
    2. index.html文件保存至本地路径。

    3. 分别将index.html文件上传至examplebucket根目录以及子目录subdir下。上传文件时,您需要将文件读写权限设置为公共读。

      关于上传文件的具体操作,请参见简单上传

  3. 创建并上传默认404页。

    当您为examplebucket配置静态网站托管时指定的默认404页为error.html,您需要将与默认404页名称相同的文件上传至examplebucket根目录下。

    1. 创建error.html文件。error.html文件配置示例如下:

      <html>
      <head>
          <title>Hello OSS!</title>
          <meta charset="utf-8">
      </head>
      <body>  
        <p>This is error 404 page.</p>
      </body>
      </html>
    2. error.html文件保存至本地。

    3. error.html文件上传至examplebucket根目录下。上传文件时,您需要将文件读写权限设置为公共读。

使用阿里云SDK

仅支持通过Java SDK设置静态网站托管,Java SDK要求3.15.0及以上版本。

import com.aliyun.oss.ClientException;
import com.aliyun.oss.OSS;
import com.aliyun.oss.OSSClientBuilder;
import com.aliyun.oss.OSSException;
import com.aliyun.oss.model.SetBucketWebsiteRequest;
import com.aliyun.oss.common.auth.CredentialsProviderFactory;
import com.aliyun.oss.common.auth.EnvironmentVariableCredentialsProvider;

public class Demo {

    public static void main(String[] args) throws Exception {
        // 填写云盒Bucket的数据域名。
        String endpoint = "https://cb-f8z7yvzgwfkl9q0h****.cn-hangzhou.oss-cloudbox.aliyuncs.com";
        // 从环境变量中获取访问凭证。运行本代码示例之前,请确保已设置环境变量OSS_ACCESS_KEY_ID和OSS_ACCESS_KEY_SECRET。
        EnvironmentVariableCredentialsProvider credentialsProvider = CredentialsProviderFactory.newEnvironmentVariableCredentialsProvider();
        // 填写云盒Bucket名称,例如examplebucket。
        String bucketName = "examplebucket";
        // 填写云盒Bucket所在地域。
        String region = "cn-hangzhou";
        // 填写云盒ID。
        String cloudBoxId = "cb-f8z7yvzgwfkl9q0h****";

        // 创建OSSClient实例。
        ClientBuilderConfiguration conf = new ClientBuilderConfiguration();
        conf.setSignatureVersion(SignVersion.V4);
        OSS ossClient = OSSClientBuilder.create()
                .endpoint(endpoint)
                .credentialsProvider(new DefaultCredentialProvider(credentialsProvider.getCredentials()))
                .clientConfiguration(conf)
                .region(region)
                .cloudBoxId(cloudBoxId)
                .build();

        try {
            // 设置静态网站托管。
            SetBucketWebsiteRequest request = new SetBucketWebsiteRequest(bucketName);
            // 设置静态网站托管的默认主页。
            request.setIndexDocument("index.html");
            // 设置静态网站托管的默认404页。
            request.setErrorDocument("error.html");
            ossClient.setBucketWebsite(request);
        } catch (OSSException oe) {
            System.out.println("Caught an OSSException, which means your request made it to OSS, "
                    + "but was rejected with an error response for some reason.");
            System.out.println("Error Message:" + oe.getErrorMessage());
            System.out.println("Error Code:" + oe.getErrorCode());
            System.out.println("Request ID:" + oe.getRequestId());
            System.out.println("Host ID:" + oe.getHostId());
        } catch (ClientException ce) {
            System.out.println("Caught an ClientException, which means the client encountered "
                    + "a serious internal problem while trying to communicate with OSS, "
                    + "such as not being able to access the network.");
            System.out.println("Error Message:" + ce.getMessage());
        } finally {
            if (ossClient != null) {
                ossClient.shutdown();
            }
        }
    }
}

使用命令行工具ossutil

关于使用ossutil设置静态网站托管的具体步骤,请参见website(静态网站托管及回源配置)

使用REST API

如果您的程序自定义要求较高,您可以直接发起REST API请求。直接发起REST API请求需要手动编写代码计算签名。更多信息,请参见PutBucketWebsite

  • 本页导读 (1)
文档反馈