对于部署在新浪应用引擎SAE上的项目,使用新浪SAE云存储是不错的存储方案。
新浪SAE云存储仅能在SAE环境中正常使用,对它进行简单封装后,可以直接在Flask中使用,项目代码见GitHub上Flask-SaeStorage。
使用示例代码:
from flask import Flask from flask_saestorage import SaeStorage SAE_ACCESS_KEY = 'SAE Access Key' SAE_SECRET_KEY = 'SAE Secret Key' SAE_APP_NAME = 'SAE App Name' SAE_BUCKET_NAME = 'SAE Bucket Name' app = Flask(__name__) app.config.from_object(__name__) sae_storage = SaeStorage(app) # 或者 # sae_storage = SaeStorage() # sae_storage.init_app(app) # 保存文件到SAE Storage @app.route('/save') def save(): data = 'data to save' filename = 'filename' ret = sae_storage.save(data, filename) return str(ret) # 删除SAE Storage中的文件 @app.route('/delete') def delete(): filename = 'filename' ret = sae_storage.delete(filename) return str(ret) # 根据文件名获取对应的公开URL @app.route('/url') def url(): filename = 'filename' return sae_storage.url(filename)