I'm trying to upload image file to Blobstore using Google Cloud Endpoints. The code below successfully stores image in Blobstore but doesn't create the Entity "Image" in Datastore and so it doesn't add the Image Reference to the MyUser Entity (I use Objectify to store entities in Datastore).
Plus when i upload an image i get "404 NOT_FOUND" error as response...but the image is correctly stored in the Blobstore.
<html>
<head>
<meta http-equiv="content-type" content="text/html;charset=utf-8">
<title>404 NOT_FOUND</title>
</head>
<body text=#000000 bgcolor=#ffffff>
<h1>Error: NOT_FOUND</h1>
</body>
</html>
My Endpoints Code
@Api(
name = "imageManager"
)
public class ImageManager {
BlobstoreService blobstoreService = BlobstoreServiceFactory.getBlobstoreService();
@ApiMethod(name = "urlToUpload", httpMethod = "get")
public Image urlToUpload(){
Image image = new Image();
image.url = blobstoreService.createUploadUrl("/imageManager/upload");
return image;
}
This is the upload handler:
@ApiMethod(name = "upload", httpMethod = "post")
public Image upload(HttpServletRequest httpServletRequest) throws BadRequestException, NotFoundException, UnauthorizedException {
String op = httpServletRequest.getParameter("op");
if(op == null) throw new BadRequestException("op is null");
Image image = null;
if(op.equalsTo("registerUser") image = this.uploadRegisterUser(httpServletRequest);
return image;
}
UploadRegisterUser:
private Image uploadRegisterUser(HttpServletRequest httpServletRequest) throws NotFoundException {
Long userId = Long.parseLong(httpServletRequest.getParameter("userId"));
MyUser user = ofy().load().type(MyUser.class).filter("id",userId).first().now();
if(user == null) throw new NotFoundException("User not found");
Ref<Image> imageRef = this.saveImage(httpServletRequest, wUser, null);
user.profileImage = imageRef;
ofy().save().entity(wUser).now();
return imageRef.getValue();
}
SaveImage:
private Ref<Image> saveImage(HttpServletRequest httpServletRequest, MyUser user, String label){
List<BlobKey> blobs = blobstoreService.getUploads(httpServletRequest).get("file");
Image image = new Image();
image.key = blobs.get(0).getKeyString();
image.user = Ref.create(user);
image.date = new Date();
image.label = label;
return Ref.create(ofy().save().entity(image).now());
}
Where i'm wrong? Thanks in advance.