I currently have the following problem:
I would like to upload a Blob to the Google Blobstore from my client Java application. In the Blobstore documentation (https://cloud.google.com/appengine/docs/java/blobstore/) is described how to do this with a form with which the Blob is uploaded by using a FileChooser.
However, I would like to do this from my code, but I do not know how to convert the Object to a Blob in Java. I would like to pass the generated Blob in a HttpURLConnection, and I have created the classes for this on the Google App Engine.
So my question is: how can I upload a Blob from a client java application?
Note: in my case I would like to upload a JavaFX Image as a Blob, but I think this question could be asked in general.
UPDATE 1: Using the FileService could be the solution, as @TejjD kindly pointed out. However, the Files API is going to be deprecated and is therefore not useful.
UPDATE 2: I am using the following code on the server:
@Override
public void doPost(HttpServletRequest req, HttpServletResponse res)
throws ServletException, IOException {
//Check if there is a parameter for the outfit id
try {
if (!req.getParameter("id").isEmpty() && Functions.isInteger(req.getParameter("id"))) {
//Check if the requested id exists in the Cloud SQL database
Connection conn = ConnectionManager.connectToCloudSQL();
GetQueryBuilder query = DataManager.executeGet(conn, req, "outfit");
//Check result
if (query.getResultSet() == null) {
throw new Exception("The Outfit object with the requested ID is not yet present in the Cloud SQL database");
}
Map<String,List<BlobKey>> blobs = blobstoreService.getUploads(req);
BlobKey blobKey = blobs.get("image").get(0);
if (blobKey == null) {
throw new Exception("No blobkey specified");
}
else {
//First create a checksum from the file
String checkSum = this.buildCheckSum(blobKey);
PersistenceManager pm = PMF.get().getPersistenceManager();
//Create new outfit object
int outfitId = Integer.parseInt(req.getParameter("id"));
OutfitExtension outfit = new OutfitExtension();
outfit.setId(outfitId);
outfit.setImage(blobKey);
outfit.setCheckSum(checkSum);
pm.makePersistent(outfit);
}
}
else {
throw new Exception("An outfit ID needs to be added to the HTTP request and should be from type Integer only.");
}
} catch (Exception e) {
res.sendError(HttpServletResponse.SC_BAD_REQUEST, "Bad request: "+e.getMessage());
}
}
This code is run when some URL is called (I have connected this in the web.xml).
As you can see in the code, it is about blobstoreService.getUploads(req). How can I post Blob directly in which it is recognized as a upload?