如何操纵谷歌应用程序引擎数据存储中的文件 [英] How to manipulate files in google app engine datastore

查看:134
本文介绍了如何操纵谷歌应用程序引擎数据存储中的文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的问题围绕着一个用户将文本文件上传到我的应用程序。我需要获取这个文件,并在将其保存到数据存储之前使用我的应用程序处理它。从我读过的小部分,我明白,用户上传直接到数据存储blob,这是好的,如果我可以然后获得该文件,对其执行操作(意味着更改数据里面),然后重新写回到数据存储。所有这些操作都需要由应用程序完成。
不幸的是,从数据存储文档中, http://code.google .com / appengine / docs / python / blobstore / overview.html
应用程序无法在数据存储中直接创建Blob。这是我的头痛。我只需要在应用程序中从我的应用程序中创建一个新的blob /文件,而无需任何用户上传交互。解决方案

您的帮助。经过许多不眠之夜,3个App Engine书籍和大量的Google搜索,我找到了答案。这里是代码(它应该是非常自我解释):

  from __future__ import with_statement 
from google.appengine。 api从google.appengine.ext导入文件
从google.appengine.ext导入blobstore
从google.appengine.ext.webapp导入webapp
导入util

class MainHandler(webapp.RequestHandler):
def get(self):
self.response.out.write('Hello WOrld')
form ='''< form action =/ method =POSTenctype =multipart / form-data>
上传档案:< input type =filename =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)
printBlob Key =
print blob_key

def main():
application = webapp.WSGIApplicatio n([('/',MainHandler)],debug = True)
util.run_wsgi_app(应用程序)
$ b $如果__name __ =='__ main__':
main()


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.

解决方案

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

这篇关于如何操纵谷歌应用程序引擎数据存储中的文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆