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?