0
votes

I am trying to upload the image files using gcs client library+java in local google app engine dev server. Images are uploaded successfully and i can see the entries created in local datastore under localhost:8888/_ah/admin/datastore

How to get the public key for the uploaded images so that i can show images in my application. Example profile pic.

Sample copy of code:

 String filePath = Constants.gcsUrl + "/" + bucketName + "/"+ objectName;
 GcsService gcsService = GcsServiceFactory
                .createGcsService(new RetryParams.Builder()
                        .initialRetryDelayMillis(10).retryMaxAttempts(10)
                        .totalRetryPeriodMillis(15000).build());
        GcsFilename filename = new GcsFilename(bucketName, objectName);
        GcsFileOptions options = new GcsFileOptions.Builder()
                .addUserMetadata("cache-control",
                        "max-age=" + (86400 * 365)).mimeType(mimeType)
                .acl(ACL_PUBLIC_READ).build();
        GcsOutputChannel writeChannel = gcsService.createOrReplace(
                filename, options);
        writeChannel.write(ByteBuffer.wrap(fileBytes));
        writeChannel.close();
1

1 Answers

2
votes

You can use the Images service on the local devserver. For that you need a blobkey:

BlobstoreService blobStore = BlobstoreServiceFactory.getBlobstoreService();
BlobKey blobKey = blobStore.createGsBlobKey("/gs/" + filename.getBucketName() + "/" + filename.getObjectName())); // blobKey can be persisted
ImagesService imagesService = ImagesServiceFactory.getImagesService();
String url = imagesService.getServingUrl(ServingUrlOptions.Builder.withBlobKey(image));

Resizing also works on the local devserver by appending =sxxxx, but you can't get the native image size - on production, appending =s0 to the url gets the original image, but it doesn't work locally.