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