5
votes

I have the following Firebase Cloud Function to get the URL of the file stored in Google Cloud Storage.

const gcs = require('@google-cloud/storage')({keyFilename: 'service-account.json'});

exports.generateFileLink = functions.storage.object().onChange(event => {
  const object = event.data;
  const filePath = object.name;
  const bucket = gcs.bucket(object.bucket);
  const file = bucket.file(filePath);
  const action = 'read';
  const expires = '03-09-2491';
  return file.getSignedUrl({action, expires}).then(signedUrls => {
    console.log(signedUrls[0])
  });
})

This returns the correct URL, but it is over 600 characters long. The URL for the same file as seen on the Firebase web console is less than 200 characters long. Is there any way I can retrieve the URL using firebase-admin or firebase-functions modules to get the shorter URL?

1

1 Answers

4
votes

Short answer is that we're working on a firebase-admin Storage client, but it's still a little ways away. For now, signed URLs are the way to go if you need to create a download URL in a function.

Why do you need to generate signed URLs in the function vs using the download URLs provided by Firebase? Is it that you can't retrieve the URL via a client in the function and you need to move it somewhere else?