Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 26 additions & 0 deletions examples/batch_copy.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
# -*- coding: utf-8 -*-
"""
批量拷贝文件

https://developer.qiniu.com/kodo/api/1250/batch
"""


from qiniu import build_batch_copy, Auth, BucketManager

access_key = ''

secret_key = ''

q = Auth(access_key, secret_key)

bucket = BucketManager(q)

src_bucket_name = ''

target_bucket_name = ''

# force为true时强制同名覆盖, 字典的键为原文件,值为目标文件
ops = build_batch_copy(src_bucket_name, {'src_key1': 'target_key1', 'src_key2': 'target_key2'}, target_bucket_name, force='true')
ret, info = bucket.batch(ops)
print(info)
25 changes: 25 additions & 0 deletions examples/batch_delete.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
# -*- coding: utf-8 -*-
"""
批量删除文件

https://developer.qiniu.com/kodo/api/1250/batch
"""


from qiniu import build_batch_delete, Auth, BucketManager

access_key = ''

secret_key = ''

q = Auth(access_key, secret_key)

bucket = BucketManager(q)

bucket_name = ''

keys = ['1.gif', '2.txt', '3.png', '4.html']

ops = build_batch_delete(bucket_name, keys)
ret, info = bucket.batch(ops)
print(info)
27 changes: 27 additions & 0 deletions examples/batch_move.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
# -*- coding: utf-8 -*-
"""
批量移动文件

https://developer.qiniu.com/kodo/api/1250/batch
"""


from qiniu import build_batch_move, Auth, BucketManager

access_key = ''

secret_key = ''


q = Auth(access_key, secret_key)

bucket = BucketManager(q)

src_bucket_name = ''

target_bucket_name = ''

# force为true时强制同名覆盖, 字典的键为原文件,值为目标文件
ops = build_batch_move(src_bucket_name, {'src_key1': 'target_key1', 'src_key2': 'target_key2'}, target_bucket_name, force='true')
ret, info = bucket.batch(ops)
print(info)
26 changes: 26 additions & 0 deletions examples/batch_rename.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
# -*- coding: utf-8 -*-
"""
批量重命名文件

https://developer.qiniu.com/kodo/api/1250/batch
"""


from qiniu import build_batch_rename, Auth, BucketManager

access_key = ''

secret_key = ''


q = Auth(access_key, secret_key)

bucket = BucketManager(q)

bucket_name = ''


# force为true时强制同名覆盖, 字典的键为原文件,值为目标文件
ops = build_batch_rename(bucket_name, {'src_key1': 'target_key1', 'src_key2': 'target_key2'}, force='true')
ret, info = bucket.batch(ops)
print(info)
26 changes: 26 additions & 0 deletions examples/batch_stat.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
# -*- coding: utf-8 -*-
"""
批量查询文件信息

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)

bucket = BucketManager(q)

bucket_name = ''

# 需要查询的文件名
keys = ['1.gif', '2.txt', '3.png', '4.html']

ops = build_batch_stat(bucket_name, keys)
ret, info = bucket.batch(ops)
print(info)
30 changes: 30 additions & 0 deletions examples/cdn_bandwidth.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
# -*- coding: utf-8 -*-
"""
查询指定域名指定时间段内的带宽
"""
import qiniu
from qiniu import CdnManager


# 账户ak,sk
access_key = ''
secret_key = ''

auth = qiniu.Auth(access_key=access_key, secret_key=secret_key)
cdn_manager = CdnManager(auth)

startDate = '2017-07-20'

endDate = '2017-08-20'

granularity = 'day'

urls = [
'a.example.com',
'b.example.com'
]

ret, info = cdn_manager.get_bandwidth_data(urls, startDate, endDate, granularity)

print(ret)
print(info)
31 changes: 31 additions & 0 deletions examples/cdn_flux.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
# -*- coding: utf-8 -*-
"""
查询指定域名指定时间段内的流量
"""
import qiniu
from qiniu import CdnManager


# 账户ak,sk
access_key = ''
secret_key = ''

auth = qiniu.Auth(access_key=access_key, secret_key=secret_key)
cdn_manager = CdnManager(auth)

startDate = '2017-07-20'

endDate = '2017-08-20'

granularity = 'day'

urls = [
'a.example.com',
'b.example.com'
]

# 获得指定域名流量
ret, info = cdn_manager.get_flux_data(urls, startDate, endDate, granularity)

print(ret)
print(info)
27 changes: 27 additions & 0 deletions examples/cdn_log.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
# -*- coding: utf-8 -*-
"""
获取指定域名指定时间内的日志链接
"""
import qiniu
from qiniu import CdnManager


# 账户ak,sk
access_key = ''
secret_key = ''

auth = qiniu.Auth(access_key=access_key, secret_key=secret_key)
cdn_manager = CdnManager(auth)

log_date = '2017-07-20'

urls = [
'a.example.com',
'b.example.com'
]


ret, info = cdn_manager.get_log_list_data(urls, log_date)

print(ret)
print(info)
File renamed without changes.
File renamed without changes.
30 changes: 30 additions & 0 deletions examples/timestamp_url.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
# -*- coding: utf-8 -*-

"""
获取一个配置时间戳防盗链的url
"""

from qiniu.services.cdn.manager import create_timestamp_anti_leech_url
import time

host = 'http://a.example.com'

# 配置时间戳时指定的key
encrypt_key = ''

# 资源路径
file_name = 'a/b/c/example.jpeg'

# 查询字符串,不需加?
query_string = ''

# 截止日期的时间戳,秒为单位,3600为当前时间一小时之后过期
deadline = int(time.time())+3600


timestamp_url = create_timestamp_anti_leech_url(host, file_name, query_string, encrypt_key, deadline)

print(timestamp_url)



32 changes: 32 additions & 0 deletions examples/upload_token.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
# -*- coding: utf-8 -*-
# flake8: noqa

from qiniu import Auth

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

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

#要上传的空间
bucket_name = ''

#上传到七牛后保存的文件名
key = ''

#生成上传 Token,可以指定过期时间等

# 上传策略示例
# https://developer.qiniu.com/kodo/manual/1206/put-policy
policy = {
# 'callbackUrl':'https://requestb.in/1c7q2d31',
# 'callbackBody':'filename=$(fname)&filesize=$(fsize)'
# 'persistentOps':'imageView2/1/w/200/h/200'
}

token = q.upload_token(bucket_name, key, 3600, policy)

print(token)

16 changes: 6 additions & 10 deletions qiniu/services/cdn/manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ def prefetch_urls(self, urls):

def get_bandwidth_data(self, domains, start_date, end_date, granularity):
"""
预取带宽数据,文档 http://developer.qiniu.com/article/fusion/api/traffic-bandwidth.html
查询带宽数据,文档 http://developer.qiniu.com/article/fusion/api/traffic-bandwidth.html

Args:
domains: 域名列表
Expand All @@ -115,7 +115,7 @@ def get_bandwidth_data(self, domains, start_date, end_date, granularity):

def get_flux_data(self, domains, start_date, end_date, granularity):
"""
预取流量数据,文档 http://developer.qiniu.com/article/fusion/api/traffic-bandwidth.html
查询流量数据,文档 http://developer.qiniu.com/article/fusion/api/traffic-bandwidth.html

Args:
domains: 域名列表
Expand Down Expand Up @@ -162,25 +162,21 @@ def __post(self, url, data=None):
return http._post_with_auth_and_headers(url, data, self.auth, headers)


def create_timestamp_anti_leech_url(host, file_name, query_string_dict, encrypt_key, deadline):
def create_timestamp_anti_leech_url(host, file_name, query_string, encrypt_key, deadline):
"""
创建时间戳防盗链

Args:
host: 带访问协议的域名
file_name: 原始文件名,不需要urlencode
query_string_dict: 查询参数,不需要urlencode
query_string: 查询参数,不需要urlencode
encrypt_key: 时间戳防盗链密钥
deadline: 链接有效期时间戳(以秒为单位)

Returns:
带时间戳防盗链鉴权访问链接
"""
if query_string_dict is not None and len(query_string_dict) > 0:
query_string_items = []
for k, v in query_string_dict.items():
query_string_items.append('{0}={1}'.format(urlencode(str(k)), urlencode(str(v))))
query_string = '&'.join(query_string_items)
if query_string:
url_to_sign = '{0}/{1}?{2}'.format(host, urlencode(file_name), query_string)
else:
url_to_sign = '{0}/{1}'.format(host, urlencode(file_name))
Expand All @@ -190,7 +186,7 @@ def create_timestamp_anti_leech_url(host, file_name, query_string_dict, encrypt_
str_to_sign = '{0}{1}{2}'.format(encrypt_key, path, expire_hex).encode()
sign_str = hashlib.md5(str_to_sign).hexdigest()

if query_string_dict is not None and len(query_string_dict) > 0:
if query_string:
signed_url = '{0}&sign={1}&t={2}'.format(url_to_sign, sign_str, expire_hex)
else:
signed_url = '{0}?sign={1}&t={2}'.format(url_to_sign, sign_str, expire_hex)
Expand Down