简体   繁体   English

使用Python在Google Cloud Storage上读写JSON文件

[英]Reading & Writing JSON file on Google Cloud Storage using Python

As said, I have a JSON file on a Cloud Storage bucket, is there a way to read (and modify) its content via Python? 如前所述,我在Cloud Storage存储桶上有一个JSON文件,是否可以通过Python读取(和修改)其内容?

@app.route("/myform/", methods=('GET', 'POST'))
def myform():
    form = MyForm()
    if form.validate_on_submit():
       return redirect('/')
    return render_template('my_form.html', form=form)

I would like to read a JSON on Google Cloud Storage and add to it the value defined by the form (a key and its value). 我想在Google Cloud Storage上读取JSON,并向其中添加表单定义的值(键及其值)。

This is all on a Flask webapp running on a standard AppEngine. 所有这些都在标准AppEngine上运行的Flask Web应用程序上进行。

Google Cloud Platform (GCP) has a sample Bookshelf tutorial that shows how to store persistent data on Cloud Storage using Flask framework in Python. Google Cloud Platform(GCP)有一个示例书架教程 ,该教程展示了如何使用Python中的Flask框架在Cloud Storage上存储持久性数据。 Here is an example on how to create, read, update, and delete (CRUD) data stored in Cloud Storage. 这是有关如何创建,读取,更新和删除Cloud Storage中存储的数据的示例。

Create: 创造:

@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={})

Read: 读:

@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)

Update: 更新:

@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)

Delete: 删除:

@crud.route('/<id>/delete')
def delete(id):
    get_model().delete(id)
    return redirect(url_for('.list'))

You can find more details on GCPs JSON API reference here . 您可以在此处找到有关GCP JSON API参考的更多详细信息。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM