对于部署在百度应用引擎bae上的项目,使用百度云存储bcs(baidu cloud storage)是不错的存储方案。
百度云存储已有python sdk,对它进行简单封装后,就可以直接在flask中使用了,项目代码见github上flask-bcs。
使用示例代码:
代码如下:
from flask import flask
from flask_bcs import bcs
bcs_host = ‘bcs host’
bcs_access_key = ‘bcs access key’
bcs_secret_key = ‘bcs secret key’
bcs_bucket_name = ‘bcs bucket name’
app = flask(__name__)
app.config.from_object(__name__)
bcs = bcs(app)
# 或者
# bcs = bcs()
# bcs.init_app(app)
# 保存文件到bcs
@app.route(‘/save’)
def save():
data = ‘data to save’
filename = ‘filename’
allow_referers = [‘http://*.duapp.com/*’, ‘http://zhangxc.com/*’]
# 不设置allow_referers为完全public
ret = bcs.save(data, filename, allow_referers)
return str(ret)
# 删除bcs中的文件
@app.route(‘/delete’)
def delete():
filename = ‘filename’
ret = bcs.delete(filename)
return str(ret)
# 根据文件名获取对应的公开url
@app.route(‘/url’)
def url():
filename = ‘filename’
return bcs.url(filename)