Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

修复扩展状态码异常处理问题、添加了查询短信发送记录、空间和域名操作 相关方法 #379

Merged
merged 10 commits into from
Sep 23, 2020
1 change: 1 addition & 0 deletions examples/.qiniu_pythonsdk_hostscache.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"http:wxCLv4yl_5saIuOHbbZbkP-Ef3kFFFeCDYmwTdg3:upload30": {"upHosts": ["http://up.qiniu.com", "http://upload.qiniu.com", "-H up.qiniu.com http://183.131.7.3"], "ioHosts": ["http://iovip.qbox.me"], "deadline": 1598428478}}
31 changes: 31 additions & 0 deletions examples/batch_restoreAr.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
# -*- coding: utf-8 -*-
# flake8: noqa

"""
批量解冻文件
https://developer.qiniu.com/kodo/api/1250/batch
"""

from qiniu import build_batch_restoreAr, Auth, BucketManager

# 七牛账号的公钥和私钥
access_key = '<access_key>'
secret_key = '<secret_key>'

q = Auth(access_key, secret_key)

bucket = BucketManager(q)

# 存储空间
bucket_name = "空间名"

# 字典的键为需要解冻的文件,值为解冻有效期1-7
ops = build_batch_restoreAr(bucket_name,
{"test00.png": 1,
"test01.jpeg": 2,
"test02.mp4": 3
}
)

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

from qiniu import Auth
from qiniu import BucketManager

"""
获取空间绑定的加速域名
https://developer.qiniu.com/kodo/api/3949/get-the-bucket-space-domain
"""

# 七牛账号的 公钥和私钥
access_key = '<access_key>'
secret_key = '<secret_key>'

# 空间名
bucket_name = ''

q = Auth(access_key, secret_key)

bucket = BucketManager(q)

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

from qiniu import Auth
from qiniu import BucketManager

# 需要填写七牛账号的 公钥和私钥
access_key = '<access_key>'
secret_key = '<secret_key>'

# 空间名
bucket_name = ""

# private 参数必须是str类型,0表示公有空间,1表示私有空间
private = "0"

q = Auth(access_key, secret_key)

bucket = BucketManager(q)

ret, info = bucket.change_bucket_permission(bucket_name, private)
print(info)
60 changes: 60 additions & 0 deletions examples/domain_relevant.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
# -*- coding: utf-8 -*-
from qiniu import QiniuMacAuth, DomainManager
import json

"""域名上线"""

# 七牛账号的 公钥和私钥
access_key = "<access_key>"
secret_key = "<secret_key>"

auth = QiniuMacAuth(access_key, secret_key)

manager = DomainManager(auth)

# 域名
name = "zhuchangzhao2.peterpy.cn"

ret, res = manager.domain_online(name)

headers = {"code": res.status_code, "reqid": res.req_id, "xlog": res.x_log}
print(json.dumps(headers, indent=4, ensure_ascii=False))
print(json.dumps(ret, indent=4, ensure_ascii=False))

"""域名下线"""

# 七牛账号的 公钥和私钥
access_key = "<access_key>"
secret_key = "<secret_key>"

auth = QiniuMacAuth(access_key, secret_key)

manager = DomainManager(auth)

# 域名
name = ""

ret, res = manager.domain_offline(name)

headers = {"code": res.status_code, "reqid": res.req_id, "xlog": res.x_log}
print(json.dumps(headers, indent=4, ensure_ascii=False))
print(json.dumps(ret, indent=4, ensure_ascii=False))

"""删除域名"""

# 七牛账号的 公钥和私钥
access_key = "<access_key>"
secret_key = "<secret_key>"

auth = QiniuMacAuth(access_key, secret_key)

manager = DomainManager(auth)

# 域名
name = ""

ret, res = manager.delete_domain(name)

headers = {"code": res.status_code, "reqid": res.req_id, "xlog": res.x_log}
print(json.dumps(headers, indent=4, ensure_ascii=False))
print(json.dumps(ret, indent=4, ensure_ascii=False))
12 changes: 7 additions & 5 deletions examples/sms_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -84,15 +84,17 @@
print(req, info)
"""

"""
# 查询短信发送记录
req, info = sms.get_messages_info()
print(req, info)
"""

"""
#发送短信
"""
template_id = ''
template_id = ''
mobiles = []
parameters = {}
req, info = sms.sendMessage(template_id, mobiles, parameters)
print(req, info)




37 changes: 37 additions & 0 deletions examples/upload_with_append.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
from qiniu import Auth, urlsafe_base64_encode, append_file

# 七牛账号的公私钥
access_key = '<access_key>'
secret_key = '<secret_key>'

# 要上传的空间
bucket_name = ""

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

key = "append.txt"

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


def file2base64(localfile):
with open(localfile, 'rb') as f: # 以二进制读取文件
data = f.read()
return data


# 要追加的文本文件路径
localfile = ""

data = file2base64(localfile)

# 首次以追加方式上传文件时,offset设置为0;后续继续追加内容时需要传入上次追加成功后响应的"nextAppendPosition" :34 参数值。
offset = 0

encodekey = urlsafe_base64_encode(key)

ret, info = append_file(token, encodekey, data, offset)
print(ret)
print(info)
2 changes: 1 addition & 1 deletion qiniu/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
from .region import Region

from .services.storage.bucket import BucketManager, build_batch_copy, build_batch_rename, build_batch_move, \
build_batch_stat, build_batch_delete
build_batch_stat, build_batch_delete, build_batch_restoreAr
from .services.storage.uploader import put_data, put_file, put_stream
from .services.cdn.manager import CdnManager, create_timestamp_anti_leech_url, DomainManager
from .services.processing.pfop import PersistentFop
Expand Down
19 changes: 13 additions & 6 deletions qiniu/http.py
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ def _post_with_auth(url, data, auth):


def _get_with_auth(url, data, auth):
return _get(url, data, qiniu.auth.RequestsAuth(auth))
return _get(url, data, qiniu.auth.RequestsAuth(auth))


def _post_with_auth_and_headers(url, data, auth, headers):
Expand Down Expand Up @@ -245,14 +245,14 @@ def __init__(self, response, exception=None):
self.req_id = response.headers.get('X-Reqid')
self.x_log = response.headers.get('X-Log')
if self.status_code >= 400:
if 600 > self.status_code >= 499:
self.error = response.text
else:
if self.__check_json(response):
ret = response.json() if response.text != '' else None
if ret is None or ret['error'] is None:
if ret is None:
self.error = 'unknown'
else:
self.error = ret['error']
self.error = response.text
else:
self.error = response.text
if self.req_id is None and self.status_code == 200:
self.error = 'server is not qiniu'

Expand Down Expand Up @@ -280,3 +280,10 @@ def __str__(self):

def __repr__(self):
return self.__str__()

def __check_json(self, reponse):
try:
reponse.json()
return True
except Exception:
return False
40 changes: 40 additions & 0 deletions qiniu/services/cdn/manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -200,6 +200,46 @@ def create_domain(self, name, body):
url = '{0}/domain/{1}'.format(self.server, name)
return self.__post(url, body)

def domain_online(self, name):
"""
上线域名,文档 https://developer.qiniu.com/fusion/api/4246/the-domain-name#6

Args:
name: 域名, 如果是泛域名,必须以点号 . 开头
bosy: 创建域名参数
Returns:
{}
"""
url = '{0}/domain/{1}/online'.format(self.server, name)
return http._post_with_qiniu_mac(url, None, self.auth)

def domain_offline(self, name):
"""
下线域名,文档 https://developer.qiniu.com/fusion/api/4246/the-domain-name#5

Args:
name: 域名, 如果是泛域名,必须以点号 . 开头
bosy: 创建域名参数
Returns:
{}
"""
url = '{0}/domain/{1}/offline'.format(self.server, name)
return http._post_with_qiniu_mac(url, None, self.auth)

def delete_domain(self, name):
"""
删除域名,文档 https://developer.qiniu.com/fusion/api/4246/the-domain-name#8

Args:
name: 域名, 如果是泛域名,必须以点号 . 开头
Returns:
返回一个tuple对象,其格式为(<result>, <ResponseInfo>)
- result 成功返回dict{},失败返回{"error": "<errMsg string>"}
- ResponseInfo 请求的Response信息
"""
url = '{0}/domain/{1}'.format(self.server, name)
return self.__get(url)

def get_domain(self, name):
"""
获取域名信息,文档 https://developer.qiniu.com/fusion/api/4246/the-domain-name
Expand Down
Loading