使用Firebasefunction更新存储更改上的Firebase数据库

我试图在我的Android应用程序中实现configuration文件图片function。 所以我使用了来自Firebase的“ 生成缩略图”样本。 所以每当我上传一个完整大小的图片,它会为我生成缩略图。 但是我想要在缩略图生成后更新我的实时数据库中的缩略图的URL。

const functions = require('firebase-functions'); const mkdirp = require('mkdirp-promise'); const gcs = require('@google-cloud/storage')(); const spawn = require('child-process-promise').spawn; const LOCAL_TMP_FOLDER = '/tmp/'; // Max height and width of the thumbnail in pixels. const THUMB_MAX_HEIGHT = 100; const THUMB_MAX_WIDTH = 100; // Thumbnail prefix added to file names. const THUMB_PREFIX = 'thumb_'; /** * When an image is uploaded in the Storage bucket We generate a thumbnail automatically using * ImageMagick. */ exports.generateThumbnail = functions.storage.object().onChange(event => { const filePath = event.data.name; const filePathSplit = filePath.split('/'); const fileName = filePathSplit.pop(); const fileDir = filePathSplit.join('/') + (filePathSplit.length > 0 ? '/' : ''); const thumbFilePath = `${fileDir}${THUMB_PREFIX}${fileName}`; const tempLocalDir = `${LOCAL_TMP_FOLDER}${fileDir}`; const tempLocalFile = `${tempLocalDir}${fileName}`; const tempLocalThumbFile = `${LOCAL_TMP_FOLDER}${thumbFilePath}`; // Exit if this is triggered on a file that is not an image. if (!event.data.contentType.startsWith('image/')) { console.log('This is not an image.'); return; } // Exit if the image is already a thumbnail. if (fileName.startsWith(THUMB_PREFIX)) { console.log('Already a Thumbnail.'); return; } // Exit if this is a move or deletion event. if (event.data.resourceState === 'not_exists') { console.log('This is a deletion event.'); return; } // Create the temp directory where the storage file will be downloaded. return mkdirp(tempLocalDir).then(() => { // Download file from bucket. const bucket = gcs.bucket(event.data.bucket); return bucket.file(filePath).download({ destination: tempLocalFile }).then(() => { console.log('The file has been downloaded to', tempLocalFile); // Generate a thumbnail using ImageMagick. return spawn('convert', [tempLocalFile, '-thumbnail', `${THUMB_MAX_WIDTH}x${THUMB_MAX_HEIGHT}>`, tempLocalThumbFile]).then(() => { console.log('Thumbnail created at', tempLocalThumbFile); // Uploading the Thumbnail. return bucket.upload(tempLocalThumbFile, { destination: thumbFilePath }).then(() => { console.log('Thumbnail uploaded to Storage at', thumbFilePath); // Don't know what to write here. }); }); }); }); }); 

一旦这个代码的最终承诺完成,我想从生成的configuration文件图片中的/users/{userId}/profilePic下载URL

你需要添加一些东西来实现你想要的东西。 首先你需要firebase-admin并初始化应用程序。

 const admin = require('firebase-admin'); admin.initializeApp(functions.config().firebase); 

在您需要其他组件之后,这应该在文件的顶部完成。

一旦你有了,你可以添加一些代码到最后的承诺来更新数据库与新的参考。

 .then(() => { console.log('Thumbnail uploaded to Storage at', thumbFilePath); //userID here would be the userID that you want to update. I couldn't see a reference to it in the code you provided. var databaseRef = admin.database().ref('users').child(userID).child("profilePic"); databaseRef.transaction(function(oldValue) { return bucket.file(thumbFilePath).getDownloadURL().then(funct‌​ion(url) { return url; } }); }); 

您可以阅读有关交易以更新此处的参考。 我只包含强制性的transactionUpdate参数,而不是你可能需要的可选参数。 https://firebase.google.com/docs/reference/admin/node/admin.database.Reference#transaction