Aralizer Aralizer - 5 months ago 79
Node.js Question

Secure external links for Firebase Storage on NodeJS server-side

I'm having issues generating external links to files stored in my Firebase Storage bucket.

I'm using Google Cloud Storage for a while now and used this library (which is based on this answer) for generating external links for regular Storage buckets, but using it on the Firebase-assigned bucket doesn't seem to work.

I can't generate any secure HTTPS links and keep getting certificate validation error

NET::ERR_CERT_COMMON_NAME_INVALID
stating that my connection is not private. If I remove the 'S' from the HTTPS, the link works.

NOTE: Using the same credentials and private key to generate links for other buckets in my project, works just fine. It's only the Firebase bucket that is refusing to accept my signing...

Answer

I recommend using the official GCloud client, and then you can use getSignedUrl() to get a download URL to the file, like so:

bucket.file(filename).getSignedUrl({
  action: 'read',
  expires: '03-17-2025'
}, function(err, url) {
  if (err) {
    console.error(err);
    return;
  }

  // The file is now available to read from this URL.
  request(url, function(err, resp) {
    // resp.statusCode = 200
  });
});

Per Generate Download URL After Successful Upload this seems to work with Firebase and GCS buckets.

Comments