4
votes

I'm trying to use Google Cloud storage to store images that I'll be using in jsp files. I've created the bucket, uploaded an image (for testing purposes) and try to retrieve a Url to it from a java class. I keep getting the error message

    HTTP ERROR 500

Problem accessing /. Reason:

INVALID_BLOB_KEY: Could not read blob.
Caused by:

java.lang.IllegalArgumentException: INVALID_BLOB_KEY: Could not read blob.
at 
com.google.appengine.api.images.ImagesServiceImpl.getServingUrl(ImagesServiceImpl.java:282)

The Bucket I created

The Bucket I created

The code I use to retrieve the Url (BUCKETNAME is a static string with the name of the bucket)

public static String getImageURL(String inFilename) {
    String key = "/gs/" + BUCKETNAME + "/" + inFilename;
    ImagesService imagesService = ImagesServiceFactory.getImagesService();
    ServingUrlOptions options =     ServingUrlOptions.Builder.withGoogleStorageFileName(key);
    String servingUrl = imagesService.getServingUrl(options);

    return servingUrl;
}

I've tried to make the image public, but that didn't help. I've looked through various answers here, but I'm a bit lost. Any help would be appreciated

1
I am experiencing the same problem. I used Firebase Storage to save the images. Did you find a solution? And did you use Firebase also? It seems weird that it needs the BlobKey when using withGoogleStorageFileName(), and not withBlobKey(). - jonasjuss
I used Google App Engine with java for this one. I'm looking into Firebase for a future project. If I look at my code that I ended up with, I went a different route: public static String getImageURL(String inFolder, String inFilename) { String servingUrl = PATHNAME + BUCKETPATH + inFolder + inFilename; return servingUrl; } I also remember that I had some problems using this from localhost. You need to do some setup to get this to work from localhost - Mirko Pelgrom
Thank you. After a while I realized that I only got the error when running locally, so managed to solve my problem without local debugging for this part. - jonasjuss

1 Answers

1
votes

I also faced the same issue. For future people, make sure you have made the bucket public. From the same method you can also generate thumbnails and secured urls to your images

public static String getImageURL(String inFilename) {
    String key = "/gs/" + BUCKETNAME + "/" + inFilename;
    ImagesService imagesService = ImagesServiceFactory.getImagesService();
    ServingUrlOptions options = ServingUrlOptions.Builder  
           .withGoogleStorageFileName(key).imageSize(150).secureUrl(true);
    String servingUrl = imagesService.getServingUrl(options);

    return servingUrl;
}