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

#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()

你可能感兴趣的:(脚本,脚本)