0

We have firebase nodejs functions where we get base64 image data from our partner site. This data we are storing into firebase storage like below:

const storageRef = admin.storage().bucket(bucketName)
const fileRef = storageRef.file('myimage.png')
await fileRef.save(base64Image) 

The bucket used here for this purpose has readonly true so security is not a concern. How do I get the url of the image stored in bucket to put in my realtime db?

In UI way, I can directly upload image from the firebase console and can see the link. I need this link.

Based on my research on google, I see getting a download url is an issue on server side due to security of the bucket and you need to get signedUrl that expires. In my case as it is public so I am hoping there's a way to get the url.

An alternate way would be to bypass the bucket completely and store the base64 image directly into realtime db, but I'm not comfortable with that due to very large length of base64 strings. My images ideally would be in the range of few kbs.

How to deal with this?

Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
Moblize IT
  • 1,140
  • 2
  • 18
  • 44

1 Answers1

1

You have two options:

  1. Use a server-side generate signed URL with a very long expiration date.
  2. Generate a (non-expiring) download URL client-side.

To use download URLs, you'd need to:

  1. Write the path of the image to the database when it uploads.
  2. Have the clients detect of there is a download URL for their image in the database.
  3. If there is no download URL, have the client generate the download URL from the path, and write it to the database.
Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
  • though the 2nd option is more tempting but there is no client side involved so i am not sure how i can use this option? is there a code sample for very long expiration date signed url ? – Moblize IT Feb 15 '20 at 20:02
  • You can always create a client app, just for this purpose. Code sample for long expiration: https://stackoverflow.com/questions/42956250/get-download-url-from-file-uploaded-with-cloud-functions-for-firebase – Frank van Puffelen Feb 15 '20 at 21:35
  • sure definitely want to upvote and answer and was trying out whatever you mentioned. one question on ur comment: you said i can always create a client app for this purpose. trying to understand more on this. do u mean i can create a client app inside firebase functions and use it or you mean totally something else? – Moblize IT Feb 15 '20 at 21:49
  • Nope. You can't create a client-side app in Cloud Functions. The accepted answer on https://stackoverflow.com/questions/42956250/get-download-url-from-file-uploaded-with-cloud-functions-for-firebase would probably not have as many upvotes (including mine) if there was another way to do it inside Cloud Functions. – Frank van Puffelen Feb 15 '20 at 22:04
  • I got it working with signedUrl way. However, the url is crazy long with 256 chars with signedUrl option. I have public read access of the bucket but still removing that super long signature throws access error. anyway to have it shorter – Moblize IT Feb 16 '20 at 01:42
  • I've given you the only two options that exist for publicly readable, non-authenticated URLs – Frank van Puffelen Feb 16 '20 at 02:57
  • I am really sorry but I am unable to connect your comment with my question. You ruled out using client api in firebase functions. the only option left is get a signedUrl with a long expiry date. So, i did that which works but the url is just tooo long. Is there a option to get smaller url like we get in case of client sdk or going to console and can see. my bucket is public – Moblize IT Feb 16 '20 at 16:17