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

前言

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

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

本文作者twowinter,转载请注明作者:http://blog.csdn.net/iotisan/

1 Azure 参考

    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 返回格式如下,可见是一个字典,我们需要取到文件列表。

{'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 移植结果

    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


评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值