使用Python将七牛云批量下载文件到本地
- 之前旧的域名过期了没有续费,换了个新的域名备案还没完成,因为之前七牛云图床绑定了这个域名,现在需要把这个图床文件全部下载下来。
from qiniu import Auth
from qiniu import BucketManager
import requests
import os
access_key = 'o0mE-m0dfaHhxxxxxxxxxxxxxxxxxxxxMrEhswA'
secret_key = 'ONN81ZIgnAZ0xxxxxxxxxxxxxxxxxx4l0Eei'
q = Auth(access_key, secret_key)
bucket = BucketManager(q)
bucket_name = 'whaimeta'
prefix = None
limit = 1000
delimiter = None
marker = None
path = 'dir/'
ret, eof, info = bucket.list(bucket_name, prefix, marker, limit, delimiter)
for i in ret['items']:
print(i['key'])
base_url = 'http://域名/'+i['key']
print(base_url)
private_url = q.private_download_url(base_url, expires=100)
print(private_url)
r = requests.get(private_url)
if r.content:
if not os.path.exists(path):
os.makedirs(path)
try:
file = open(path + i['key'], "wb")
file.write(r.content)
file.flush()
file.close()
except Exception as e:
print(e)