时间:2021-07-01 10:21:17 帮助过:49人阅读
百度云存储已有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)