title: How can I download file inside a folder in Google Cloud Storage using Cloud Functions?
body: <p>I am using an https triggered Google Cloud Function that is supposed to download a file from Google Cloud Storage (and then combine it with data from req.body). While it seems to work as long as the downloaded file is in the root directory I am having problems accessing the same file when placed inside a folder. The path to the file is <code>documents/someTemplate.docx</code></p>
  
  <pre><code>'use strict';
  const functions = require('firebase-functions');
  const path = require('path');
  const os = require("os");
  const fs = require('fs');
  const gcconfig = {
    projectId: "MYPROJECTNAME",
    keyFilename: "KEYNAME.json"
  };
  const Storage = require('@google-cloud/storage')(gcconfig)
  const bucketPath = 'MYPROJECTNAME.appspot.com'
  const bucket = Storage.bucket(bucketPath);
  
  exports.getFileFromStorage = functions.https.onRequest((req, res) =&gt; {
        let fileName = 'documents/someTemplate.docx'
        let tempFilePath = path.join(os.tmpdir(), fileName);
  
        return bucket.file(fileName)
        .download({
            destination: tempFilePath,
          })
          .then(() =&gt; {
            console.log(fileName + ' downloaded locally to', tempFilePath);
            let content = fs.readFileSync(tempFilePath, 'binary');
  
            // do stuff with the file and data from req.body
  
            return
          })
          .catch(err =&gt; {
            res.status(500).json({
              error: err
            });
          });
  })
  </code></pre>
  
  <p>What I don't understand is that when I move the file to the root directory and use the file name <code>someTemplate.docx</code> instead then the code works. </p>
  
  <p><a href="https://cloud.google.com/storage/docs/cloud-console" rel="nofollow noreferrer">Google's documentation</a> states that </p>
  
  <blockquote>
    <p>Objects added to a folder appear to reside within the folder in the GCP Console. In reality, all objects exist at the bucket level, and simply include the directory structure in their name. For example, if you create a folder named pets and add a file cat.jpeg to that folder, the GCP Console makes the file appear to exist in the folder. In reality, there is no separate folder entity: the file simply exists in the bucket and has the name pets/cat.jpeg.</p>
  </blockquote>
  
  <p>This seems to be correct as in the metadata the file name is indeed <code>documents/someTemplate.docx</code>. Therefore I don't understand why the code above does not work.</p>