How to delete a file using cloud functions?

When I delete a message in my Firebase database, I want the cloud function to delete the message thumbnail in the firebase store accordingly. My problem is when I try to delete a thumbnail. I don’t think I am placing the image file correctly.

Here is what I tried:

const functions = require('firebase-functions') const admin = require('firebase-admin') const gcs = require('@google-cloud/storage')() exports.deletePost = functions.database.ref('Posts/{pushId}').onWrite(event => { const original = event.data.val() const previous = event.data.previous.val() const pushId = event.params.pushId if (original === null) return const filePath = 'Posts/' + pushId + 'thumbnail.jpg' const bucket = gcs.bucket('postsapp-12312') const file = bucket.file(filePath) const pr = file.delete() return pr }); 

This is what I get in the magazines

ApiError: not found in Object.parseHttpRespBody (/ user_code / node_modules / @ google-cloud / storage / node_modules / @ google-cloud / common / src / util.js: 192: 30) in Object.handleResp (/ user_code / node_modules / @ google-cloud / storage / node_modules / @ google-cloud / common / src / util.js: 132: 18) to / user _code / node_modules / @ google-cloud / storage / node_modules / @ google-cloud / common / src /util.js:465:12 in Request.onResponse [as _callback] (/user_code/node_modules/@google-cloud/storage/node_modules/retry-request/index.js:120:7) on Request.self.callback ( /user_code/node_modules/@google-cloud/storage/node_modules/request/request.js:188:22) on emitTwo (events.js: 106: 13) on Request.emit (events.js: 191: 7) on request . (/user_code/node_modules/@google-cloud/storage/node_modules/request/request.js:1171:10) on emitOne (events.js: 96: 13) on Request.emit (events.js: 188: 7)

+7
firebase firebase-storage google-cloud-functions
source share
3 answers

I managed to fix it. The problem here was that I wrote my bucket address incorrectly; it should be something like postsapp-12312.appspot.com instead of postsapp-12312

Update For a better way to put your address in a bucket @Robert answer

+4
source share

You can use the firebase function environment configuration to get the bucket name:

 const bucket = gcs.bucket(functions.config().firebase.storageBucket) 
+5
source share

As far as I know, it is not possible to remove firebase storage files from cloud functions. You can listen for change events in the clothing store, but you cannot interact with it.

What I was able to do was remove the β€œmessage” from the database using .set (null). This returns a promise that, when resolved, you can use to invoke deletion in the firebase repository.

 firebase.database().ref('Posts/{pushId}').set(null).then(() => { console.log('Post deleted from database') firebase.storage().ref(`Posts/${pushId}thumbnail-image.jpg`).delete().then(() => { console.log('Successfully deleted thumbnail'); }).catch(err => { console.log(err); }); }).catch(err => { console.log(err); }); 

Firebase storage will not allow you to delete the entire array recursively, so you need to delete every file every time.

+3
source share

All Articles