Python脚本05 —— 从七牛服务器下载图片

本文介绍如何使用Python从七牛云存储服务器下载图片到本地,并列出存储桶中的所有文件。通过设置下载路径和检查文件是否存在来避免重复下载。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

#coding=utf-8
#从测试七牛服务器下载图片
import sys
import urllib,urllib2
import os
from qiniu import Auth
from qiniu import BucketManager

access_key = ''
secret_key = ''
bucket_name = ''
bucket_domain = ''
q = Auth(access_key,secret_key)

bucket = BucketManager(q)

def download(key):
	base_url = 'http://%s/%s' % (bucket_domain, key)
	# private_url = q.private_download_url(base_url, expires=3600)
	print('url:' + base_url)
	#设置下载后存放的存储路径
	dest_file=os.path.join('/Users/fenggeqiang/Desktop/qiniu/',key)
	if not os.path.exists(dest_file):
		dest_dir = os.path.dirname(dest_file)
		if not os.path.exists(dest_dir):
			print 'dest dir not exists. created:' + dest_dir
			os.mkdir(dest_dir)
		urllib.urlretrieve(base_url , dest_file)
	else:
		print 'exists. skipped'

def list_all(prefix=None, limit=None):
    marker = None
    eof = False
    while eof is False:
        ret, eof, info = bucket.list(bucket_name, prefix=prefix, marker=marker, limit=limit)
        marker = ret.get('marker', None)
        for item in ret['items']:
            print('item:' + item['key'])
            if marker != None:
            	print('marker:' + marker)
            	pass
            download(item['key'])
            pass
    if eof is not True:
        # 错误处理
        pass

list_all()

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值