Python Web框架Flask中使用新浪SAE云存储实例

WBOY
Release: 2016-06-10 15:17:56
Original
1105 people have browsed it

对于部署在新浪应用引擎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)
Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template