|
| 1 | +# -*- coding: utf-8 -*- |
| 2 | + |
| 3 | +import os |
| 4 | +import time |
| 5 | + |
| 6 | +from qiniu import http |
| 7 | +from qiniu import compat, utils |
| 8 | + |
| 9 | +UC_HOST = 'https://uc.qbox.me' # 获取空间信息Host |
| 10 | + |
| 11 | + |
| 12 | +class Zone(object): |
| 13 | + """七牛上传区域类 |
| 14 | +
|
| 15 | + 该类主要内容上传区域地址。 |
| 16 | +
|
| 17 | + Attributes: |
| 18 | + up_host: 首选上传地址 |
| 19 | + up_host_backup: 备用上传地址 |
| 20 | + """ |
| 21 | + def __init__(self, up_host=None, up_host_backup=None, io_host=None, host_cache={}, scheme="http"): |
| 22 | + """初始化Zone类""" |
| 23 | + self.up_host, self.up_host_backup, self.io_host = up_host, up_host_backup, io_host |
| 24 | + self.host_cache = host_cache |
| 25 | + self.scheme = scheme |
| 26 | + |
| 27 | + def get_up_host_by_token(self, up_token): |
| 28 | + ak, bucket = self.unmarshal_up_token(up_token) |
| 29 | + up_hosts = self.get_up_host(ak, bucket) |
| 30 | + return up_hosts[0] |
| 31 | + |
| 32 | + def get_up_host_backup_by_token(self, up_token): |
| 33 | + ak, bucket = self.unmarshal_up_token(up_token) |
| 34 | + up_hosts = self.get_up_host(ak, bucket) |
| 35 | + |
| 36 | + if (len(up_hosts) <= 1): |
| 37 | + up_host = up_hosts[0] |
| 38 | + else: |
| 39 | + up_host = up_hosts[1] |
| 40 | + return up_host |
| 41 | + |
| 42 | + def get_io_host(self, ak, bucket): |
| 43 | + bucket_hosts = self.get_bucket_hosts(ak, bucket) |
| 44 | + io_hosts = bucket_hosts['ioHosts'] |
| 45 | + return io_hosts[0] |
| 46 | + |
| 47 | + def get_up_host(self, ak, bucket): |
| 48 | + bucket_hosts = self.get_bucket_hosts(ak, bucket) |
| 49 | + up_hosts = bucket_hosts['upHosts'] |
| 50 | + return up_hosts |
| 51 | + |
| 52 | + def unmarshal_up_token(self, up_token): |
| 53 | + token = up_token.split(':') |
| 54 | + if(len(token) != 3): |
| 55 | + raise ValueError('invalid up_token') |
| 56 | + |
| 57 | + ak = token[0] |
| 58 | + policy = compat.json.loads(str(utils.urlsafe_base64_decode(token[2]), "utf-8")) |
| 59 | + |
| 60 | + scope = policy["scope"] |
| 61 | + bucket = scope |
| 62 | + if(':' in scope): |
| 63 | + bucket = scope.split(':')[0] |
| 64 | + |
| 65 | + return ak, bucket |
| 66 | + |
| 67 | + def get_bucket_hosts(self, ak, bucket): |
| 68 | + key = self.scheme + ":" + ak + ":" + bucket |
| 69 | + |
| 70 | + bucket_hosts = self.get_bucket_hosts_to_cache(key) |
| 71 | + if(len(bucket_hosts) > 0): |
| 72 | + return bucket_hosts |
| 73 | + |
| 74 | + hosts = compat.json.loads(self.bucket_hosts(ak, bucket)) |
| 75 | + |
| 76 | + scheme_hosts = hosts[self.scheme] |
| 77 | + bucket_hosts = { |
| 78 | + 'upHosts': scheme_hosts['up'], |
| 79 | + 'ioHosts': scheme_hosts['io'], |
| 80 | + 'deadline': int(time.time()) + hosts['ttl'] |
| 81 | + } |
| 82 | + |
| 83 | + self.set_bucket_hosts_to_cache(key, bucket_hosts) |
| 84 | + |
| 85 | + # hosts = self.bucket_hosts(ak, bucket) |
| 86 | + # self.up_host = compat.json.loads(hosts)[self.scheme]["up"][0] |
| 87 | + # self.up_host_backup = compat.json.loads(hosts)[self.scheme]["up"][1] |
| 88 | + # self.io_host = compat.json.loads(hosts)[self.scheme]["io"][0] |
| 89 | + return bucket_hosts |
| 90 | + |
| 91 | + def get_bucket_hosts_to_cache(self, key): |
| 92 | + ret = [] |
| 93 | + if(len(self.host_cache) == 0): |
| 94 | + self.host_cache_from_file() |
| 95 | + |
| 96 | + if(not (key in self.host_cache)): |
| 97 | + return ret |
| 98 | + |
| 99 | + if(self.host_cache[key]['deadline'] > time.time()): |
| 100 | + ret = self.host_cache[key] |
| 101 | + |
| 102 | + return ret |
| 103 | + |
| 104 | + def set_bucket_hosts_to_cache(self, key, val): |
| 105 | + self.host_cache[key] = val |
| 106 | + self.host_cache_to_file() |
| 107 | + return |
| 108 | + |
| 109 | + def host_cache_from_file(self): |
| 110 | + path = self.host_cache_file_path() |
| 111 | + if not os.path.isfile(path): |
| 112 | + return None |
| 113 | + with open(path, 'r') as f: |
| 114 | + bucket_hosts = compat.json.load(f) |
| 115 | + self.host_cache = bucket_hosts |
| 116 | + f.close() |
| 117 | + return |
| 118 | + |
| 119 | + def host_cache_to_file(self): |
| 120 | + path = self.host_cache_file_path() |
| 121 | + with open(path, 'w') as f: |
| 122 | + compat.json.dump(self.host_cache, f) |
| 123 | + f.close() |
| 124 | + |
| 125 | + def host_cache_file_path(self): |
| 126 | + home = os.getenv("HOME") |
| 127 | + return home + "/.qiniu_pythonsdk_hostscache2.json" |
| 128 | + |
| 129 | + def bucket_hosts(self, ak, bucket): |
| 130 | + url = "{0}/v1/query?ak={1}&bucket={2}".format(UC_HOST, ak, bucket) |
| 131 | + ret, info = http._get(url, None, None) |
| 132 | + data = compat.json.dumps(ret, separators=(',', ':')) |
| 133 | + return data |
0 commit comments