From b5c6acde12bbe2cc43a428ed6a453ab453aa47bd Mon Sep 17 00:00:00 2001 From: kfang Date: Mon, 12 Jul 2021 12:22:39 +0800 Subject: [PATCH 1/2] =?UTF-8?q?=E4=B8=BABucketManager=E7=B1=BB=E6=96=B0?= =?UTF-8?q?=E5=A2=9Ebucket=5Fusedspace=E5=92=8Cbucket=5Ffilecount=E6=96=B9?= =?UTF-8?q?=E6=B3=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- qiniu/services/storage/bucket.py | 58 ++++++++++++++++++++++++++++++++ 1 file changed, 58 insertions(+) diff --git a/qiniu/services/storage/bucket.py b/qiniu/services/storage/bucket.py index 2f54260a..720dd0e0 100644 --- a/qiniu/services/storage/bucket.py +++ b/qiniu/services/storage/bucket.py @@ -3,6 +3,7 @@ from qiniu import config from qiniu import http from qiniu.utils import urlsafe_base64_encode, entry +from datetime import datetime, timedelta class BucketManager(object): @@ -359,6 +360,63 @@ def bucket_domain(self, bucket_name): url = "{0}/v6/domain/list?tbl={1}".format(config.get_default("default_api_host"), bucket_name) return self.__get(url, options) + def bucket_usedspace(self, bucket_name, begin_time=None, end_time=None, g=None): + """ + 获取bucket的存储空间大小.可查询当天计量,统计延迟大概 5 分钟。 + Args: + bucket_name: 存储空间名 + begin_time: 起始日期字符串,闭区间,例如: 20060102150405 + end_time: 结束日期字符串,开区间,例如: 20060102150405 + g: 时间粒度,支持 day;当天支持5min、hour、day + """ + url = "{0}/v6/space".format(config.get_default("default_api_host")) + now_time = now = datetime.now() + hour_ago = now + timedelta(hours=-1) + + if begin_time is None: + begin_time=datetime.strftime(hour_ago, '%Y%m%d%H%M%S') + if end_time is None: + end_time=datetime.strftime(now, '%Y%m%d%H%M%S') + if g is None: + g="hour" + params = { + "bucket": bucket_name, + "begin": begin_time, + "end": end_time, + "g": g + } + return self.__get(url, params=params) + + def bucket_filecount(self, bucket_name, begin_time=None, end_time=None, g=None): + """ + 获取bucket的存储空间文件数量.可查询当天计量,统计延迟大概 5 分钟。 + Args: + bucket_name: 存储空间名 + begin_time: 起始日期字符串,闭区间,例如: 20060102150405 + end_time: 结束日期字符串,开区间,例如: 20060102150405 + g: 时间粒度,支持 day;当天支持5min、hour、day + """ + url = "{0}/v6/count".format(config.get_default("default_api_host")) + + now_time = now = datetime.now() + hour_ago = now + timedelta(hours=-1) + + if begin_time is None: + begin_time=datetime.strftime(hour_ago, '%Y%m%d%H%M%S') + if end_time is None: + end_time=datetime.strftime(now, '%Y%m%d%H%M%S') + if g is None: + g="hour" + + params = { + "bucket": bucket_name, + "begin": begin_time, + "end": end_time, + "g": g + } + + return self.__get(url, params=params) + def change_bucket_permission(self, bucket_name, private): """ 设置 存储空间访问权限 From d1187119f0284c05096d4455b6a0c7e538f68b8a Mon Sep 17 00:00:00 2001 From: kfang Date: Mon, 12 Jul 2021 12:26:52 +0800 Subject: [PATCH 2/2] delete unnecessary variable --- qiniu/services/storage/bucket.py | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/qiniu/services/storage/bucket.py b/qiniu/services/storage/bucket.py index 720dd0e0..3ddfde98 100644 --- a/qiniu/services/storage/bucket.py +++ b/qiniu/services/storage/bucket.py @@ -370,13 +370,13 @@ def bucket_usedspace(self, bucket_name, begin_time=None, end_time=None, g=None): g: 时间粒度,支持 day;当天支持5min、hour、day """ url = "{0}/v6/space".format(config.get_default("default_api_host")) - now_time = now = datetime.now() - hour_ago = now + timedelta(hours=-1) + now_time = datetime.now() + hour_ago = now_time + timedelta(hours=-1) if begin_time is None: begin_time=datetime.strftime(hour_ago, '%Y%m%d%H%M%S') if end_time is None: - end_time=datetime.strftime(now, '%Y%m%d%H%M%S') + end_time=datetime.strftime(now_time, '%Y%m%d%H%M%S') if g is None: g="hour" params = { @@ -398,13 +398,13 @@ def bucket_filecount(self, bucket_name, begin_time=None, end_time=None, g=None): """ url = "{0}/v6/count".format(config.get_default("default_api_host")) - now_time = now = datetime.now() - hour_ago = now + timedelta(hours=-1) + now_time = datetime.now() + hour_ago = now_time + timedelta(hours=-1) if begin_time is None: begin_time=datetime.strftime(hour_ago, '%Y%m%d%H%M%S') if end_time is None: - end_time=datetime.strftime(now, '%Y%m%d%H%M%S') + end_time=datetime.strftime(now_time, '%Y%m%d%H%M%S') if g is None: g="hour"