根据Google云存储中的存储类别获取存储桶的大小

时间:2018-11-23 06:27:34

标签: google-cloud-platform google-cloud-storage

我想根据存储类获取存储桶的大小。我已在存储桶中添加了规则,以根据文件的使用期限更改文件的存储类。 我使用了以下命令

gsutil du -sh gs://[bucket-name] 

To get Meta-data : 
gsutil ls -L gs://[bucket-name] 

To set ACL to bucket 

gsutil lifecycle set life-cycle.json gs://[bucket-name] 

请对此提供任何帮助以解决我的问题

1 个答案:

答案 0 :(得分:2)

编辑:

我已经在Public Issue Tracker上为此提交了功能请求。同时,可以使用下面的代码。

我相信没有gsutil命令可以按存储类别显示GCS存储桶的总大小。

但是,我使用Cloud Storage Client Libraries for Python,编写了一个脚本,该脚本可以满足您的要求:

from google.cloud import storage
import math

### SET THESE VARIABLES ###
PROJECT_ID = "" 
CLOUD_STORAGE_BUCKET = "" 
###########################

def _get_storage_client():
    return storage.Client(
        project=PROJECT_ID)

def convert_size(size_bytes):
   if size_bytes == 0:
       return "0 B"
   size_name = ("B", "KB", "MB", "GB", "TB", "PB", "EB", "ZB", "YB")
   i = int(math.floor(math.log(size_bytes, 1024)))
   p = math.pow(1024, i)
   s = round(size_bytes / p, 2)
   return "%s %s" % (s, size_name[i])

def size_by_class():
    client = _get_storage_client()
    bucket = client.bucket(CLOUD_STORAGE_BUCKET)
    blobs = bucket.list_blobs()

    size_multi_regional = size_regional = size_nearline = size_coldline = 0
    for blob in blobs:
        if blob.storage_class == "MULTI_REGIONAL":
            size_multi_regional = size_multi_regional + blob.size
        if blob.storage_class == "REGIONAL":
            size_regional = size_regional + blob.size
        if blob.storage_class == "NEARLINE":
            size_nearline = size_nearline + blob.size
        if blob.storage_class == "COLDLINE":
            size_coldline = size_coldline + blob.size

    print("MULTI_REGIONAL: "+str(convert_size(size_multi_regional))+"\n"+
            "REGIONAL: "+str(convert_size(size_regional)+"\n"+
            "NEARLINE: "+str(convert_size(size_nearline))+"\n"+
            "COLDLINE: "+str(convert_size(size_coldline))
            ))

if __name__ == '__main__':
    size_by_class()

要从Google Cloud Shell运行此程序,请确保以前安装了Client Library for Python并带有:

pip install --upgrade google-cloud-storage

并且为了向应用程序代码提供身份验证凭据,您必须将环境变量GOOGLE_APPLICATION_CREDENTIALS指向包含service account密钥的JSON文件的位置:

export `GOOGLE_APPLICATION_CREDENTIALS`="/home/user/Downloads/[FILE_NAME].json"

在运行脚本之前,将PROJECT_ID设置为项目的ID,并将CLOUD_STORAGE_BUCKET设置为GCS Bucket的名称。

使用python main.py运行脚本。输出应类似于:

MULTI_REGIONAL: 1.0 GB
REGIONAL: 300 MB
NEARLINE: 200 MB
COLDLINE: 10 MB