from qiniu import Auth
from qiniu import BucketManager
import urllib
from datetime import datetime
import requests
import json
access_key = 'yourAccessKey'
secret_key = 'yourSecretKey'
q = Auth(access_key, secret_key)
bucket = BucketManager(q)
bucket_name = 'yourBucket'
# 获取bucket下面所有图片的数量
now = datetime.now()
query_string = 'bucket='+bucket_name+'&begin=20170818140000&end='+now.strftime('%Y%m%d%H%M%S')+'&g=day®ion=z1'
url = 'http://api.qiniu.com/v6/count?'+query_string
part_path = '/v6/count?'+query_string+'\n'
token = 'QBox '+ q.token(part_path)
headers = {'Authorization' : token}
response = requests.get(url,headers=headers)
content = json.loads(response.content.decode('utf8'))
total = 10000
if content and 'datas' in content:
total = content['datas'][-1]
prefix = None
limit = 1000
delimiter = None
marker = None
fileNames = []
rounds = 0
# 每次循环处理一千个文件
if total%1000==0:
rounds = total//1000
else:
rounds = total//1000 + 1
# 将所有符合要求的文件名称放到 fileNames中
for i in range(rounds):
ret, eof, info = bucket.list(bucket_name, prefix, marker, limit, delimiter)
if ret and 'items' in ret:
fileNames.extend([x['key'] for x in ret['items']
if x['key'].endswith('.jpg') and 'gray' not in x['key'] and 'binary' not in x['key']])
else:
break
if ret and 'marker' in ret:
marker = ret['marker']
else:
break
print len(fileNames)
index = 1
# 下载文件
for fileName in fileNames:
base_url = 'yourHost'+fileName
private_url = q.private_download_url(base_url, expires=3600)
urllib.urlretrieve(private_url,'yourPath%s' %fileName)
print 'download ending...'
上面的代码,可以将七牛上的图片下载到本地,调用api,获取bucket下的图片的总数,然后计算循环次数。