Nor Sea
Nor Sea

Reputation: 23

Similar method getReferenceFromUrl (from Java client) to use with Cloud Functions... exists?

i have one function that deletes a document from firebase using pub / sub, however, before deleting the document (using document reference) I want to get the storage reference for a link that is saved in a field of that document.

I'll exemplify to make it easier, there is the document Joseph that has the fields username: "Joseph"; SexUser: "Male"; and urlProfileUser: "any-valid-link-to-download-to-storage-image-uploaded".

Before deleting the document I want to take this field from the selected document, get the reference through the link (in Java I use storage.getReferenceFromUrl (urlProfileUser)), and through that, delete that photo from the storage, so that, just like that , I delete the document from the firestore.

Code for a cloud function that deletes the document I want: (I just need to now delete the image referenced by the storage link...)

import * as functions from 'firebase-functions'
import * as admin from "firebase-admin"; 

admin.initializeApp();

    //Scheduled job executed every day 23:00
    exports.removeUsersUnavailable = functions.pubsub.schedule('0 23 * * *').onRun((context)=>
    {
        const db = admin.firestore();
        const dateEvent = Date.now();
        const cutOff = dateEvent - 24*60*60*1000; // After 24 hours(one day) delete document
        db.collection("userManagers").orderBy('dateCreated').endAt(cutOff)
        .get()
        .then(snapshot => {
            if(snapshot.empty){
                console.log('Nothing still expirated');            
                return;
            }        
            snapshot.forEach(doc =>{                
                //console.log(doc.id, "=>", doc.data);            
                console.log('Expirated, date deleted');      
          
// Here I should delete the photo from the storage, since I already have the document data at that time
{...} <-// delete the image from storage with getReference link (link is string doc.data().urlProfile);
//Delete from firestore
                doc.ref.
                delete()
                .then(response=>{                
                    console.log('Document deleted successful', response);
                })
                .catch(error=>{
                    console.log('Error ocurred while data delete', error);
                });            
            });            
        })        
        .catch(error =>{
            console.log('Error while get the documents', error);
        }); 
    });

I'm use typescript to write cloud functions

Upvotes: 1

Views: 234

Answers (1)

Doug Stevenson
Doug Stevenson

Reputation: 317760

Cloud Storage server SDKs don't offer an equivalent of getReferenceFromUrl. That's a client SDK operation only.

What you should probably do instead is store the full path of the file in the storage bucket along with the URL, and use that path to delete the object instead of the URL you generated with the client SDK. So, for Android clients, you would store the value of StorageReference.getPath(), then feed that to storage SDK using Bucket.file() to build another reference to delete the object.

Upvotes: 1

Related Questions