3

我是NodeJS的新手,并且正在尝试为Firebase的Cloud Functions编写下一个方法。使用NodeJS从数据库中删除对象时,从存储中删除文件的Firebase功能

我想实现:

  1. 当用户从火力地堡DB一个照片对象的功能应该被触发;
  2. 代码应该从对应于Photo obj的存储中移除文件对象。

这些是我的火力地堡DB结构:

照片/ {userUID}/{photoUID}

{ 
"dateCreated":  "2017-07-27T16:40:31.000000Z", 
"isProfilePhoto": true, 
"isSafe":   true, 
"uid":    "{photoUID}", 
"userUID":   "{userUID}" 
} 

而且火力地堡存储格式:

照片/ {userUID }/{photoUID} .png

和的NodeJS代码,我使用:

const functions = require('firebase-functions') 
    const googleCloudStorage = require('@google-cloud/storage')({keyFilename: 'firebase_admin_sdk.json' }) 
    const admin = require('firebase-admin') 
    const vision = require('@google-cloud/vision')(); 

    admin.initializeApp(functions.config().firebase) 

    exports.sanitizePhoto = functions.database.ref('photos/{userUID}/{photoUID}') 
     .onDelete(event => { 

      let photoUID = event.data.key 
      let userUID = event.data.ref.parent.key 

      console.log(`userUID: ${userUID}, photoUID: ${photoUID}`); 

      if (typeof photoUID === 'undefined' || typeof userUID === 'undefined') { 
       console.error('Error while sanitize photo, user uid or photo uid are missing'); 
       return 
      } 

      console.log(`Deleting photo: ${photoUID}`) 

      googleCloudStorage.bucket(`photos/${userUID}/${photoUID}.png`).delete().then(() => { 
       console.log(`Successfully deleted photo with UID: ${photoUID}, userUID : ${userUID}`) 
      }).catch(err => { 
       console.log(`Failed to remove photo, error: ${err}`) 
      }); 

     }); 

当我运行它,我得到了一个错误:“ApiError中:未找到”

enter image description here

我觉得代码的这些部分是在导致问题的一个:

googleCloudStorage.bucket(`photos/${userUID}/${photoUID}.png`).delete() 

预先感谢支持和耐心。

+0

@Jay我做了必要的修改。 –

回答

2

发现问题,这里是为我工作的代码:

exports.sanitizePhoto = functions.database.ref('photos/{userUID}/{photoUID}') 
    .onDelete(event => { 

     let photoUID = event.data.key 
     let userUID = event.data.ref.parent.key 

     console.log(`userUID: ${userUID}, photoUID: ${photoUID}`); 

     if (typeof photoUID === 'undefined' || typeof userUID === 'undefined') { 
      console.error('Error while sanitize photo, user uid or photo uid are missing'); 
      return 
     } 

     console.log(`Deleting photo: ${photoUID}`) 

     const filePath = `photos/${userUID}/${photoUID}.png` 
     const bucket = googleCloudStorage.bucket('myBucket-91823.appspot.com') 
     const file = bucket.file(filePath) 

     file.delete().then(() => { 
      console.log(`Successfully deleted photo with UID: ${photoUID}, userUID : ${userUID}`) 
     }).catch(err => { 
      console.log(`Failed to remove photo, error: ${err}`) 
     }); 

    }); 
+0

谢谢,我得到一个参考错误:未定义googleCloudStorage。我想知道如果我忘记安装谷歌存储的名义。有什么想法吗? –

+0

固定,我已经安装googleCloudStorage到项目目录,而不是函数目录。 –

1

try代码这个

const gcs = require('@google-cloud/storage')() 

    const fileBucketPush = 'Storage bucket.appspot.com'; // The Storage bucket that contains the file. 
    const filePathPush = 'folder/'+nameImage; // File path in the bucket. 
    vision.detectSafeSearch(gcs.bucket(fileBucketPush).file(filePathPush)) 
     .then((results) => { 
     ../// 

     }) 
     .catch((err) => { 
     console.error('ERROR:', err); 
     }); 

您启用云愿景API?