Skip to content

Commit 81d77ed

Browse files
authored
Merge pull request #431 from qiniu/fix/list-domains
update list_domains and get_bucket_hosts
2 parents 07dcb0d + d19ecd8 commit 81d77ed

File tree

5 files changed

+61
-40
lines changed

5 files changed

+61
-40
lines changed

.github/workflows/ci-test.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ jobs:
2121
- name: Install dependencies
2222
run: |
2323
python -m pip install --upgrade pip
24-
pip install flake8 pytest pytest-cov freezegun requests scrutinizer-ocular codecov
24+
pip install "coverage<7.2" flake8 pytest pytest-cov freezegun requests scrutinizer-ocular codecov
2525
- name: Run cases
2626
env:
2727
QINIU_ACCESS_KEY: ${{ secrets.QINIU_ACCESS_KEY }}

CHANGELOG.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,12 @@
11
# Changelog
22

3+
## Next Version
4+
* 对象存储,更新 api 默认域名
5+
* 对象存储,新增 api 域名的配置与获取
6+
* 对象存储,修复获取区域域名后无法按照预期进行过期处理
7+
* 对象存储,更新获取区域域名的接口
8+
* 对象存储,bucket_domains 修改为 list_domains 的别名
9+
310
## 7.10.0(2022-11-15)
411
* 对象存储,修复通过 set_default 设置 rs, rsf 不生效,而 SDK 自动获取的问题(v7.9.0)
512
* 对象存储,支持直接从 qiniu 导入 UploadProgressRecorder

qiniu/config.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
RS_HOST = 'http://rs.qiniu.com' # 管理操作Host
66
RSF_HOST = 'http://rsf.qbox.me' # 列举操作Host
7-
API_HOST = 'http://api.qiniu.com' # 数据处理操作Host
7+
API_HOST = 'http://api.qiniuapi.com' # 数据处理操作Host
88
UC_HOST = 'https://uc.qbox.me' # 获取空间信息Host
99

1010
_BLOCK_SIZE = 1024 * 1024 * 4 # 断点续传分块大小,该参数为接口规格,暂不支持修改

qiniu/region.py

Lines changed: 45 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -23,13 +23,15 @@ def __init__(
2323
home_dir=None,
2424
scheme="http",
2525
rs_host=None,
26-
rsf_host=None):
26+
rsf_host=None,
27+
api_host=None):
2728
"""初始化Zone类"""
2829
self.up_host = up_host
2930
self.up_host_backup = up_host_backup
3031
self.io_host = io_host
3132
self.rs_host = rs_host
3233
self.rsf_host = rsf_host
34+
self.api_host = api_host
3335
self.home_dir = home_dir
3436
self.host_cache = host_cache
3537
self.scheme = scheme
@@ -91,6 +93,20 @@ def get_rsf_host(self, ak, bucket, home_dir=None):
9193
rsf_hosts = bucket_hosts['rsfHosts']
9294
return rsf_hosts[0]
9395

96+
def get_api_host(self, ak, bucket, home_dir=None):
97+
from .config import get_default, is_customized_default
98+
if self.api_host:
99+
return self.api_host
100+
if is_customized_default('default_api_host'):
101+
return get_default('default_api_host')
102+
if home_dir is None:
103+
home_dir = os.getcwd()
104+
bucket_hosts = self.get_bucket_hosts(ak, bucket, home_dir)
105+
if 'apiHosts' not in bucket_hosts:
106+
bucket_hosts = self.get_bucket_hosts(ak, bucket, home_dir, force=True)
107+
api_hosts = bucket_hosts['apiHosts']
108+
return api_hosts[0]
109+
94110
def get_up_host(self, ak, bucket, home_dir):
95111
bucket_hosts = self.get_bucket_hosts(ak, bucket, home_dir)
96112
if 'upHosts' not in bucket_hosts:
@@ -123,39 +139,38 @@ def get_bucket_hosts(self, ak, bucket, home_dir, force=False):
123139
if not force and len(bucket_hosts) > 0:
124140
return bucket_hosts
125141

126-
hosts = {}
127-
hosts.update({self.scheme: {}})
128-
129-
hosts[self.scheme].update({'up': []})
130-
hosts[self.scheme].update({'io': []})
142+
hosts = compat.json.loads(self.bucket_hosts(ak, bucket)).get('hosts', [])
131143

132-
if self.up_host is not None:
133-
hosts[self.scheme]['up'].append(self.scheme + "://" + self.up_host)
144+
if type(hosts) is not list or len(hosts) == 0:
145+
raise KeyError("Please check your BUCKET_NAME! Server hosts not correct! The hosts is %s" % hosts)
134146

135-
if self.up_host_backup is not None:
136-
hosts[self.scheme]['up'].append(
137-
self.scheme + "://" + self.up_host_backup)
147+
region = hosts[0]
138148

139-
if self.io_host is not None:
140-
hosts[self.scheme]['io'].append(self.scheme + "://" + self.io_host)
149+
default_ttl = 24 * 3600 # 1 day
150+
region['ttl'] = region.get('ttl', default_ttl)
141151

142-
if len(hosts[self.scheme]) == 0 or self.io_host is None:
143-
hosts = compat.json.loads(self.bucket_hosts(ak, bucket))
144-
else:
145-
# 1 year
146-
hosts['ttl'] = int(time.time()) + 31536000
147-
try:
148-
scheme_hosts = hosts[self.scheme]
149-
except KeyError:
150-
raise KeyError(
151-
"Please check your BUCKET_NAME! The UpHosts is %s" %
152-
hosts)
153152
bucket_hosts = {
154-
'upHosts': scheme_hosts['up'],
155-
'ioHosts': scheme_hosts['io'],
156-
'rsHosts': scheme_hosts['rs'],
157-
'rsfHosts': scheme_hosts['rsf'],
158-
'deadline': int(time.time()) + hosts['ttl']
153+
'upHosts': [
154+
'{0}://{1}'.format(self.scheme, domain)
155+
for domain in region.get('up', {}).get('domains', [])
156+
],
157+
'ioHosts': [
158+
'{0}://{1}'.format(self.scheme, domain)
159+
for domain in region.get('io', {}).get('domains', [])
160+
],
161+
'rsHosts': [
162+
'{0}://{1}'.format(self.scheme, domain)
163+
for domain in region.get('rs', {}).get('domains', [])
164+
],
165+
'rsfHosts': [
166+
'{0}://{1}'.format(self.scheme, domain)
167+
for domain in region.get('rsf', {}).get('domains', [])
168+
],
169+
'apiHosts': [
170+
'{0}://{1}'.format(self.scheme, domain)
171+
for domain in region.get('api', {}).get('domains', [])
172+
],
173+
'deadline': int(time.time()) + region['ttl']
159174
}
160175
home_dir = ""
161176
self.set_bucket_hosts_to_cache(key, bucket_hosts, home_dir)
@@ -166,9 +181,6 @@ def get_bucket_hosts_to_cache(self, key, home_dir):
166181
if len(self.host_cache) == 0:
167182
self.host_cache_from_file(home_dir)
168183

169-
if self.host_cache == {}:
170-
return ret
171-
172184
if key not in self.host_cache:
173185
return ret
174186

@@ -211,7 +223,7 @@ def bucket_hosts(self, ak, bucket):
211223
uc_host = UC_HOST
212224
if is_customized_default('default_uc_host'):
213225
uc_host = get_default('default_uc_host')
214-
url = "{0}/v1/query?ak={1}&bucket={2}".format(uc_host, ak, bucket)
226+
url = "{0}/v4/query?ak={1}&bucket={2}".format(uc_host, ak, bucket)
215227
ret = requests.get(url)
216228
data = compat.json.dumps(ret.json(), separators=(',', ':'))
217229
return data

qiniu/services/storage/bucket.py

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -411,11 +411,7 @@ def bucket_domain(self, bucket_name):
411411
Args:
412412
bucket_name: 存储空间名
413413
"""
414-
options = {
415-
'tbl': bucket_name,
416-
}
417-
url = "{0}/v6/domain/list?tbl={1}".format(config.get_default("default_api_host"), bucket_name)
418-
return self.__get(url, options)
414+
return self.list_domains(bucket_name)
419415

420416
def change_bucket_permission(self, bucket_name, private):
421417
"""
@@ -428,6 +424,12 @@ def change_bucket_permission(self, bucket_name, private):
428424
url = "{0}/private?bucket={1}&private={2}".format(config.get_default("default_uc_host"), bucket_name, private)
429425
return self.__post(url)
430426

427+
def __api_do(self, bucket, operation, data=None):
428+
ak = self.auth.get_access_key()
429+
api_host = self.zone.get_api_host(ak, bucket)
430+
url = '{0}/{1}'.format(api_host, operation)
431+
return self.__post(url, data)
432+
431433
def __uc_do(self, operation, *args):
432434
return self.__server_do(config.get_default('default_uc_host'), operation, *args)
433435

0 commit comments

Comments
 (0)