Upload image to GCS thorugh REST API / Endpoints

990 views
Skip to first unread message

Bradford Stephens

unread,
Feb 21, 2014, 3:02:10 PM2/21/14
to google-a...@googlegroups.com
Hi there!

I can't find an example of this that strictly uses REST APIs, so I'm asking you all. I'm building a mobile photo-sharing app, with java on the server-side.

I want to:

-Upload an image to a REST API (preferably served by Endpoints) -- no browser
-Store that image in Google Cloud Storage
-Retrieve that image's URL and store it in datastore

The first two have me really puzzled...any example code out there?

Alejandro Gonzalez

unread,
Feb 26, 2014, 3:23:51 AM2/26/14
to google-a...@googlegroups.com
This is basically the code:


com.google.appengine.api.images.Image image = YOU_IMAGE;
GcsService gcsService = GcsServiceFactory.createGcsService();
BlobstoreService blobstoreService = BlobstoreServiceFactory.getBlobstoreService();

GcsFileOptions options = new GcsFileOptions.Builder()
                    .mimeType(p_contentType)
                    .acl("project-private")
                    .build();

GcsFilename p_filename = new GcsFilename(bucketName, secureFilename);
GcsOutputChannel writeChannel = gcsService.createOrReplace(p_filename, options);
writeChannel.write(ByteBuffer.wrap(image.getImageData()));
writeChannel.close();

BlobKey blobKey = blobstoreService.createGsBlobKey("/gs/" + p_filename.getBucketName() + "/" + p_filename.getObjectName());

ImagesService imageService = ImagesServiceFactory.getImagesService();
ServingUrlOptions servoptions = ServingUrlOptions.Builder.withBlobKey(blobKey).secureUrl(true);
String url = imageService.getServingUrl(servoptions);




--
You received this message because you are subscribed to the Google Groups "Google App Engine" group.
To unsubscribe from this group and stop receiving emails from it, send an email to google-appengi...@googlegroups.com.
To post to this group, send email to google-a...@googlegroups.com.
Visit this group at http://groups.google.com/group/google-appengine.
For more options, visit https://groups.google.com/groups/opt_out.

Vinny P

unread,
Feb 26, 2014, 2:13:52 PM2/26/14
to google-a...@googlegroups.com
On Fri, Feb 21, 2014 at 2:02 PM, Bradford Stephens <bradford...@gmail.com> wrote:
I want to:
-Upload an image to a REST API (preferably served by Endpoints) -- no browser
-Store that image in Google Cloud Storage
The first two have me really puzzled...any example code out there?


Endpoints doesn't accept the multipart/form-data encoding ( http://stackoverflow.com/a/15440083 ) so you can't upload the image directly to Endpoints. You'll have to configure a separate servlet to handle the file upload; Alejandro's code will handle that.

On the client side, you can use the standard HttpUrlConnection to open the upload URL and upload the file via DataOutputStream.
 
 
-----------------
-Vinny P
Technology & Media Advisor
Chicago, IL

App Engine Code Samples: http://www.learntogoogleit.com

Reply all
Reply to author
Forward
0 new messages