對於部署在新浪應用引擎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)