zoukankan      html  css  js  c++  java
  • Python

    import oss2
    
    # 连接 oss 
    auth = oss2.Auth(settings.OSS_ACCESS_KEY_ID, settings.OSS_ACCESS_KEY_SECRET)
    bucket = oss2.Bucket(auth, settings.OSS_ENDPOINT, settings.OSS_BUCKET_NAME)
    
    
    def CalculateFolderLength(bucket, folder):
        # 列举所有的文件或目录,但每次循环列举100个,可以通过 max_keys 来调整数目,最大值为 1000
        length = 0
        for obj in oss2.ObjectIterator(bucket, prefix=folder, max_keys=1000):
            length += obj.size
        return length
    
    
    def get_size():
        # 计算所有文件总大小
        size = 0
        for obj in oss2.ObjectIterator(bucket, delimiter='/'):
            if obj.is_prefix():  # 文件夹
                length = CalculateFolderLength(bucket, obj.key)
                size += length
            else:
                size += obj.size
        return size
    
    
    def StrOfSize(size):
        '''
        文件大小单位转换
        递归实现,精确为最大单位值 + 小数点后三位
        '''
        def strofsize(integer, remainder, level):
            if integer >= 1024:
                remainder = integer % 1024
                integer //= 1024
                level += 1
                return strofsize(integer, remainder, level)
            else:
                return integer, remainder, level
    
        units = ['B', 'KB', 'MB', 'GB', 'TB', 'PB']
        integer, remainder, level = strofsize(size, 0, 0)
        if level+1 > len(units):
            level = -1
        return ( '{}.{:>03d} {}'.format(integer, remainder, units[level]) )
    
    
    print(StrOfSize(get_size()))
      
     

    参考文章(大小单位转换):  https://blog.csdn.net/wskzgz/article/details/99293181

  • 相关阅读:
    网页挂马实验
    基于内核网络过滤实验
    基与内核的键盘记录实验
    网络蠕虫病毒代码分析
    脚本病毒编写实验
    病毒查找与清除实验
    木马分析(植入分析)实验
    木马分析(控制分析)实验
    木马分析(隐藏分析)实验
    移动存储型病毒分析实验
  • 原文地址:https://www.cnblogs.com/chaoqi/p/14139085.html
Copyright © 2011-2022 走看看