Skip to content

Commit

Permalink
add rs and region
Browse files Browse the repository at this point in the history
  • Loading branch information
bernieyangmh committed Feb 22, 2019
1 parent 2f4446c commit 5d59aba
Show file tree
Hide file tree
Showing 11 changed files with 341 additions and 187 deletions.
2 changes: 0 additions & 2 deletions examples/batch_stat.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,9 @@
https://developer.qiniu.com/kodo/api/1250/batch
"""


from qiniu import build_batch_stat, Auth, BucketManager

access_key = ''

secret_key = ''

q = Auth(access_key, secret_key)
Expand Down
19 changes: 19 additions & 0 deletions examples/bucket_info.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
# -*- coding: utf-8 -*-
# flake8: noqa

from qiniu import Auth
from qiniu import BucketManager

# 需要填写你的 Access Key 和 Secret Key
access_key = ''
secret_key = ''

# 空间名
bucket_name = 'bucket_name'

q = Auth(access_key, secret_key)

bucket = BucketManager(q)

ret, info = bucket.bucket_info(bucket_name)
print(info)
29 changes: 29 additions & 0 deletions examples/change_status.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
# -*- coding: utf-8 -*-
# flake8: noqa

from qiniu import Auth
from qiniu import BucketManager

# 需要填写你的 Access Key 和 Secret Key
access_key = ''
secret_key = ''

q = Auth(access_key, secret_key)

bucket = BucketManager(q)

# 空间名
bucket_name = 'bucket_name'

# 文件名
key = 'file_name'

# 条件匹配,只有匹配上才会执行修改操作
# cond可以填空,一个或多个
cond = {"fsize": "186371",
"putTime": "14899798962573916",
"hash": "FiRxWzeeD6ofGTpwTZub5Fx1ozvi",
"mime": "image/png"}

ret, info = bucket.change_status(bucket_name, key, '0', cond)
print(info)
25 changes: 25 additions & 0 deletions examples/list_buckets.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
# -*- coding: utf-8 -*-
# flake8: noqa

from qiniu import Auth
from qiniu import BucketManager

# 需要填写你的 Access Key 和 Secret Key
access_key = ''
secret_key = ''

q = Auth(access_key, secret_key)

bucket = BucketManager(q)

# 指定需要列举的区域,填空字符串返回全部空间,为减少响应时间建议填写
# z0:只返回华东区域的空间
# z1:只返回华北区域的空间
# z2:只返回华南区域的空间
# na0:只返回北美区域的空间
# as0:只返回东南亚区域的空间
region = "z0"

ret, info = bucket.list_bucket(region)
print(info)
print(ret)
20 changes: 20 additions & 0 deletions examples/mk_bucket.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
# -*- coding: utf-8 -*-
# flake8: noqa

from qiniu import Auth
from qiniu import BucketManager

# 需要填写你的 Access Key 和 Secret Key
access_key = '...'
secret_key = '...'

bucket_name = 'Bucket_Name'

q = Auth(access_key, secret_key)

bucket = BucketManager(q)

region = "z0"

ret, info = bucket.mkbucketv2(bucket_name, region)
print(info)
32 changes: 18 additions & 14 deletions examples/upload_with_zone.py
Original file line number Diff line number Diff line change
@@ -1,38 +1,42 @@
# -*- coding: utf-8 -*-
# flake8: noqa

from qiniu import Auth, put_file, etag, urlsafe_base64_encode
import qiniu.config
from qiniu import Auth, put_file
from qiniu import Zone, set_default

# 需要填写你的 Access Key 和 Secret Key
access_key = '...'
secret_key = '...'
access_key = ''
secret_key = ''

# 构建鉴权对象
q = Auth(access_key, secret_key)

# 要上传的空间
bucket_name = 'Bucket_Name'
bucket_name = 'bucket_name'

# 上传到七牛后保存的文件名
key = 'my-python-logo.png'
key = 'a.jpg'

# 生成上传 Token,可以指定过期时间等
token = q.upload_token(bucket_name, key, 3600)

# 要上传文件的本地路径
localfile = 'stat.py'
localfile = '/Users/abc/Documents/a.jpg'

# 指定固定域名的zone,不同区域uphost域名见下文档
# https://developer.qiniu.com/kodo/manual/1671/region-endpoint
# 未指定或上传错误,sdk会根据token自动查询对应的上传域名
# *.qiniup.com 支持https上传
# 备用*.qiniu.com域名 不支持https上传
# 要求https上传时,如果客户指定的两个host都错误,且sdk自动查询的第一个*.qiniup.com上传域名因意外不可用导致访问到备用*.qiniu.com会报ssl错误
# 建议https上传时查看上面文档,指定正确的host

# 指定固定的zone
zone = Zone(
up_host='uptest.qiniu.com',
up_host_backup='uptest.qiniu.com',
io_host='iovip.qbox.me',
scheme='http')
up_host='https://up.qiniup.com',
up_host_backup='https://upload.qiniup.com',
io_host='http://iovip.qbox.me',
scheme='https')
set_default(default_zone=zone)

ret, info = put_file(token, key, localfile)
print(info)
assert ret['key'] == key
assert ret['hash'] == etag(localfile)
16 changes: 10 additions & 6 deletions qiniu/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,10 @@

from qiniu import zone

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

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

Expand All @@ -13,9 +14,10 @@
'default_rs_host': RS_HOST,
'default_rsf_host': RSF_HOST,
'default_api_host': API_HOST,
'connection_timeout': 30, # 链接超时为时间为30s
'connection_retries': 3, # 链接重试次数为3次
'connection_pool': 10, # 链接池个数为10
'default_uc_host': UC_HOST,
'connection_timeout': 30, # 链接超时为时间为30s
'connection_retries': 3, # 链接重试次数为3次
'connection_pool': 10, # 链接池个数为10
}


Expand All @@ -25,7 +27,7 @@ def get_default(key):

def set_default(
default_zone=None, connection_retries=None, connection_pool=None,
connection_timeout=None, default_rs_host=None,
connection_timeout=None, default_rs_host=None, default_uc_host=None,
default_rsf_host=None, default_api_host=None):
if default_zone:
_config['default_zone'] = default_zone
Expand All @@ -35,6 +37,8 @@ def set_default(
_config['default_rsf_host'] = default_rsf_host
if default_api_host:
_config['default_api_host'] = default_api_host
if default_uc_host:
_config['default_uc_host'] = default_api_host
if connection_retries:
_config['connection_retries'] = connection_retries
if connection_pool:
Expand Down
161 changes: 161 additions & 0 deletions qiniu/region.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,161 @@
# -*- coding: utf-8 -*-

import os
import time
import requests
from qiniu import compat
from qiniu import utils

UC_HOST = 'https://uc.qbox.me' # 获取空间信息Host


class Region(object):
"""七牛上传区域类
该类主要内容上传区域地址。
"""

def __init__(
self,
up_host=None,
up_host_backup=None,
io_host=None,
host_cache={},
scheme="http",
home_dir=os.getcwd()):
"""初始化Zone类"""
self.up_host, self.up_host_backup, self.io_host = up_host, up_host_backup, io_host
self.host_cache = host_cache
self.scheme = scheme
self.home_dir = home_dir

def get_up_host_by_token(self, up_token):
ak, bucket = self.unmarshal_up_token(up_token)
up_hosts = self.get_up_host(ak, bucket)
return up_hosts[0]

def get_up_host_backup_by_token(self, up_token):
ak, bucket = self.unmarshal_up_token(up_token)
up_hosts = self.get_up_host(ak, bucket)
if (len(up_hosts) <= 1):
up_host = up_hosts[0]
else:
up_host = up_hosts[1]
return up_host

def get_io_host(self, ak, bucket):
if self.io_host:
return self.io_host
bucket_hosts = self.get_bucket_hosts(ak, bucket)
io_hosts = bucket_hosts['ioHosts']
return io_hosts[0]

def get_up_host(self, ak, bucket):
bucket_hosts = self.get_bucket_hosts(ak, bucket)
up_hosts = bucket_hosts['upHosts']
return up_hosts

def unmarshal_up_token(self, up_token):
token = up_token.split(':')
if (len(token) != 3):
raise ValueError('invalid up_token')

ak = token[0]
policy = compat.json.loads(
compat.s(
utils.urlsafe_base64_decode(
token[2])))

scope = policy["scope"]
bucket = scope
if (':' in scope):
bucket = scope.split(':')[0]

return ak, bucket

def get_bucket_hosts(self, ak, bucket):
key = self.scheme + ":" + ak + ":" + bucket

bucket_hosts = self.get_bucket_hosts_to_cache(key)
if (len(bucket_hosts) > 0):
return bucket_hosts

hosts = {}
hosts.update({self.scheme: {}})

hosts[self.scheme].update({'up': []})
hosts[self.scheme].update({'io': []})

if self.up_host is not None:
hosts[self.scheme]['up'].append(self.scheme + "://" + self.up_host)

if self.up_host_backup is not None:
hosts[self.scheme]['up'].append(
self.scheme + "://" + self.up_host_backup)

if self.io_host is not None:
hosts[self.scheme]['io'].append(self.scheme + "://" + self.io_host)

if len(hosts[self.scheme]) == 0 or self.io_host is None:
hosts = compat.json.loads(self.bucket_hosts(ak, bucket))
else:
# 1 year
hosts['ttl'] = int(time.time()) + 31536000
try:
scheme_hosts = hosts[self.scheme]
except KeyError:
raise KeyError(
"Please check your BUCKET_NAME! The UpHosts is %s" %
hosts)
bucket_hosts = {
'upHosts': scheme_hosts['up'],
'ioHosts': scheme_hosts['io'],
'deadline': int(time.time()) + hosts['ttl']
}

self.set_bucket_hosts_to_cache(key, bucket_hosts)
return bucket_hosts

def get_bucket_hosts_to_cache(self, key):
ret = []
if (len(self.host_cache) == 0):
self.host_cache_from_file()

if (not (key in self.host_cache)):
return ret

if (self.host_cache[key]['deadline'] > time.time()):
ret = self.host_cache[key]

return ret

def set_bucket_hosts_to_cache(self, key, val):
self.host_cache[key] = val
self.host_cache_to_file()
return

def host_cache_from_file(self):
path = self.host_cache_file_path()
if not os.path.isfile(path):
return None
with open(path, 'r') as f:
bucket_hosts = compat.json.load(f)
self.host_cache = bucket_hosts
f.close()
return

def host_cache_file_path(self):
return os.path.join(self.home_dir, ".qiniu_pythonsdk_hostscache.json")

def host_cache_to_file(self):
path = self.host_cache_file_path()
with open(path, 'w') as f:
compat.json.dump(self.host_cache, f)
f.close()

def bucket_hosts(self, ak, bucket):
url = "{0}/v1/query?ak={1}&bucket={2}".format(UC_HOST, ak, bucket)
ret = requests.get(url)
data = compat.json.dumps(ret.json(), separators=(',', ':'))
return data
Loading

0 comments on commit 5d59aba

Please sign in to comment.