0
votes

I have a question about the Google Cloud Datastore.

I set up an application in Java that writes and retrives entities from the datastore, but I'm not able to write image or binary data inside it.

The google docs says that is this (it is the only missing example): https://developers.google.com/datastore/docs/concepts/entities#Embedded_Entities

But it is not clear how to do this.

Thanks in advance for the help

2

2 Answers

2
votes

I think this might be a good place to use Cloud Storage to store the image file, and then store the URL for the image file in the Datastore.

If you're on App Engine, you can explore the Cloud Storage Client Library.

If not, check out the Javadocs for Google Cloud Storage.

0
votes

im using Spring MVC + jpa + datastore to do it.

Model :

@Entity
public class Product {
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Key id;
    private MultipartFile image;
    private Blob img;
    //setter and getter
}

Controller :

@RequestMapping(value = "/add", method = RequestMethod.POST)
public String add(ModelMap model, Product form, HttpServletRequest request) {
   if(ServletFileUpload.isMultipartContent(request)) {
     try { 
        Blob file = new Blob(form.getImage().getBytes()); 
        form.setImg(file);
     } catch (IOException  e) { // TODO Auto-generated catch block 
        e.printStackTrace(); 
     }
   }
   Product inserted = ProductDAO.INSTANCE.save(form);
   //other logic and return to jsp
}

for dao just save as usual, you can found many example for it.