Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get shorter file URL from Google Cloud Storage (with Firebase Cloud Functions)

Tags:

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?

like image 456
Salil Thakur Avatar asked Apr 08 '17 21:04

Salil Thakur


People also ask

How do I get Firebase Storage URL?

Download Data via URL If you already have download infrastructure based around URLs, or just want a URL to share, you can get the download URL for a file by calling the getDownloadUrl() method on a Cloud Storage reference.

How can I get download URL from Firebase Storage node JS?

To retrieve a download URL, your client app needs to call the getDownloadUrl() method on the file reference. (The examples in this article are using the JavaScript SDK, but the concepts apply to all platforms.) const storage = firebase. storage(); storage.

How do I get files from Firebase Storage?

There are two ways to download files with Firebase Storage, using references in the SDK or a download URL. iOS and Android users can download files into memory, disk, or from a download URL. The web SDK can download files just from a download URL. StorageReference storageRef = FirebaseStorage.


1 Answers

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?

like image 169
Mike McDonald Avatar answered Sep 21 '22 09:09

Mike McDonald