pythonweb框架flask中使用七牛云存储实例

对于小型站点,使用七牛云存储的免费配额已足够为站点提供稳定、快速的存储服务

七牛云存储已有python sdk,对它进行简单封装后,就可以直接在flask中使用了,项目代码见github上flask-qiniustorage。

使用示例代码:

代码如下:

from flask import flask
from flask_qiniustorage import qiniu

qiniu_access_key = ‘七牛 access key’
qiniu_secret_key = ‘七牛 secret key’
qiniu_bucket_name = ‘七牛空间名称’
qiniu_bucket_domain = ‘七牛空间对应域名’

app = flask(__name__)
app.config.from_object(__name__)
qiniu_store = qiniu(app)
# 或者
# qiniu_store = qiniu()
# qiniu_store.init_app(app)

# 保存文件到七牛
@app.route(‘/save’)
def save():
data = ‘data to save’
filename = ‘filename’
ret, info = qiniu_store.save(data, filename)
return str(ret)

# 删除七牛空间中的文件
@app.route(‘/delete’)
def delete():
filename = ‘filename’
ret, info = qiniu_store.delete(filename)
return str(ret)

# 根据文件名获取对应的公开url
@app.route(‘/url’)
def url():
filename = ‘filename’
return qiniu_store.url(filename)

Posted in 未分类

发表评论