服务通过应用程序引擎的端点API Blob存储图像 [英] serving blobstore image through app-engine endpoints api
问题描述
我建立一个应用程序引擎的端点的API,它需要的画面从用户(Android应用程序),并将其保存到Blob存储编程。然后,我保存在我的数据存储在blob_key。在code是这样的:
I am building an app-engine endpoint api that takes picture from a user (android app) and saves it to blobstore programmatically. Then I save the blob_key in my datastore. The code goes like this:
-
首先,我通过接收到的图像我的
@ endpoint.method
为messages.BytesField
:
= IMAGE_DATA messages.BytesField(1,必需= TRUE)
image_data = messages.BytesField(1, required=True)
然后,我保存到这样的Blob存储:
Then I save to the blobstore like this:
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
现在我想为图像了。我不知道如何适应以下code到我的终端API:
Now I want to serve the image back. I don't see how to fit the following code into my endpoints 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)
在最后,我想像这样的一个服务程序:
In the end I imagine a serving procedure like this:
-
在@ endpoints.method:
in @endpoints.method:
获取blob_key从数据存储
get blob_key from datastore
获取图像blob_key
obtain image with blob_key
图像添加到StuffResponseMessage
add image to StuffResponseMessage
发送StuffResponseMessage到前端(Android应用程序)
send StuffResponseMessage to front-end (android app)
我的做法是,因为我要保护我的用户的隐私。就如何做好这一点有什么想法? 我的code段一般由谷歌开发者教程。
My approach is because I want to protect the privacy of my users. Any thoughts on how to do this well? My code snippets are generally from the google developer tutorial.
编辑:
我不明白我怎么会通过blob_key从数据存储到下面的方法来获取图像:
I don't see how I would pass the blob_key from the datastore to the following method to retrieve the image:
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)
什么是资源
啊?
推荐答案
我相信资源
是 BlobKey
对象你想成为,作为字符串,从URL路径retreived什么。如果你看看 google.appengine.ext.blobstore.BlobInfo
的 GET
方法使用功能<$源C $ C> __ normalize_and_convert_keys ,它利用一个参数 BlobKey
对象或字符串。
I believe resource
is the BlobKey
object of what you want to serve, as a string, retreived from url path. If you look at the source of google.appengine.ext.blobstore.BlobInfo
the get
method uses a function __normalize_and_convert_keys
that takes an argument of BlobKey
object OR string.
如果BLOB是图像,也许这是最好送服务的URL,而不是到你的Android应用,在 StuffResponseMessage
也许在你的情况。从谷歌的服务的Blob DOC:
If the blob is an image, maybe it's best to send the serving url instead to your Android app, in StuffResponseMessage
maybe in your case. From google's Serving a Blob doc:
如果您是服务的图像,更有效的和潜在的较便宜的方法是使用的使用App Engine的图像API,而不是send_blob get_serving_url 。该get_serving_url功能可让您直接服务于图像,而不必通过您的App Engine的实例。
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(数据)
以上)做一个服务的URL,然后在你的Android应用获取图像从返回的URL。这当然意味着暴露的图片网址没有隐私保护。
So after you save the image, take the returned blob_key
(as per your method save_image(data)
above) to make a serving url, then in your Android app get the image from the returned url. That of course means exposing the image url without privacy protection.
如果你想与保护做到这一点,使用 BlobReader
的类用类似的界面文件中读取的斑点。不能从服务的Blob的例子使用的方法/类,因为你做了 remote.Service
的子类,而不是一个处理程序。
If you want to do it with protection, use BlobReader
class to read the blob with file like interface. You can't use the method/class from Serving a Blob example because you do it in a remote.Service
subclass rather than a handler.
这篇关于服务通过应用程序引擎的端点API Blob存储图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!