Home  >  Article  >  Backend Development  >  Python Web框架Flask中使用百度云存储BCS实例

Python Web框架Flask中使用百度云存储BCS实例

WBOY
WBOYOriginal
2016-06-06 11:21:191537browse

对于部署在百度应用引擎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)

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn