2

I want to generate an URL for accessing the images in the bucket in Google Cloud Storage, as I want to reference it in img src tag of html. The reason is that we don’t store the image in server.

How can I achieve the following using java?

  • Generate URL to reference the image in my bucket of Google Cloud Storage, https://storage.cloud.google.com/my-bucket-name/myImage.jpg.
  • This URL could be used to get the reference to src of img tag but the problem is only the ones who are signed in to google are able to view else it would ask for sign in first.
  • But what I observed is when I paste the above url in the browser, I find that the URL gets changed to some random url like below, and with this below url anybody can view the image without sign in and I feel if I could generate this URL using java I may be able to resolve the problem.

  • URL is as below (random fake URL)

https://00e9e544fb-apidata.googleusercontent.com/download/storage/v1/b/my-bucket-name/o/myImage.jpg?qk=AD5-vreOG-Y-RsJ0DZuI--owy_O_cx1RQy_Ya_kn0A_T-i-T-3xhg

alextru
  • 441
  • 2
  • 12
NatsuZeref
  • 69
  • 2
  • 8

1 Answers1

0

Your original suggestion, https://storage.cloud.google.com/my-bucket-name/myImage.jpg, should be fine, but for it to work without any authentication, you'll need to grant read permission for those objects to anonymous users.

For instructions on that, see Allow Public Read access on a GCS bucket?.

Brandon Yarbrough
  • 37,021
  • 23
  • 116
  • 145
  • i tried granting read permission to the anonymous users using -> gsutil acl ch -u AllUsers:R gs://yourbucket/** works fine but , making object or bucket public is not good i feel... can you say me any way to create the encrypted url of the public url .... – NatsuZeref Jan 10 '19 at 08:48
  • Sure, you can sign a URL : `gsutil signurl -d 1h service_account.json gs://bucket/object`. The trick here is that such URLs are only valid for a short time. – Brandon Yarbrough Jan 10 '19 at 17:46
  • umm , this would do i feel... thank you , i will try and get back to you.. – NatsuZeref Jan 11 '19 at 00:24
  • thank you , your suggestion was great, i created url using gsutil as well as google cloud java api please refer this link for the code to create url using java-. https://hirooka.pro/?p=9132 – NatsuZeref Jan 11 '19 at 06:55