I have running servlet on google app engine that receives image in byte[] format. I want to save this image on google cloud storage using POST request.
To get the idea here is some code that is not working but is in the right direction:
public void postReq( byte[] image) {
HttpClient httpclient = new DefaultHttpClient();
HttpPost httpPost = new HttpPost(getUploadUrl());
FileBody uploadFilePart = new FileBody(?????, "image/jpg");
MultipartEntity reqEntity = new MultipartEntity();
reqEntity.addPart("upload-file", uploadFilePart);
httpPost.setEntity(reqEntity);
HttpResponse response = httpclient.execute(httpPost);
}
public String getUploadUrl() {
BlobstoreService blobstoreService = BlobstoreServiceFactory.getBlobstoreService();
UploadOptions uploadOptions = null;
String bucket = Constants.BUCKET_NAME;
uploadOptions = UploadOptions.Builder.withGoogleStorageBucketName(bucket);
return blobstoreService.createUploadUrl("/upload", uploadOptions);
}
The org.apache.http.impl.client.DefaultHttpClient; is deprecated so obviously is not the right way. Some things that I found in the internet but I couldn't manage to run them: http://knowledge-serve.blogspot.com/2012/09/upload-file-on-google-cloud-storage.html Uploading files - Using java.net.URLConnection to fire and handle HTTP requests
Google made some very good tutorial but is for uploading image from html page: https://developers.google.com/cloud/samples/photofeed/servephotos
The goal is to make POST request sending multipart/data to google and then google returns request to my GAE Servlet in this case to "/upload". So I should do the same as this but in servlet and from byte[] image:
<form action="<%=serviceManager.getUploadUrl()%>" method="post" enctype="multipart/form-data">
<input id="input-file" class="inactive file btn" type="file" name="photo" onchange="onFileSelected()">
<textarea name="title" placeholder="Write a description"></textarea>
<input id="btn-post" class="active btn" type="submit" value="Post">
<a class="cancel" onclick="togglePhotoPost(false)">Cancel</a>
</form>