七牛是个好东西
免费10G空间10G下载流量10w上传请求100w下载请求 自带CDN和图片处理 对于个人用户和创业公司来说是个很好的平台
本文主要是解决 服务器端请求七牛对网络某文件进行保存这一业务需求
这里是七牛文章中对Fetch API的说明。写的有点复杂,不过用Python sdk比较简单
七牛的Python sdk支持 pip 和 easy_install 安装,包名是 qiniu 即:
pip install qiniu esay_install qiniu
对于SAE用户,安装好sae python本地环境后在项目目录(就是有config.yaml 和 index.wsgi的目录)运行:
saecloud install qiniu本地环境就会将七牛SDK下载到项目目录的site-packages文件夹中。
修改index.wsgi文件,添加如下代码到文件开头
import os import sys root = os.path.dirname(__file__) sys.path.insert(0, os.path.join(root, 'site-packages'))这样就安装完毕了
def qiniufetch(url,bucket,filename): """调用七牛的fetch API 将url的图片存储到七牛""" from base64 import urlsafe_b64encode as b64e from qiniu.auth import digest access_key = "****************************************" secret_key = "****************************************" encoded_url = b64e(url) dest_entry = "%s:%s" % (bucket, filename) encoded_entry = b64e(dest_entry.encode('utf-8')) api_host = "iovip.qbox.me" api_path = "/fetch/%s/to/%s" % (encoded_url, encoded_entry) mac = digest.Mac(access=access_key, secret=secret_key) client = digest.Client(host=api_host, mac=mac) ret, err = client.call(path=api_path) if err is not None: print "Fetch image file\"%s\" failed" % url print err return None else: print "Fetch \"%s\" to qiniu \"%s\" success!" % (url,dest_entry) return "http://%s.qiniudn.com/%s" % (bucket,urllib.quote(filename.encode('utf-8')))
if info['avatr_src']: if avatar_urlfactory: info['avatr_src'] = avatar_urlfactory(info['avatr_src']) if saveto == 'qiniu': filename = "%s/%s-%s.%s" % (prefix, info['name'], todaystr, info['avatr_src'].split('.')[-1]) info["avatar"] = qiniufetch(info['avatr_src'],'akb-member',filename) elif saveto == 'storage': filename = "%s/%s-%s.%s" % (prefix, info['name'], todaystr, info['avatr_src'].split('.')[-1]) info["avatar"] = retrieve(info['avatr_src'],'avatar',filename)
原文地址:http://blog.csdn.net/cugbabybear/article/details/37955891