七牛云批量下载文件到本地

七牛云中文件批量下载

在这里插入图片描述

业务需要
  • 之前很多的文件都存储在了七牛云,但是目前有项目需要在内网部署,所以所有的外链资源都要本地化。所以要从七牛云的仓库中再下载回来。

1.终端脚本下载

官方文档
参考官方文档中的配置进行配置。当可以配置了PATH之后,可以在终端使用qshell的时候,就成功了一半。
我的设备是mac,在运行的时候发现如下问题

  • MacOs 无法打开xxx ,因为无法验证开发者的问题。导致本地terminal无法使用qshell
# 禁用验证
sudo spctl --master-disable

qshell命令行需要参考如下文档:qshell命令行文档
进行如下操作,注册账号信息

#需要鉴权的命令都需要依赖七牛账号下的 AccessKey 和 SecretKey。所以这类命令运行之前,需要使用 account 命令来设置下 AccessKey ,SecretKey ,最后的账号没具体看过,我是写的账户名,应该可以随便起的
qshell account m1lGO9G95*******************hSIQX_O694U 3KeBSg65S*******************PtkwZ4CFRXcyD progerchai@gmail.com

# 下载命令-- 单文件下载
# qshell get <Bucket> <Key> [-o <OutFile>]
# Bucket	存储空间(空间名)
# Key	存储空间中的文件名字(文件名)
# OutFile	保存在本地的名字,不指定,默认使用存储空间中的名字(忽略默认到当前terminal路径)
qshell get mo-imgs ClassRoom/detail.png 

qshell qdownload 参考文档
配置conf

{
    "dest_dir"   :   "<LocalBackupDir>",
    "bucket"     :   "<Bucket>",
    "prefix"     :   "image/",
    "suffixes"   :   ".png,.jpg",
    "cdn_domain" :   "down.example.com",
    "referer"    :   "http://www.example.com",
    "log_file"   :   "download.log",
    "log_level"  :   "info",
    "log_rotate" :   1,
    "log_stdout" :   false
}

在这里插入图片描述

# 多文件批量下载
qshell qdownload -c 10 qdisk_down.conf  # 10是线程数,支持的同时下载文件数量

效果图

在这里插入图片描述

2.python批量下载

根据官方文档中的配置
参考官方的代码,参考地址
根据文档可以写一个py文件,在终端运行。
终端cd到py文件路径,执行

python downloadQiniu.py

出现的一些错误:

  1. no module named ‘qiniu’
# 解决
pip install qiniu
$ python downloadQiniu.py # 执行py

# 结果
1/发票.pdf
https://mo-imgs.momodel.cn/1/发票.pdf
Traceback (most recent call last):
  File "downloadQiniu.py", line 32, in <module>
    private_url = q.private_download_url(base_url, expires=100)
  File "/Users/aiyouwei/Library/Python/2.7/lib/python/site-packages/qiniu/auth.py", line 121, in private_download_url
    url = '{0}e={1}'.format(url, str(deadline))
UnicodeEncodeError: 'ascii' codec can't encode characters in position 29-30: ordinal not in range(128)

# 解决
# qiniu py文件出错
# 1.到对应到目录 File "/Users/aiyouwei/Library/Python/2.7/lib/python/site-packages/qiniu/auth.py", line 121
# 2.打开这个文件,修改代码中的 format(url, str(deadline)) 为 format(url.encode('utf-8'), str(deadline))
  1. No such file or directory
# 执行
$ python downloadQiniu.py

# 结果
1/发票.pdf
https://mo-imgs.momodel.cn/1/发票.pdf
https://mo-imgs.momodel.cn/1/发票.pdf?e=1599840018&token=m1lGO9G95*******************hSIQX_O694U:0w1*********tdZD0JIps=
Traceback (most recent call last):
  File "downloadQiniu.py", line 40, in <module>
    file = open(path + i['key'], "wb")
IOError: [Errno 2] No such file or directory: u'/Users/aiyouwei/Desktop/qiniu/1/\u53d1\u7968.pdf'

问题的原因是官方给的py代码file path 的地方处理有点问题,可以直接用下面的最终代码运行。
最终的代码:

# -*- coding: utf-8 -*-
from qiniu import Auth
from qiniu import BucketManager
import requests
import os

access_key = 'm1lGO9G95*******************hSIQX_O694U'
secret_key = '3KeBSg65S*******************PtkwZ4CFRXcyD'

q = Auth(access_key, secret_key)
bucket = BucketManager(q)

bucket_name = 'mo-imgs'
# 前缀 一般无需理会,可自定义前缀
prefix = None
# 列举条目,你需要的数据,有大小限制,1-1000,默认1000
limit = 1000
# 列举出除'/'的所有文件以及以'/'为分隔的所有前缀
delimiter = None
# 标记
marker = None
# 下载到你本地的地址,记得头尾加上 / 代表文件夹
path = '/Users/aiyouwei/Desktop/qiniu/mo-imgs/'

ret, eof, info = bucket.list(bucket_name, prefix, marker, limit, delimiter)
for i in ret['items']:
    print(i['key'])
    base_url = 'https://mo-imgs.momodel.cn/'+i['key']
    print(base_url)

    #如果空间有时间戳防盗链或是私有空间,可以调用该方法生成私有链接
    private_url = q.private_download_url(base_url, expires=100)
    print(private_url)

    r = requests.get(private_url)
    pass
    if r.content:
        file_path = path + i['key']
        file_dir = os.path.dirname(file_path)
        if not os.path.exists(file_dir):
            os.makedirs(file_dir)
        file = open(file_path, "wb")
        file.write(r.content)
        file.flush()
        file.close()

最终结果:成功下载下来所有的文件
在这里插入图片描述


对应的
access_key = ‘’
secret_key = ‘’
做的一定的处理,请填自己的对应七牛云账号下的key

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值