Google Cloud Platform (GCP) 有一个示例 Bookshelf tutorial,它展示了如何使用 Python 中的 Flask 框架在 Cloud Storage 上存储持久数据。以下是有关如何创建、读取、更新和删除 (CRUD) 存储在 Cloud Storage 中的数据的示例。
创建:
@crud.route('/add', methods=['GET', 'POST'])
def add():
if request.method == 'POST':
data = request.form.to_dict(flat=True)
book = get_model().create(data)
return redirect(url_for('.view', id=book['id']))
return render_template("form.html", action="Add", book={})
阅读:
@crud.route("/")
def list():
token = request.args.get('page_token', None)
if token:
token = token.encode('utf-8')
books, next_page_token = get_model().list(cursor=token)
return render_template(
"list.html",
books=books,
next_page_token=next_page_token)
更新:
@crud.route('/<id>/edit', methods=['GET', 'POST'])
def edit(id):
book = get_model().read(id)
if request.method == 'POST':
data = request.form.to_dict(flat=True)
book = get_model().update(data, id)
return redirect(url_for('.view', id=book['id']))
return render_template("form.html", action="Edit", book=book)
删除:
@crud.route('/<id>/delete')
def delete(id):
get_model().delete(id)
return redirect(url_for('.list'))
您可以找到有关 GCP JSON API 参考here 的更多详细信息。