gpt4 book ai didi

python - GAE - 将图像从移动设备上传到 blobstore

转载 作者:行者123 更新时间:2023-11-28 17:50:08 25 4
gpt4 key购买 nike

我已经能够使用提供的代码成功地将图像上传到我的 Google App Engine Blobstore here (在其中一条评论中,主要基于 GAE 文档)。

完整代码供引用:

import os
import urllib

from google.appengine.ext import blobstore
from google.appengine.ext import webapp
from google.appengine.ext.webapp import blobstore_handlers
from google.appengine.ext.webapp import template
from google.appengine.ext.webapp.util import run_wsgi_app

class MainHandler(webapp.RequestHandler):
def get(self):
upload_url = blobstore.create_upload_url('/upload')
self.response.out.write('<html><body>')
self.response.out.write('<form action="%s" method="POST" enctype="multipart/form-data">' % upload_url)
self.response.out.write("""Upload File: <input type="file" name="file"><br> <input type="submit" name="submit" value="Submit"> </form></body></html>""")

for b in blobstore.BlobInfo.all():
self.response.out.write('<li><a href="/serve/%s' % str(b.key()) + '">' + str(b.filename) + '</a>')

class UploadHandler(blobstore_handlers.BlobstoreUploadHandler):
def post(self):
upload_files = self.get_uploads('file')
blob_info = upload_files[0]
self.redirect('/')

class ServeHandler(blobstore_handlers.BlobstoreDownloadHandler):
def get(self, blob_key):
blob_key = str(urllib.unquote(blob_key))
if not blobstore.get(blob_key):
self.error(404)
else:
self.send_blob(blobstore.BlobInfo.get(blob_key), save_as=True)

def main():
application = webapp.WSGIApplication(
[('/', MainHandler),
('/upload', UploadHandler),
('/serve/([^/]+)?', ServeHandler),
], debug=True)
run_wsgi_app(application)

if __name__ == '__main__':
main()

但是,此代码要求在包含图像数据的 POST 请求之前,根据 GET 请求创建上传 url:

def get(self):
upload_url = blobstore.create_upload_url('/upload')

当我尝试从移动设备发送图像时,我想将服务器代码推送到 def post(self): 函数下的单个代码块中,然后我'我在做这件事时遇到了麻烦。

将上面的行移动到 def post(self): 代码似乎并不能解决问题。

有什么想法吗?

干杯!布雷特

最佳答案

是的,您可以使用 urlfetch 使其成为一个单独的代码块。这是我的方法:

class UploadHandler(blobstore_handlers.BlobstoreUploadHandler):
def post(self):
upload_files = self.get_uploads('file')
if len(upload_files) > 0 :
blob_info = upload_files[0]
self.response.write(str(blob_info.key()))
else:
self.error(404)

class SomeHandler(webapp.RequestHandler):
def post(self):
file = self.request.POST.get('file')
if (file is not None):
# Use urlfetch to call to the blob upload url and get result
# Just copy the same request body and header and pass to UploadHandler here
result = urlfetch.fetch(
url= blobstore.create_upload_url('/upload'),
payload=self.request.body,
method=urlfetch.POST,
headers=self.request.headers)
if result.status_code == 200:
blob_key_str = result.content
# Get blob key
blob_key = blobstore.BlobKey(blob_key_str)
# Maybe a url for the file
blob_url = images.get_serving_url(blob_key_str, 400)

这样你就可以随时将文件上传到 SomeHandler 而不是必须先获取上传 URL 然后再上传。

希望对您有所帮助!

关于python - GAE - 将图像从移动设备上传到 blobstore,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11850263/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com