android - 通过应用引擎端点 api 提供 blobstore 图像

标签 android google-app-engine python-2.7 blobstore google-cloud-endpoints

我正在构建一个应用程序引擎端点 api,它从用户(Android 应用程序)拍摄照片并以编程方式将其保存到 blobstore。然后我将 blob_key 保存在我的数据存储中。代码是这样的:

  • 首先,我通过 @endpoint.method 收到图像作为 messages.BytesField:

    image_data = messages.BytesField(1, required=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)

最后我想像这样的服务程序:

  • 在@endpoints.method 中:

  • 从数据存储中获取 blob_key

  • 通过blob_key获取图片

  • 将图像添加到 StuffResponseMessage

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

我的做法是因为我想保护我用户的隐私。关于如何做好这件事有什么想法吗? My code snippets are generally from the google developer tutorial .


编辑:

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

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 是您想要服务的 BlobKey 对象,作为一个字符串,从 url 路径中检索。如果您查看 google.appengine.ext.blobstore.BlobInfo 的源代码,get 方法使用一个函数 __normalize_and_convert_keys,该函数的参数为​​ BlobKey 对象或字符串。

如果 blob 是图像,也许最好将服务 url 发送到您的 Android 应用程序,在 StuffResponseMessage 中可能适合您的情况。来自谷歌的 Serving a Blob 文档:

If you are serving images, a more efficient and potentially less-expensive method is to use get_serving_url using the App Engine Images API rather than send_blob. The get_serving_url function lets you serve the image directly, without having to go through your App Engine instances.

因此,在您保存图像后,使用返回的 blob_key(按照您上面的方法 save_image(data))制作一个服务 url,然后在您的 Android 应用中从返回的 url 中获取图像。这当然意味着在没有隐私保护的情况下暴露图片 url。

如果你想保护它,使用BlobReader class使用类似界面的文件读取 blob。您不能使用服务 Blob 示例中的方法/类,因为您是在 remote.Service 子类而不是处理程序中执行的。

关于android - 通过应用引擎端点 api 提供 blobstore 图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15562771/

相关文章:

php - Android 多部分图像上传问题

java - AsyncTask 什么时候 onPostExecute() 不被调用?

python - webapp2 请求参数/处理程序参数的自动转换?

python - cx_Oracle 在连接字符串上使用 SID 而不是服务名称时不连接

android - 使用 SMS 验证设备的电话号码

android - 无法启动 Activity ComponentInfo : NullPointerException

java - 如何从 eclipse 运行配置中指定 appengine devserver 端口?

python - 使用谷歌应用程序从字符串中检索字符

python - 对 App Engine 数据库的查询显示为空白

python - 如何从 url 获取 int 值