简体   繁体   English

如何从Android上传文件(图片)并使用GAE和Google端点保存到Blobstore?

[英]How do I upload a file(picture) from android and save to the blobstore using GAE and google endpoints?

Sorry if this is a little general. 抱歉,这有点笼统。 Hopfully I can get pointed in the correct direction. 希望我能指出正确的方向。

I have an android app that includes instant messaging. 我有一个包含即时通讯的android应用。 I want to add the functionality to send photos in messages as well. 我也想添加功能,以便在邮件中发送照片。 My backend is built on GAE and cloud endpoints and is written in Java. 我的后端建立在GAE和云端点上,并用Java编写。

So far, I've looked into google cloud storage, the blobstore, java servlet pages, etc. But a clear solution (or example using endpoints) has been impossible to find. 到目前为止,我已经研究了Google云存储,blobstore,java servlet页面等。但是找不到一个明确的解决方案(或使用端点的示例)。

So, as the question states, how can I send and serve images from the blobstore using android and GAE endpoint backend? 因此,正如问题所指出的,如何使用android和GAE端点后端从blobstore发送并提供图片?

Edit: This question is only regarding the back end. 编辑:此问题仅与后端有关。 In android, I can do the http post easily enough. 在android中,我可以轻松完成http发布。 I'm just lost when it comes to doing this in endpoints 在端点上执行此操作时,我只是迷路了

I suggest the following approach to implement what you require: 我建议采用以下方法来实现您的要求:

  • Pass the image data as base64 encoded text from your client. 从客户端将图像数据作为base64编码的文本传递。
  • On the server side, you can extract out the image content and then use one of many options that are available, which include Blobstore, Google Cloud Storage, etc. I suggest that you go with Google Cloud Storage because that is the recommended approach. 在服务器端,您可以提取图像内容,然后使用许多可用选项之一,包括Blobstore,Google Cloud Storage等。我建议您使用Google Cloud Storage,因为这是推荐的方法。 If you prefer the Datastore, keep in mind that the data is limited to 1 MB in size, so you might hit that limit depending on the size of images that you are dealing with. 如果您喜欢数据存储,请记住数据大小限制为1 MB,因此您可能会达到该限制,具体取决于要处理的图像大小。
  • A SO question and answer here contains lot of relevant code that you could utilize: Sending images as a base64 string to a google cloud endpoint from cms 一个SO问题和答案在此处包含许多您可以利用的相关代码: 从cms将图像作为base64字符串发送到Google云端点

如果您已经在使用端点,则可以按照@Romin的建议,将图像作为Base64发送,或者使用blobstore / GCS服务分两步上传图像,首先(通过端点)获取Upload URL,然后将图像发布到该端点URL。

Why not using a simple HTTP POST to handler for image upload? 为什么不使用简单的HTTP POST处理程序进行图像上传?

When I use the blobstore I typically have a pair of handlers one for uploading and one for serving images. 当我使用Blobstore时,通常会有一对处理程序,一个用于上传,一个用于提供图像。

So, in your case I'll rather do a post to a URL sending the image file and some parameter than can link it to the proper place in the datastore (ie user ID). 因此,在您的情况下,我宁愿发布到发送图像文件和某些参数的URL上,也不愿将其链接到数据存储中的正确位置(即用户ID)。

Then, using endpoints I'll have the same linking key and display the image using the serving handler with the given parameter. 然后,使用端点,我将具有相同的链接键,并使用具有给定参数的服务处理程序来显示图像。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM