title: Update Firebase database on Storage change using Firebase Functions
body: <p>I am trying to implement the Profile Picture feature in my Android app. So I have used <a href="https://github.com/firebase/functions-samples/tree/master/generate-thumbnail" rel="nofollow noreferrer">Generate Thumbnail</a> sample from firebase. So whenever I am uploading an full sized image, it generates and thumbnail for me. But I want to update the URL of thumbnail in my realtime database once the thumbnail is generated.</p>
  
  <pre><code>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 =&gt; {
    const filePath = event.data.name;
    const filePathSplit = filePath.split('/');
    const fileName = filePathSplit.pop();
    const fileDir = filePathSplit.join('/') + (filePathSplit.length &gt; 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(() =&gt; {
      // Download file from bucket.
      const bucket = gcs.bucket(event.data.bucket);
      return bucket.file(filePath).download({
        destination: tempLocalFile
      }).then(() =&gt; {
        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}&gt;`, tempLocalThumbFile]).then(() =&gt; {
          console.log('Thumbnail created at', tempLocalThumbFile);
          // Uploading the Thumbnail.
          return bucket.upload(tempLocalThumbFile, {
            destination: thumbFilePath
          }).then(() =&gt; {
            console.log('Thumbnail uploaded to Storage at', thumbFilePath);
            // Don't know what to write here.
          });
        });
      });
    });
  });
  </code></pre>
  
  <p>Once the final promise of this code completes, I want to take the downloadable URL from the generated profile pic at <code>/users/{userId}/profilePic</code></p>