You need to enable JavaScript to run this app.
文档中心
对象存储

对象存储

复制全文
下载 pdf
管理桶配置
管理镜像回源(Python SDK)
复制全文
下载 pdf
管理镜像回源(Python SDK)

镜像回源主要用于数据无缝迁移到 TOS 的场景。例如 Web 服务已经在用户自建的源站上运行,用户将静态网页迁移到 TOS中 存储,可配置镜像回源规则获取未迁移至 TOS 中的部分数据。

设置镜像回源规则

注意

  • 要为桶设置镜像回源规则,默认您必须为桶所有者。
  • 一个存储桶仅支持创建一条回源规则。
  • 镜像回源最大能够支持 5GiB 的对象。如果需要请求超过 5GiB 的对象,请使用 Get Range 的功能。
  • 重定向回源最大能够支持 300GiB。

示例代码

以下代码用于设置指定桶 bucket-test 的镜像回源规则。

import os
import tos
from tos import RedirectType
from tos.models2 import Rule, Condition, Redirect, PublicSource, SourceEndpoint, MirrorHeader
# 从环境变量获取 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)

    # 指定镜像回源携带的header
    mirror_header = MirrorHeader(pass_all=True, pass_headers=['pass_header1', 'pass_header2'],
                                 remove=['remove_header1', 'remove_header2'])
    # 设置回源条件
    condition = Condition(http_code=404)
    # 设置后执行的动作
    redirect = Redirect(redirect_type=RedirectType.Mirror,
                        # fetch_source_on_redirect=True 时,表明重定向后是否去配置的源站拉取信息
                        fetch_source_on_redirect=True,
                        # 配置公共可访问的源端配置
                        public_source=PublicSource(SourceEndpoint(primary=['http://test.com/obj/tostest/'])),
                        # pass_query=True 执行跳转或者镜像回源规则时,携带请求参数
                        pass_query=True,
                        # follow_redirect=True 镜像回源获取的结果为 3xx 继续跳转到指定的Location 获取数据
                        follow_redirect=True,
                        # 配置回源携带 header
                        mirror_header=mirror_header)

    rule = Rule(id='1', condition=condition, redirect=redirect)
    client.put_bucket_mirror_back(bucket_name, [rule])
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)

    rules = client.get_bucket_mirror_back(bucket_name)
    for rule in rules.rules:
        print('id', rule.id)
        print('condition_http_code', rule.condition.http_code)
        print('redirect_redirect_type', rule.redirect.redirect_type.value)
        print('redirect_follow_redirect', rule.redirect.follow_redirect)
        print('redirect_fetch_source_on_redirect', rule.redirect.fetch_source_on_redirect)
        print('source_endpoint', rule.redirect.public_source.source_endpoint.primary)
        print('redirect_pass_query', rule.redirect.pass_query)
        print('pass_all', rule.redirect.mirror_header.pass_all)
        if rule.redirect.mirror_header.pass_headers is not None:
            for header in rule.redirect.mirror_header.pass_headers:
                print('pass_header', header)
        if rule.redirect.mirror_header.remove is not None:
            for header in rule.redirect.mirror_header.remove:
                print('remove_header', header)
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_mirror_back(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))

相关文档

关于镜像回源的更多信息,请参见设置回源规则

最近更新时间:2024.02.04 18:31:03
这个页面对您有帮助吗?
有用
有用
无用
无用