2

I have been trying to retrieve the download URL from the last 2 days. I searched a lot for getting the correct answer but I got all the older and unclear answers. I do not find any newer/perfect answer that's why I am posting this question here.

I am uploading a base64 image to firebase storage via admin-SDK through firebase functions. Below is the code which I am using to upload my base64 to firebase storage.

base64 = "mybase64imagestring";

let bufferStream = new stream.PassThrough();
bufferStream.end(new Buffer.from(base64, 'base64'));

// Retrieve default storage bucket
let bucket = admin.storage().bucket();

// Create a reference to the new image file
let file = bucket.file(`/jk_${dish.createdAt}.jpg`);

bufferStream.pipe(file.createWriteStream({
    metadata: {
      contentType: 'image/jpeg'
    }
  }))
  .on('error', error => {
    reject(`Error while uploading picture ${JSON.stringify(error)}`);
  })
  .on('finish', () => {
    // The file upload is complete and i want to get download URL(which is not public and should not expire) here.

  });

I tried the method "getSignedUrl" to get download URL as stated here but I found in comments and also on some other websites that link retrieved by getSignedUrl() is expiring in a week. I want a permanent link that does not expire for at least a month. I found somewhere that I can use a public link that doesn't expire but I do not find any way that how to retrieve it. Also, I do not want to compromise security so I do not want to store public link.

After getting that link, I am going to store that on the database and from the database, I will access the image via that link. That's why I expect a link to do not expire.

Jaydip Kalkani
  • 2,493
  • 6
  • 24
  • 56
  • 1
    You're supposed to use getSignedUrl for that. The URL should expire on the date that you pass to it. If that doesn't work the way you expect, contact Google Cloud Support. https://cloud.google.com/support/ – Doug Stevenson Jun 07 '19 at 12:57
  • Did you **try** whether the link expires when you do what James answered [here](https://stackoverflow.com/a/42959262/209103)? Or are you just uncertain because of comments on an accepted, and highly upvoted, answer? I'd really give it a try, and post back (and comment) if it doesn't work as the answer says. – Frank van Puffelen Jun 07 '19 at 14:16
  • 1
    @FrankvanPuffelen I am uncertain about that and I am saying that because of comments of that upvoted answer. Also found the same thing on other SO answers and on some GitHub repo issue trackers that download URL gets invalidated in a week. – Jaydip Kalkani Jun 07 '19 at 15:51
  • @FrankvanPuffelen I tried James method. It's returning a very big URL. I am storing download URL to firebase realtime DB. The url returned by `getSignedUrl` is too long so it's using too much bandwidth in db usage. There is no way to get download url which is showing on firebase storage page (e.g. https://firebasestorage.googleapis.com/v0/b/xxxx-xxxxx.appspot.com/o/myimage.jpg?alt=media&token=mytoken). It's way shorter than the URL I get from `getSignedUrl`. – Jaydip Kalkani Jun 08 '19 at 04:08
  • Those download URLs are only generated by the client-side SDKs. To get them, you'll need to have a client-side SDK call `StorageReference.getDownloadUrl()` (or equivalent). – Frank van Puffelen Jun 08 '19 at 13:40
  • @FrankvanPuffelen and i assume u cannot have those client-sdk call on firebase functions as that is nodejs server side code ? – Moblize IT Aug 28 '19 at 05:46
  • @MoblizeIT Yes, I am also guessing that. but I think it should be possible no? or maybe there is some reason for not to do so. – Jaydip Kalkani Aug 28 '19 at 11:54

0 Answers0