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

管理对象的访问权限(Python SDK)

最近更新时间2024.02.04 18:30:54

首次发布时间2021.12.31 17:38:36

本文介绍如何获取和设置对象的访问权限。对象的访问权限优先级高于桶的访问权限,如果对象未设置访问权限,则遵循桶的访问权限。

设置对象的读写权限

注意

设置对象的读写权限,您必须具备 tos:PutObjectACL 权限,或具备 WRITE_ACP 对象 ACL 权限,具体操作,请参见权限配置指南

对象 ACL 说明

对象 ACL 权限包含以下类型。

访问权限

描述

访问权限值

READ

允许被授权者读取对象数据及其元数据

tos.PermissionType.Permission_Read

READ_ACP

允许被授权者读取对象 ACL

tos.PermissionType.Permission_Read_Acp

WRITE_ACP

允许被授权者为适用的对象编写 ACL

tos.PermissionType.Permission_Write_Acp

FULL_CONTROL

允许被授权者在对象上的 READ、READ_ACP 和 WRITE_ACP 权限

tos.PermissionType.Permission_Full_Control

您可通过请求体中填写详细的ACL权限信息,或请求头中设置。

请求体中填写详细 ACL 权限信息

import os
import tos

from tos import GranteeType, CannedType, PermissionType
from tos.models2 import Grantee, Grant, Owner

# 从环境变量获取 AK 和 SK 信息。
ak = os.getenv('TOS_ACCESS_KEY')
sk = os.getenv('TOS_SECRET_KEY')
endpoint = "your endpoint"
region = "your region"
bucket_name = 'bucket-test'
object_key = 'object-key'
try:
    client = tos.TosClientV2(ak, sk, endpoint, region)

    # 被授权者为账户ID
    grantee1 = Grantee(GranteeType.Grantee_User, id='account id', display_name='account display name')
    # 被授权者为预定义组中的所有用户
    grantee2 = Grantee(GranteeType.Grantee_Group, canned=CannedType.Canned_All_Users)

    # 向指定账户ID授予Permission_Full_Control权限
    grant_1 = Grant(grantee1, PermissionType.Permission_Full_Control)
    # 向所有用户授予Permission_Read权限
    grant_2 = Grant(grantee2, PermissionType.Permission_Read)

    client.put_object_acl(bucket_name, object_key, grants=[grant_1, grant_2],
                          owner=Owner("object owner id", 'object owner 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))

请求头中设置访问权限

通过 x-tos-acl 请求头设置对象的读写权限有以下六类。

访问权限

描述

访问权限值

私有

私有。对象的所有者拥有所有权限,其他用户没有权限操作该对象。

tos.ACLType.ACL_Private

公共读

公共读。对象的所有者拥有所有权限,其他用户只有该对象的读权限。

tos.ACLType.ACL_Public_Read

认证用户读

对象的所有者拥有所有权限,认证用户拥有该对象的读权限。

tos.ACLType.ACL_Authenticated_Read

桶所有者读

对象所有者拥有所有权限,桶所有者拥有此对象的读权限。

tos.ACLType.ACL_Bucket_Owner_Read

桶所有者具备所有权限

桶所有者和对象所有者都拥有对象的所有操作权限。

tos.ACLType.ACL_Bucket_Owner_Full_Control

以下代码通过 x-tos-acl 设置对象的读写权限。

import os
import tos


# 从环境变量获取 AK 和 SK 信息。
ak = os.getenv('TOS_ACCESS_KEY')
sk = os.getenv('TOS_SECRET_KEY')
endpoint = "your endpoint"
region = "your region"
bucket_name = 'bucket-test'
object_key = 'object-key'
try:
    client = tos.TosClientV2(ak, sk, endpoint, region)
    # 设置对象权限为私有
    client.put_object_acl(bucket_name, object_key, acl=tos.ACLType.ACL_Private)
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))

以下代码通过 x-tos-grant-* 请求设头置对象的读写权限。

import os
import tos


# 从环境变量获取 AK 和 SK 信息。
ak = os.getenv('TOS_ACCESS_KEY')
sk = os.getenv('TOS_SECRET_KEY')
endpoint = "your endpoint"
region = "your region"
bucket_name = 'bucket-test'
object_key = 'object-key'
try:
    client = tos.TosClientV2(ak, sk, endpoint, region)
    # 设置用户ID为1000000001具备FULL_CONTROL控制权, 所有用户具备桶的READ权限
    client.put_object_acl(bucket_name, object_key, grant_full_control='id="1000000001"', grant_read='canned="AllUsers"')
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))

相关文档

关于设置对象的读写权限 API 文档,请参见 PutObjectAcl

获取对象的读写权限

注意

获取对象的访问权限,您必须具备 tos:GetObjectACL 权限,或具备 READ_ACP 的对象 ACL 权限,具体操作,请参见权限配置指南

示例代码

以下代码用于获取 bucket-test 中对象 object-test 的访问权限。

import os
import tos


# 从环境变量获取 AK 和 SK 信息。
ak = os.getenv('TOS_ACCESS_KEY')
sk = os.getenv('TOS_SECRET_KEY')
endpoint = "your endpoint"
region = "your region"
bucket_name = 'bucket-test'
object_key = 'object-key'
try:
    # 创建 TosClientV2 对象,对桶和对象的操作都通过 TosClientV2 实现
    client = tos.TosClientV2(ak, sk, endpoint, region)
    # 获取对象的权限
    out = client.get_object_acl(bucket_name, object_key)
    print('owner_id={}'.format(out.owner.id))
    for grant in out.grants:
        print('permission={}'.format(grant.permission.value))
        print('id={}'.format(grant.grantee.id))
        print('type={}'.format(grant.grantee.type))
        print('canned={}'.format(grant.grantee.canned))
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))

相关文档

关于获取对象的读写权限 API 文档,请参见 GetObjectAcl