文档

put-bucket-website

更新时间:

put-bucket-website用于将存储空间(Bucket)设置为静态网站托管模式并设置跳转规则。

注意事项

  • 阿里云账号默认拥有为Bucket设置静态网站托管模式和跳转规则的权限。如果您需要通过RAM用户或者STS的方式进行查看,您必须拥有oss:PutBucketWebsite权限。具体操作,请参见为RAM用户授权自定义的权限策略

  • 在设置静态网站之前,请确保您已了解该功能。详情请参见静态网站托管概述

命令格式

ossutil api put-bucket-website --bucket value --website-configuration value [flags]

参数

类型

说明

--bucket

string

Bucket的名称。

--website-configuration

string

网站根节点。

说明

put-bucket-website命令对应API接口PutBucketWebsite。关于API中的具体参数含义,请参见PutBucketWebsite

--website-configuration

--website-configuration配置选项既支持XML语法也支持JSON语法,当选项值包含file://前缀时,表示从文件中读取配置。

  • XML语法:

    <WebsiteConfiguration>
      <IndexDocument>
        <Suffix>string</Suffix>
        <SupportSubDir>boolean</SupportSubDir>
        <Type>string</Type>
      </IndexDocument>
      <ErrorDocument>
        <Key>string</Key>
        <HttpStatus>string</HttpStatus>
      </ErrorDocument>
      <RoutingRules>
        <RoutingRule>
          <RuleNumber>integer</RuleNumber>
          <Condition>
            <KeyPrefixEquals>string</KeyPrefixEquals>
            <HttpErrorCodeReturnedEquals>integer</HttpErrorCodeReturnedEquals>
          </Condition>
          <Redirect>
            <HttpRedirectCode>integer</HttpRedirectCode>
            <RedirectType>string</RedirectType>
            <MirrorPassQueryString>boolean</MirrorPassQueryString>
            <MirrorFollowRedirect>boolean</MirrorFollowRedirect>
            <MirrorHeaders>
              <PassAll>boolean</PassAll>
              <Pass>string</Pass>
              ...
              <Remove>string</Remove>
              ...
              <Set>
                <Value>string</Value>
                <Key>string</Key>
              </Set>
              ...
            </MirrorHeaders>
            <MirrorURL>string</MirrorURL>
            <EnableReplacePrefix>boolean</EnableReplacePrefix>
            <ReplaceKeyWith>string</ReplaceKeyWith>
            <PassQueryString>boolean</PassQueryString>
            <MirrorCheckMd5>boolean</MirrorCheckMd5>
            <Protocol>string</Protocol>
            <HostName>string</HostName>
            <TransparentMirrorResponseCodes>string</TransparentMirrorResponseCodes>
            <ReplaceKeyPrefixWith>string</ReplaceKeyPrefixWith>
          </Redirect>
        </RoutingRule>
        ...
      </RoutingRules>
    </WebsiteConfiguration>
  • JSON语法:

    {
      "IndexDocument": {
        "Type": "string",
        "Suffix": "string",
        "SupportSubDir": boolean
      },
      "ErrorDocument": {
        "Key": "string",
        "HttpStatus": "string"
      },
      "RoutingRules": {
        "RoutingRule": [
          {
            "RuleNumber": integer,
            "Condition": {
              "HttpErrorCodeReturnedEquals": integer,
              "KeyPrefixEquals": "string"
            },
            "Redirect": {
              "MirrorURL": "string",
              "EnableReplacePrefix": boolean,
              "ReplaceKeyWith": "string",
              "PassQueryString": boolean,
              "MirrorCheckMd5": boolean,
              "Protocol": "string",
              "HostName": "string",
              "TransparentMirrorResponseCodes": "string",
              "ReplaceKeyPrefixWith": "string",
              "HttpRedirectCode": integer,
              "RedirectType": "string",
              "MirrorPassQueryString": boolean,
              "MirrorFollowRedirect": boolean,
              "MirrorHeaders": {
                "PassAll": boolean,
                "Pass": ["string", ...],
                "Remove": ["string", ...],
                "Set": [
                  {
                    "Key": "string",
                    "Value": "string"
                  },
                  ...
                ]
              }
            }
          },
          ...
        ]
      }
    }
说明

关于支持的全局命令行选项,请参见支持的全局命令行选项

使用示例

以下示例展示了如何为名为examplebucket的静态网站托管模式并设置跳转规则。

  • 使用XML配置文件,website-configuration.xml内容如下:

    <?xml version="1.0" encoding="UTF-8"?>
    <WebsiteConfiguration>
      <IndexDocument>
        <Suffix>index.html</Suffix>
        <SupportSubDir>true</SupportSubDir>
        <Type>0</Type>
      </IndexDocument>
      <ErrorDocument>
        <Key>error.html</Key>
        <HttpStatus>404</HttpStatus>
      </ErrorDocument>
      <RoutingRules>
        <RoutingRule>
          <RuleNumber>1</RuleNumber>
          <Condition>
            <KeyPrefixEquals>abc/</KeyPrefixEquals>
            <HttpErrorCodeReturnedEquals>404</HttpErrorCodeReturnedEquals>
          </Condition>
          <Redirect>
            <RedirectType>Mirror</RedirectType>
            <PassQueryString>true</PassQueryString>
            <MirrorURL>http://example.com/</MirrorURL>   
            <MirrorPassQueryString>true</MirrorPassQueryString>
            <MirrorFollowRedirect>true</MirrorFollowRedirect>
            <MirrorCheckMd5>false</MirrorCheckMd5>
            <MirrorHeaders>
              <PassAll>true</PassAll>
              <Pass>myheader-key1</Pass>
              <Pass>myheader-key2</Pass>
              <Remove>myheader-key3</Remove>
              <Remove>myheader-key4</Remove>
              <Set>
                <Key>myheader-key5</Key>
                <Value>myheader-value5</Value>
              </Set>
            </MirrorHeaders>
          </Redirect>
        </RoutingRule>
        <RoutingRule>
          <RuleNumber>2</RuleNumber>
          <Condition>
            <KeyPrefixEquals>abc/</KeyPrefixEquals>
            <HttpErrorCodeReturnedEquals>404</HttpErrorCodeReturnedEquals>
            <IncludeHeader>
              <Key>host</Key>
              <Equals>test.oss-cn-beijing-internal.aliyuncs.com</Equals>
            </IncludeHeader>
          </Condition>
          <Redirect>
            <RedirectType>AliCDN</RedirectType>
            <Protocol>http</Protocol>
            <HostName>example.com</HostName>
            <PassQueryString>false</PassQueryString>
            <ReplaceKeyWith>prefix/${key}.suffix</ReplaceKeyWith>
            <HttpRedirectCode>301</HttpRedirectCode>
          </Redirect>
        </RoutingRule>
        <RoutingRule>
          <Condition>
            <HttpErrorCodeReturnedEquals>404</HttpErrorCodeReturnedEquals>
          </Condition>
          <RuleNumber>3</RuleNumber>
          <Redirect>
            <ReplaceKeyWith>prefix/${key}</ReplaceKeyWith>
            <HttpRedirectCode>302</HttpRedirectCode>
            <EnableReplacePrefix>false</EnableReplacePrefix>
            <PassQueryString>false</PassQueryString>
            <Protocol>http</Protocol>
            <HostName>example.com</HostName>
            <RedirectType>External</RedirectType>
          </Redirect>
        </RoutingRule>
      </RoutingRules>
    </WebsiteConfiguration>

命令示例如下:

ossutil api put-bucket-website --bucket examplebucket --website-configuration file://website-configuration.xml
  • 使用JSON配置文件,website-configuration.json内容如下:

    {
        "IndexDocument": {
        "Suffix": "index.html",
        "SupportSubDir": "true",
        "Type": "0"
        },
        "ErrorDocument": {
        "Key": "error.html",
        "HttpStatus": "404"
        },
        "RoutingRules": {
        "RoutingRule": [
        {
        "RuleNumber": "1",
        "Condition": {
        "KeyPrefixEquals": "abc/",
        "HttpErrorCodeReturnedEquals": "404"
        },
        "Redirect": {
        "RedirectType": "Mirror",
        "PassQueryString": "true",
        "MirrorURL": "http://example.com/",
        "MirrorPassQueryString": "true",
        "MirrorFollowRedirect": "true",
        "MirrorCheckMd5": "false",
        "MirrorHeaders": {
        "PassAll": "true",
        "Pass": [
        "myheader-key1",
        "myheader-key2"
        ],
        "Remove": [
        "myheader-key3",
        "myheader-key4"
        ],
        "Set": {
        "Key": "myheader-key5",
        "Value": "myheader-value5"
        }
        }
        }
        },
        {
        "RuleNumber": "2",
        "Condition": {
        "KeyPrefixEquals": "abc/",
        "HttpErrorCodeReturnedEquals": "404",
        "IncludeHeader": {
        "Key": "host",
        "Equals": "test.oss-cn-beijing-internal.aliyuncs.com"
        }
        },
        "Redirect": {
        "RedirectType": "AliCDN",
        "Protocol": "http",
        "HostName": "example.com",
        "PassQueryString": "false",
        "ReplaceKeyWith": "prefix/${key}.suffix",
        "HttpRedirectCode": "301"
        }
        },
        {
        "Condition": {
        "HttpErrorCodeReturnedEquals": "404"
        },
        "RuleNumber": "3",
        "Redirect": {
        "ReplaceKeyWith": "prefix/${key}",
        "HttpRedirectCode": "302",
        "EnableReplacePrefix": "false",
        "PassQueryString": "false",
        "Protocol": "http",
        "HostName": "example.com",
        "RedirectType": "External"
        }
        }
        ]
        }
    }
    

    命令示例如下:

    ossutil api put-bucket-website --bucket examplebucket --website-configuration file://website-configuration.json
  • 使用JSON配置参数,命令示例如下:

    ossutil api put-bucket-website --bucket examplebucket --website-configuration "{\"IndexDocument\":{\"Suffix\":\"index.html\",\"SupportSubDir\":\"true\",\"Type\":\"0\"},\"ErrorDocument\":{\"Key\":\"error.html\",\"HttpStatus\":\"404\"},\"RoutingRules\":{\"RoutingRule\":[{\"RuleNumber\":\"1\",\"Condition\":{\"KeyPrefixEquals\":\"abc/\",\"HttpErrorCodeReturnedEquals\":\"404\"},\"Redirect\":{\"RedirectType\":\"Mirror\",\"PassQueryString\":\"true\",\"MirrorURL\":\"http://example.com/\",\"MirrorPassQueryString\":\"true\",\"MirrorFollowRedirect\":\"true\",\"MirrorCheckMd5\":\"false\",\"MirrorHeaders\":{\"PassAll\":\"true\",\"Pass\":[\"myheader-key1\",\"myheader-key2\"],\"Remove\":[\"myheader-key3\",\"myheader-key4\"],\"Set\":{\"Key\":\"myheader-key5\",\"Value\":\"myheader-value5\"}}}},{\"RuleNumber\":\"2\",\"Condition\":{\"KeyPrefixEquals\":\"abc/\",\"HttpErrorCodeReturnedEquals\":\"404\",\"IncludeHeader\":{\"Key\":\"host\",\"Equals\":\"test.oss-cn-beijing-internal.aliyuncs.com\"}},\"Redirect\":{\"RedirectType\":\"AliCDN\",\"Protocol\":\"http\",\"HostName\":\"example.com\",\"PassQueryString\":\"false\",\"ReplaceKeyWith\":\"prefix/${key}.suffix\",\"HttpRedirectCode\":\"301\"}},{\"Condition\":{\"HttpErrorCodeReturnedEquals\":\"404\"},\"RuleNumber\":\"3\",\"Redirect\":{\"ReplaceKeyWith\":\"prefix/${key}\",\"HttpRedirectCode\":\"302\",\"EnableReplacePrefix\":\"false\",\"PassQueryString\":\"false\",\"Protocol\":\"http\",\"HostName\":\"example.com\",\"RedirectType\":\"External\"}}]}}"