You need to enable JavaScript to run this app.
导航

管理网站配置(Python SDK)

最近更新时间2024.02.04 18:31:04

首次发布时间2023.02.02 17:32:41

TOS 支持托管静态网站,您可以将静态网站的所有内容存储至 TOS 存储桶,配置静态网站规则后,即可通过存储桶域名访问该网站。

设置网站配置规则

注意

  • 一个存储桶中只能创建一条静态网站托管规则。
  • 设置静态网站后,必须绑定自定义域名才能生效,具体操作,请参见绑定自定义域名
  • 出于安全合规考虑,从 2022年10月18日开始,如果您使用存储桶的默认域名访问网页类型文件( mimetype 为 text/html,扩展名包括 HTM、HTML、JSP、PLG、HTX、STM),Response Header 中会自动加上 Content-Disposition:attachment,即从浏览器访问网页类型文件时,将不会直接预览网站,而会将网站的内容下载到本地。

示例代码

以下代码用于设置重定向所有的请求到另外一个站点。

import os
import tos

from tos.models2 import RedirectAllRequestsTo

# 从环境变量获取 AK 和 SK 信息。
ak = os.getenv('TOS_ACCESS_KEY')
sk = os.getenv('TOS_SECRET_KEY')
# your endpoint 和 your region 填写Bucket 所在区域对应的Endpoint。# 以华北2(北京)为例,your endpoint 填写 tos-cn-beijing.volces.com,your region 填写 cn-beijing。
endpoint = "your endpoint"
region = "your region"
bucket_name = "bucket-test"

try:
    # 创建 TosClientV2 对象,对桶和对象的操作都通过 TosClientV2 实现
    client = tos.TosClientV2(ak, sk, endpoint, region)
    # 场景1: 重定向所有的请求到另外一个站点
    redirect_all_requests_to = RedirectAllRequestsTo('www.example.com', 'http')
    client.put_bucket_website(bucket_name, redirect_all_requests_to)
except tos.exceptions.TosClientError as e:
    # 操作失败,捕获客户端异常,一般情况为非法请求参数或网络异常
    print('fail with client error, message:{}, cause: {}'.format(e.message, e.cause))
except tos.exceptions.TosServerError as e:
    # 操作失败,捕获服务端异常,可从返回信息中获取详细错误信息
    print('fail with server error, code: {}'.format(e.code))
    # request id 可定位具体问题,强烈建议日志中保存
    print('error with request id: {}'.format(e.request_id))
    print('error with message: {}'.format(e.message))
    print('error with http code: {}'.format(e.status_code))
    print('error with ec: {}'.format(e.ec))
    print('error with request url: {}'.format(e.request_url))
except Exception as e:
    print('fail with unknown error: {}'.format(e))

以下代码用于设定特定的重定向规则来重定向特定的请求。

import os
import tos

from tos import ProtocolType
from tos.models2 import IndexDocument, ErrorDocument, RoutingRule, RoutingRuleCondition, \
    RoutingRuleRedirect, RoutingRules

# 从环境变量获取 AK 和 SK 信息。
ak = os.getenv('TOS_ACCESS_KEY')
sk = os.getenv('TOS_SECRET_KEY')
# your endpoint 和 your region 填写Bucket 所在区域对应的Endpoint。# 以华北2(北京)为例,your endpoint 填写 tos-cn-beijing.volces.com,your region 填写 cn-beijing。
endpoint = "your endpoint"
region = "your region"
bucket_name = "bucket-test"
try:
    # 创建 TosClientV2 对象,对桶和对象的操作都通过 TosClientV2 实现
    client = tos.TosClientV2(ak, sk, endpoint, region)
    # 场景2: 设定特定的重定向规则来重定向特定的请求
    # IndexDocument设置
    index_document = IndexDocument(
        # 对象名为空或以“/”结尾时, 访问该目录下的index.html
        suffix='index.html'
    )
    # ErrorDocument设置
    # 访问无权限的文件时或不存在的文件,指定返回的error文件
    error_document = ErrorDocument('error.html')

    # RoutingRules设置,前缀匹配,获取重定向结果
    rule = RoutingRule(
        condition=RoutingRuleCondition(key_prefix_equals='prefix'),
        redirect=RoutingRuleRedirect(protocol=ProtocolType.Protocol_Http, host_name='www.redirect.com')
    )
    routing_rules = RoutingRules([rule])
    client.put_bucket_website(bucket_name, index_document=index_document, error_document=error_document,
                              routing_rules=routing_rules)
except tos.exceptions.TosClientError as e:
    # 操作失败,捕获客户端异常,一般情况为非法请求参数或网络异常
    print('fail with client error, message:{}, cause: {}'.format(e.message, e.cause))
except tos.exceptions.TosServerError as e:
    # 操作失败,捕获服务端异常,可从返回信息中获取详细错误信息
    print('fail with server error, code: {}'.format(e.code))
    # request id 可定位具体问题,强烈建议日志中保存
    print('error with request id: {}'.format(e.request_id))
    print('error with message: {}'.format(e.message))
    print('error with http code: {}'.format(e.status_code))
    print('error with ec: {}'.format(e.ec))
    print('error with request url: {}'.format(e.request_url))
except Exception as e:
    print('fail with unknown error: {}'.format(e))

获取网站配置规则

注意

要获取桶的网站配置规则,默认您必须为桶所有者。

示例代码

以下代码用于获取桶 bucket-test 的网站配置规则。

import os
import tos

# 从环境变量获取 AK 和 SK 信息。
ak = os.getenv('TOS_ACCESS_KEY')
sk = os.getenv('TOS_SECRET_KEY')
# your endpoint 和 your region 填写Bucket 所在区域对应的Endpoint。# 以华北2(北京)为例,your endpoint 填写 tos-cn-beijing.volces.com,your region 填写 cn-beijing。
endpoint = "your endpoint"
region = "your region"
bucket_name = "bucket-test"

try:
    # 创建 TosClientV2 对象,对桶和对象的操作都通过 TosClientV2 实现
    client = tos.TosClientV2(ak, sk, endpoint, region)
    out = client.get_bucket_website(bucket_name)
    if out.redirect_all_requests_to:
        # 打印 RedirectAllRequestsTo 相关信息
        print('redirect all host name', out.redirect_all_requests_to.host_name)
        print('redirect all protocol', out.redirect_all_requests_to.protocol)
    if out.index_document:
        print('index document suffix', out.index_document.suffix)
        print('index document forbidden sub dir', out.index_document.forbidden_sub_dir)
    if out.error_document:
        print('error document key', out.error_document.key)
    if out.routing_rules:
        for rule in out.routing_rules:
            if rule.condition:
                print('condition by prefix', rule.condition.key_prefix_equals)
                print('condition by http error code', rule.condition.http_error_code_returned_equals)
            if rule.redirect:
                print('redirect host name', rule.redirect.host_name)
                print('redirect host name', rule.redirect.protocol)
                print('redirect code', rule.redirect.http_redirect_code)
                print('redirect replace key with', rule.redirect.replace_key_with)
                print('redirect replace key prefix with', rule.redirect.replace_key_prefix_with)
except tos.exceptions.TosClientError as e:
    # 操作失败,捕获客户端异常,一般情况为非法请求参数或网络异常
    print('fail with client error, message:{}, cause: {}'.format(e.message, e.cause))
except tos.exceptions.TosServerError as e:
    # 操作失败,捕获服务端异常,可从返回信息中获取详细错误信息
    print('fail with server error, code: {}'.format(e.code))
    # request id 可定位具体问题,强烈建议日志中保存
    print('error with request id: {}'.format(e.request_id))
    print('error with message: {}'.format(e.message))
    print('error with http code: {}'.format(e.status_code))
    print('error with ec: {}'.format(e.ec))
    print('error with request url: {}'.format(e.request_url))
except Exception as e:
    print('fail with unknown error: {}'.format(e))

删除网站配置规则

注意

要删除桶的网站配置规则,默认您必须为桶所有者。

示例代码

以下代码用于删除桶 bucket-test 的网站配置规则。

import os
import tos

# 从环境变量获取 AK 和 SK 信息。
ak = os.getenv('TOS_ACCESS_KEY')
sk = os.getenv('TOS_SECRET_KEY')
# your endpoint 和 your region 填写Bucket 所在区域对应的Endpoint。# 以华北2(北京)为例,your endpoint 填写 tos-cn-beijing.volces.com,your region 填写 cn-beijing。
endpoint = "your endpoint"
region = "your region"
bucket_name = "bucket-test"

try:
    # 创建 TosClientV2 对象,对桶和对象的操作都通过 TosClientV2 实现
    client = tos.TosClientV2(ak, sk, endpoint, region)
    client.delete_bucket_website(bucket_name)
except tos.exceptions.TosClientError as e:
    # 操作失败,捕获客户端异常,一般情况为非法请求参数或网络异常
    print('fail with client error, message:{}, cause: {}'.format(e.message, e.cause))
except tos.exceptions.TosServerError as e:
    # 操作失败,捕获服务端异常,可从返回信息中获取详细错误信息
    print('fail with server error, code: {}'.format(e.code))
    # request id 可定位具体问题,强烈建议日志中保存
    print('error with request id: {}'.format(e.request_id))
    print('error with message: {}'.format(e.message))
    print('error with http code: {}'.format(e.status_code))
    print('error with ec: {}'.format(e.ec))
    print('error with request url: {}'.format(e.request_url))
except Exception as e:
    print('fail with unknown error: {}'.format(e))

相关文档

关于设置静态网站配置的更多信息,请参见设置静态网站