title: How do I access Firebase Storage inside of Functions on Node.js server?
body: <p>I am running a Firebase Functions instance like so:</p>
  
  <pre><code>import * as functions from 'firebase-functions'
  import * as express from 'express'
  import * as admin from 'firebase-admin'
  import { MyApi } from './server'
  
  admin.initializeApp(functions.config().firebase)
  
  const firebaseDb: admin.database.Database = admin.database()
  
  const app: express.Application = MyApi.bootstrap(firebaseDb).app
  
  export const myApp = functions.https.onRequest(app)
  </code></pre>
  
  <p>Everything works fine with my Realtime Database, but I am having trouble integrating Storage properly. According to <a href="https://firebase.google.com/docs/storage/web/start" rel="noreferrer">the docs</a>, I need to set it up like this:</p>
  
  <pre><code>var config = {
    apiKey: '&lt;your-api-key&gt;',
    authDomain: '&lt;your-auth-domain&gt;',
    databaseURL: '&lt;your-database-url&gt;',
    storageBucket: '&lt;your-storage-bucket&gt;'
  };
  firebase.initializeApp(config);
  
  // Get a reference to the storage service, which is used to create references in your storage bucket
  var storage = firebase.storage();
  var storageRef = storage.ref();
  </code></pre>
  
  <p>However this doesn't work for me since I'm using the Admin SDK. I'm not importing the <code>firebase</code> library at all. I tried accessing Storage like this: </p>
  
  <pre><code>const fbStorage = admin.storage()
  </code></pre>
  
  <p>This feels wrong. The interface for the Admin storage method is completely different from the Firebase client SDK's:</p>
  
  <p><strong>node_modules/firebase-admin/lib/index.d.ts</strong></p>
  
  <pre><code>declare namespace admin.storage {
    interface Storage {
      app: admin.app.App;
      bucket(name?: string): Bucket;
    }
  }
  </code></pre>
  
  <p><strong>node_modules/firebase/index.d.ts</strong></p>
  
  <pre><code>declare namespace firebase.storage {
    interface Storage {
      app: firebase.app.App;
      maxOperationRetryTime: number;
      maxUploadRetryTime: number;
      ref(path?: string): firebase.storage.Reference;
      refFromURL(url: string): firebase.storage.Reference;
      setMaxOperationRetryTime(time: number): any;
      setMaxUploadRetryTime(time: number): any;
    }
  </code></pre>
  
  <p>Significantly, the admin Storage interface is missing the <code>ref()</code> and <code>ref().put()</code> methods, so none of the documentation dealing with uploading files applies. I can access my files through <code>admin.storage().bucket().file('path/to/file.jpg')</code>, but that seems rather roundabout and I'm not sure I'm supposed to be doing that.</p>
  
  <p>As a workaround, I tried to initialize a non-admin Firebase app (<code>firebase.initializeApp(config)</code>) on top of <code>admin.initializeApp()</code>. But when I try to launch Functions that gives the fatal error <code>database: Firebase: Firebase service named 'database' already registered (app/duplicate-service).</code></p>
  
  <p>Right now I made a separate app, and am trying to delegate Storage functionality to that secondary app. Is there a better way? </p>
  
  <p>Thanks.</p>
  
  <hr>
  
  <p><strong>UDPATE (ANSWER):</strong></p>
  
  <p>Thanks to Renaud's advice, I learned that my initial attempt was actually the correct one. Turns out you ARE supposed to access your Storage instance through <code>admin.storage()</code>. The interface definitions do differ, because the client-side SDK and admin (server-side) SDK are catering to different needs. If you want to include the type definitions, you need to import them from '@google-cloud/storage'. </p>
  
  <p>Below is a sample of how to use the Bucket API based on <a href="https://cloud.google.com/storage/docs/uploading-objects#storage-upload-object-nodejs" rel="noreferrer">the official docs</a>:</p>
  
  <pre><code>import { Bucket } from '@google-cloud/storage'
  
  const filename = 'path/to/myfile.mp3'
  
  const bucket: Bucket = admin.storage().bucket()
  
  bucket
    .upload(filename, {
      destination: 'audio/music/myfile.mp3',
    })
    .then(() =&gt; {
      console.log(`${filename} uploaded to ${bucket.name}.`)
    })
    .catch(err =&gt; {
      console.error('ERROR:', err)
    })
  </code></pre>