Android 通过应用程序引擎端点api提供blobstore映像

Android 通过应用程序引擎端点api提供blobstore映像,android,google-app-engine,python-2.7,blobstore,google-cloud-endpoints,Android,Google App Engine,Python 2.7,Blobstore,Google Cloud Endpoints,我正在构建一个应用程序引擎端点api,它可以从用户(android应用程序)获取图片,并以编程方式将其保存到blobstore。然后我将blob_键保存在数据存储中。代码如下所示: from google.appengine.api import files def save_image(data): # Create the file file_name = files.blobstore.create(mime_type='image/png') # Open the fil

我正在构建一个应用程序引擎端点api,它可以从用户(android应用程序)获取图片,并以编程方式将其保存到blobstore。然后我将blob_键保存在数据存储中。代码如下所示:

from google.appengine.api import files

def save_image(data):
  # Create the file
  file_name = files.blobstore.create(mime_type='image/png')

  # Open the file and write to it
  with files.open(file_name, 'a') as f:
    f.write('data')

  # Finalize the file. Do this before attempting to read it.
  files.finalize(file_name)

  # Get the file's blob key
  blob_key = files.blobstore.get_blob_key(file_name)
  return blob_key # which is then saved to datastore
  • 首先,我通过我的
    @endpoint.method
    消息的形式接收图像。BytesField

    image_data=messages.BytesField(1,必需=True)

然后我保存到blobstore,如下所示:

from google.appengine.api import files

def save_image(data):
  # Create the file
  file_name = files.blobstore.create(mime_type='image/png')

  # Open the file and write to it
  with files.open(file_name, 'a') as f:
    f.write('data')

  # Finalize the file. Do this before attempting to read it.
  files.finalize(file_name)

  # Get the file's blob key
  blob_key = files.blobstore.get_blob_key(file_name)
  return blob_key # which is then saved to datastore
现在我想把这张照片还给你。我看不出如何将以下代码适合我的端点api:

from google.appengine.ext import blobstore
from google.appengine.ext.webapp import blobstore_handlers
class ServeHandler(blobstore_handlers.BlobstoreDownloadHandler):
  def get(self, resource):
    resource = str(urllib.unquote(resource))
    blob_info = blobstore.BlobInfo.get(resource)
    self.send_blob(blob_info)
最后,我想象一个这样的发球过程:

from google.appengine.api import files

def save_image(data):
  # Create the file
  file_name = files.blobstore.create(mime_type='image/png')

  # Open the file and write to it
  with files.open(file_name, 'a') as f:
    f.write('data')

  # Finalize the file. Do this before attempting to read it.
  files.finalize(file_name)

  # Get the file's blob key
  blob_key = files.blobstore.get_blob_key(file_name)
  return blob_key # which is then saved to datastore
  • 在@endpoints.method中:

  • 从数据存储中获取blob_密钥

  • 使用blob_键获取图像

  • 将图像添加到StuffResponseMessage

  • 将StuffResponseMessage发送到前端(android应用程序)

我的方法是因为我想保护我的用户的隐私。关于如何做好这件事有什么想法吗


编辑:

我不知道如何将blob_键从数据存储传递到以下方法来检索图像:

from google.appengine.ext import blobstore
from google.appengine.ext.webapp import blobstore_handlers
class ServeHandler(blobstore_handlers.BlobstoreDownloadHandler):
  def get(self, resource):
    resource = str(urllib.unquote(resource))
    blob_info = blobstore.BlobInfo.get(resource)
    self.send_blob(blob_info)

resource
中到底有什么?

我相信
resource
是从url路径中检索的
BlobKey
对象,它作为字符串提供服务。如果您查看
google.appengine.ext.blobstore.BlobInfo
的源代码,那么
get
方法使用一个函数
\uuuuuuunormalize\u和\uconvert\u key
,该函数的参数为
BlobKey
对象或字符串

如果blob是一个图像,那么最好将服务url发送到您的Android应用程序,在
StuffResponseMessage
中,可能在您的情况下。来自谷歌的Blob文档:

如果您正在提供图像服务,一种更高效且可能更便宜的方法是使用appengineimagesapi,而不是send_blob。get_serving_url函数允许您直接为图像提供服务,而无需查看应用程序引擎实例

因此,保存图像后,使用返回的
blob_键
(按照上面的方法
save_image(data)
)创建一个服务url,然后在Android应用程序中从返回的url获取图像。这当然意味着在没有隐私保护的情况下公开图像url


如果您想使用保护功能,请使用
BlobReader
读取具有类似文件界面的blob。您不能通过为Blob示例提供服务来使用方法/类,因为您是在
remote.Service
子类中而不是在处理程序中使用该方法/类。

如果您使用的是Python 2.7.ah,则不需要
from\uuuuuuuuuuuuuuuuu-import with\u语句。谢谢,@bossylobster。我把它拿了出来。要从端点(服务器)向android发送blob,我需要对它进行编码吗?(Base64)或者已经处理了吗?@learner给定服务url,您可以像处理任何其他外部图像一样处理它。看到这个答案了吗