1

I uploaded image using Firebase sdk on Flutter, on Flutter side i can usually just call getDownloadUrl() on the reference, then i want to get that url on my Cloud Function trigger so if image is uploaded on Firebase Storage i would get the download URL and post it on firestore.

I have tried getting metadata mediaLink but it is not the same url which i can found manually from browsing firebase storage console.

exports.generateThumbnail = functions.storage.object().onFinalize(async (object) => {
    const db = admin.firestore();
    const filePath = object.name;
    const contentType = object.contentType; 
    const fileDir = path.dirname(filePath);
    const fileName = path.basename(filePath);
    
    const jobId = fileName.replace("poster_","").replace(".png","");
    const bucket = admin.storage().bucket(object.bucket);
    const file = bucket.file(filePath);
    const posterMetadata = await file.getMetadata();
    const posterFileUrl = posterMetadata[0].mediaLink;
    return functions.logger.log('url: '+posterFileUrl );
}

Using that url i got "Anonymous caller does not have storage.objects.get access to the Google Cloud Storage object." error,

How to get this generated link, which we can found on firebase storage console

enter image description here

MBHA Phoenix
  • 2,029
  • 2
  • 11
  • 25
Jolzal
  • 547
  • 7
  • 19
  • You can either [generate a signed URL](https://stackoverflow.com/a/42959262) (which is the Google Cloud Storage concept for publicly accessible URLs for files) or [add a token to the metadata](https://stackoverflow.com/a/43764656) (which then gets you a Firebase download URL). I linked he question with both answers, which is the first link I get when I [put your title in search](https://www.google.com/search?q=How+to+get+firebase+generated+download+url+on+Firebase+function). – Frank van Puffelen Aug 25 '21 at 14:42

1 Answers1

3

I understand that you want, in a Cloud Function triggered when a new file is uploaded to Cloud Storage, to get the signed URL of this file.

The following will do the trick:

exports.generateFileURL = functions.storage.object().onFinalize(async object => {

    try {
        const bucket = admin.storage().bucket(object.bucket);
        const file = bucket.file(object.name);

        const signedURLconfig = { action: 'read', expires: '01-01-2030' };

        const signedURLArray = await file.getSignedUrl(signedURLconfig);
        const url = signedURLArray[0];

        // Do whatever you want with the signed URL
        // e.g. save it to Firestore

        await admin.firestore().collection('signedURLs').add({ fileName: object.name, signedURL: url })
        return null;
    } catch (error) {
        console.log(error);
        return null;
    }

});

We use the getSignedUrl() method from the Cloud Storage Node.js Client API. You'll find in the documentation more details on the possible properties and values of the configuration object passed to the method (i.e. signedURLconfig in the above example).

Renaud Tarnec
  • 79,263
  • 10
  • 95
  • 121
  • thanks for answering, but as far as i know the signedUrl generated would only valid for few weeks using this method and i need a long lived one, is there a way to just fetch the generated url on firebase since i uploaded it via firebase client wont it like saved on metadata or something? – Jolzal Aug 25 '21 at 08:10
  • "Is there a way to just fetch the generated url on firebase since i uploaded it via firebase client" => Why don't you use the **client** Flutterfire [`getDownloadURL()` method](https://firebase.flutter.dev/docs/storage/usage#download-urls) and save it to Firestore (from the client)? You cloud try adding this URL to the object's metadata, I've never tried. – Renaud Tarnec Aug 25 '21 at 08:19
  • at first i do use getDownloadUrl on client side, but then i face problem when using generate thumbnail trigger on cloud function, since sometime the generate thumbnail function is executed before the firestore document created , so i tried to created the firestore document first then uploading the image to make sure it executed when document created – Jolzal Aug 25 '21 at 08:28
  • The probem is that you cannot use `getDownloadUrl()` if the object doesn't exist. What you could do is to trigger the make thumbnail Cloud Function on the Firestore doc creation, not on the file creation. – Renaud Tarnec Aug 25 '21 at 08:37
  • I see thats a good workaround i will try, thanks for helping – Jolzal Aug 25 '21 at 15:07