forked from qiniu/python-sdk
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
2f4446c
commit 5d59aba
Showing
11 changed files
with
341 additions
and
187 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
Oops, something went wrong.