TOS 中只有对象的概念,内部使用扁平结构存储数据,为方便您对对象进行分组并简化权限管理,您可以使用目录层次来组织对象。
TOS 只有对象的概念, 可通过创建一个大小为 0 并且以斜线 /
结尾的对象, 模拟目录的功能。
以下代码用于桶 bucket-test
创建目录 exampledir/
和子目录 exampledir/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-test" try: # 创建 TosClientV2 对象,对桶和对象的操作都通过 TosClientV2 实现 client = tos.TosClientV2(ak, sk, endpoint, region) # 创建bucket client.create_bucket(bucket_name) # 填写目录名称,目录必须以正斜杠结尾 client.put_object(bucket_name, 'exampledir/') # 填写目录名称,创建多级目录,目录必须以正斜杠结尾 client.put_object(bucket_name, 'exampledir/test/') 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))
通过 delimiter
和 prefix
两个参数可以模拟目录的功能:
delimiter
为 /
同时设置 prefix
为空, 可返回根目录下的对象和子目录信息。delimiter
为 /
同时设置 prefix
为子目录, 可返回子目录的对象和次级目录。以下代码用于列举桶 bucket-test
目录 log/
下的对象和子目录。
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-test" try: client = tos.TosClientV2(ak, sk, endpoint, region) is_truncated = True next_continuation_token = '' while is_truncated: out = client.list_objects_type2(bucket_name, delimiter="/", prefix="log/", continuation_token=next_continuation_token) is_truncated = out.is_truncated next_continuation_token = out.next_continuation_token # common_prefixes中返回了根目录下的子目录 for prefix in out.common_prefixes: print('subDir', prefix.prefix) # contents中返回了根目录下的对象 for content in out.contents: print('file', content.key) 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
中目录 log/
的所有数据。
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-test" try: # 创建 TosClientV2 对象,对桶和对象的操作都通过 TosClientV2 实现 client = tos.TosClientV2(ak, sk, endpoint, region) is_truncated = True continuation_token = '' # 以下代码用于删除桶中指定目录 while is_truncated: out = client.list_objects_type2(bucket_name, continuation_token=continuation_token, prefix='log/') for content in out.contents: client.delete_object(bucket_name, content.key) continuation_token = out.next_continuation_token is_truncated = out.is_truncated 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))