如何操纵谷歌应用程序引擎数据存储中的文件 [英] How to manipulate files in google app engine datastore
问题描述
不幸的是,从数据存储文档中, 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屋!