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

对象存储

复制全文
下载 pdf
管理桶配置
管理实时日志(Python SDK)
复制全文
下载 pdf
管理实时日志(Python SDK)

TOS 支持日志分析功能,支持通过日志服务,检索分析您访问 TOS 过程中产生的访问日志。通过 TOS Python SDK 您可以设置日志分析功能的相关配置。

设置实时日志配置规则

注意

  • 使用日志分析功能需要您已开通日志服务功能,并已授权 TOS 访问火山引擎日志服务 TLS。
  • 开启日志分析功能后,日志服务会自动创建日志项目及主题存放 TOS 的相关日志。

示例代码

以下代码用于设置桶 bucket-test 的实时日志配置规则。

import os
import tos

from tos.models2 import RealTimeLogConfiguration, AccessLogConfiguration

# 从环境变量获取 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)
    config = RealTimeLogConfiguration(
        # 角色名
        role='TOSLogTLSRole',
        # 设置为 true 以使用服务端提供的日志 topic
        configuration=AccessLogConfiguration(use_service_topic=True)
    )
    client.put_bucket_real_time_log(bucket_name, config)

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_real_time_log(bucket_name)
    print('role', out.configuration.role)
    print('tls topic id', out.configuration.configuration.tls_topic_id)
    print('tls project id', out.configuration.configuration.tls_project_id)
    print('use service topic', out.configuration.configuration.use_service_topic)

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_real_time_log(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:04
这个页面对您有帮助吗?
有用
有用
无用
无用