前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >腾讯云对象存储 COS Python SDK 之列出目录及文件

腾讯云对象存储 COS Python SDK 之列出目录及文件

作者头像
twowinter
发布2020-04-17 11:20:57
1.7K0
发布2020-04-17 11:20:57
举报
文章被收录于专栏:twowinter

前言

腾讯云对象存储(Cloud Object Storage,COS)是腾讯云提供的一种存储海量文件的分布式存储服务,用户可通过网络随时存储和查看数据。

上一篇介绍了如何打开云端文件,这篇介绍如何列出目录及文件。

1 Azure 参考

代码语言:javascript
复制
    def listdir(self, path):
        """
        Lists the contents of the specified path, returning a 2-tuple of lists;
        the first item being directories, the second item being files.
        """
        if path != '' and path.endswith('/'):
            path = path[:-1]
        len_path = len(path)
        func = self.connection.list_blobs
        file_list = func(self.container, path) if len_path else func(self.container)
        files = []
        directories = []
        for blob in file_list:
            remote_file = blob.name[len_path + 1:] if len_path else blob.name
            pos_slash = remote_file.find('/')
            if pos_slash == -1: # files
                files.append(remote_file)
            else: #directory
                dir_name = remote_file[:pos_slash]
                if dir_name not in directories:
                    directories.append(remote_file[:pos_slash])
        return (directories, files)

目的是列出一个目录下的所有目录和文件,不包含递归子目录。

2 调试

list_objects 返回格式如下,可见是一个字典,我们需要取到文件列表。

代码语言:javascript
复制
{'Name': 'test-bucket-appid', 'EncodingType': 'url', 'Prefix': None, 'Marker': None, 'MaxKeys': '5', 'IsTruncated': 'true', 'NextMarker': 'sss-2019-06-27-124501.dump', 'Contents': [{'Key': 'sss-2019-06-27-124101.dump', 'LastModified': '2019-06-27T12:41:03.000Z', 'ETag': '"cc4a9da19c451eb1fcce03223b6011e3"', 'Size': '130050234', 'Owner': {'ID': '1234567890', 'DisplayName': '1234567890'}, 'StorageClass': 'STANDARD'}, {'Key': 'sss-2019-06-27-124201.dump', 'LastModified': '2019-06-27T12:42:02.000Z', 'ETag': '"81e10a320bebdd38489e4a1adee01984"', 'Size': '130052965', 'Owner': {'ID': '1234567890', 'DisplayName': '1234567890'}, 'StorageClass': 'STANDARD'}, {'Key': 'sss-2019-06-27-124301.dump', 'LastModified': '2019-06-27T12:43:02.000Z', 'ETag': '"9429409fffc0523ea7be6c3bc416855b"', 'Size': '130055800', 'Owner': {'ID': '1234567890', 'DisplayName': '1234567890'}, 'StorageClass': 'STANDARD'}, {'Key': 'sss-2019-06-27-124401.dump', 'LastModified': '2019-06-27T12:44:02.000Z', 'ETag': '"d178d941aaa4d051bbee532e73628935"', 'Size': '130058644', 'Owner': {'ID': '1234567890', 'DisplayName': '1234567890'}, 'StorageClass': 'STANDARD'}, {'Key': 'sss-2019-06-27-124501.dump', 'LastModified': '2019-06-27T12:45:02.000Z', 'ETag': '"01f3828dd6768fa1fc63cbc0c637d85c"', 'Size': '130061487', 'Owner': {'ID': '1234567890', 'DisplayName': '1234567890'}, 'StorageClass': 'STANDARD'}]}

针对 Contents 列表,单个项目字典里关于文件名的键是‘Key’,取之。

3 移植结果

代码语言:javascript
复制
    def listdir(self, path):
        """
        Lists the contents of the specified path, returning a 2-tuple of lists;
        the first item being directories, the second item being files.
        """
        if path != '' and path.endswith('/'):
            path = path[:-1]
        len_path = len(path)
        func = self.client.list_objects
        file_list = func(Bucket=self.bucket, Prefix=path)['Contents'] if len_path else func(Bucket=self.bucket)['Contents']
        files = []
        directories = []
        for blob in file_list:
            remote_file = blob['Key'][len_path + 1:] if len_path else blob['Key']
            pos_slash = remote_file.find('/')
            if pos_slash == -1: # files
                files.append(remote_file)
            else: #directory
                dir_name = remote_file[:pos_slash]
                if dir_name not in directories:
                    directories.append(remote_file[:pos_slash])
        return (directories, files)

END

本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 前言
  • 1 Azure 参考
  • 2 调试
  • 3 移植结果
  • END
相关产品与服务
对象存储
对象存储(Cloud Object Storage,COS)是由腾讯云推出的无目录层次结构、无数据格式限制,可容纳海量数据且支持 HTTP/HTTPS 协议访问的分布式存储服务。腾讯云 COS 的存储桶空间无容量上限,无需分区管理,适用于 CDN 数据分发、数据万象处理或大数据计算与分析的数据湖等多种场景。
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档