如何操作谷歌应用程序引擎数据存储中的文件

发布于 2024-11-13 12:01:28 字数 433 浏览 5 评论 0原文

我的问题围绕用户将文本文件上传到我的应用程序。我需要获取此文件并使用我的应用程序对其进行处理,然后再将其保存到数据存储区。从我所读到的一点来看,我了解到用户上传作为 blob 直接进入数据存储,如果我可以获取该文件,对其执行操作(意味着更改内部数据),然后将其重新写回,那就可以了数据存储。所有这些操作都需要由应用程序来完成。 遗憾的是,根据数据存储区文档,http://code.google.com/appengine /docs/python/blobstore/overview.html 应用程序无法直接在数据存储中创建 blob。这是我最头疼的问题。我只需要一种从我的应用程序在数据存储中创建新的 blob/文件的方法,而无需任何用户上传交互。

My problem revolves around a user making a text file upload to my app. I need to get this file and process it with my app before saving it to the datastore. From the little I have read, I understand that user uploads go directly to the datastore as blobs, which is ok if I could then get that file, perform operations on it(meaning change data inside) and then re-write it back to the datastore. All these operations need to be done by the app.
Unfortunately from the datastore documenation, http://code.google.com/appengine/docs/python/blobstore/overview.html
an app cannot directly create a blob in the datastore. That's my main headache. I simply need a way of creating a new blob/file in the datastore from my app without any user upload interaction.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

厌味 2024-11-20 12:01:28

blobstore != 数据存储

您可以根据需要在数据存储区中读取和写入数据,只要您使用db.BlobProperty 在您的实体上。

正如 Wooble 评论的那样,新的文件 API 可让您编写到blobstore,但除非您使用任务或类似mapreduce库的东西增量写入blobstore文件,否则您仍然受到1MB API调用限制用于阅读/写作。

blobstore != datastore.

You can read and write data to the datastore as much as you like so long as your data is <1MB using a db.BlobProperty on your entity.

As Wooble comments, the new File API lets you write to the blobstore, but unless you are incrementally writting to the blobstore-file using tasks or something like the mapreduce library you are still limited by the 1MB API call limit for reading/writing.

咋地 2024-11-20 12:01:28

感谢您的帮助。经过许多个不眠之夜、三本 App Engine 书籍和大量谷歌搜索后,我找到了答案。这是代码(它应该是非常不言自明的):

from __future__ import with_statement
from google.appengine.api import files
from google.appengine.ext import blobstore
from google.appengine.ext import webapp
from google.appengine.ext.webapp import util

class MainHandler(webapp.RequestHandler):
    def get(self):
        self.response.out.write('Hello WOrld')
        form=''' <form action="/" method="POST" enctype="multipart/form-data">
Upload File:<input type="file" name="file"><br/>
<input type="submit"></form>'''
        self.response.out.write(form)
        blob_key="w0MC_7MnZ6DyZFvGjgdgrg=="
        blob_info=blobstore.BlobInfo.get(blob_key)
        start=0
        end=blobstore.MAX_BLOB_FETCH_SIZE-1
        read_content=blobstore.fetch_data(blob_key, start, end)
        self.response.out.write(read_content)
    def post(self):
        self.response.out.write('Posting...')
        content=self.request.get('file')
        #self.response.out.write(content)
        #print content
        file_name=files.blobstore.create(mime_type='application/octet-stream')
        with files.open(file_name, 'a') as f:
            f.write(content)
        files.finalize(file_name)
        blob_key=files.blobstore.get_blob_key(file_name)
        print "Blob Key="
        print blob_key

def main():
    application=webapp.WSGIApplication([('/', MainHandler)],debug=True)
    util.run_wsgi_app(application)

if __name__=='__main__':
    main()

Thanks for your help. After many sleepless nights, 3 App Engine Books and A LOT of Googling, I've found the answer. Here is the code (it should be pretty self explanatory):

from __future__ import with_statement
from google.appengine.api import files
from google.appengine.ext import blobstore
from google.appengine.ext import webapp
from google.appengine.ext.webapp import util

class MainHandler(webapp.RequestHandler):
    def get(self):
        self.response.out.write('Hello WOrld')
        form=''' <form action="/" method="POST" enctype="multipart/form-data">
Upload File:<input type="file" name="file"><br/>
<input type="submit"></form>'''
        self.response.out.write(form)
        blob_key="w0MC_7MnZ6DyZFvGjgdgrg=="
        blob_info=blobstore.BlobInfo.get(blob_key)
        start=0
        end=blobstore.MAX_BLOB_FETCH_SIZE-1
        read_content=blobstore.fetch_data(blob_key, start, end)
        self.response.out.write(read_content)
    def post(self):
        self.response.out.write('Posting...')
        content=self.request.get('file')
        #self.response.out.write(content)
        #print content
        file_name=files.blobstore.create(mime_type='application/octet-stream')
        with files.open(file_name, 'a') as f:
            f.write(content)
        files.finalize(file_name)
        blob_key=files.blobstore.get_blob_key(file_name)
        print "Blob Key="
        print blob_key

def main():
    application=webapp.WSGIApplication([('/', MainHandler)],debug=True)
    util.run_wsgi_app(application)

if __name__=='__main__':
    main()
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文