title: Firebase cloud function [ Error: memory limit exceeded. Function invocation was interrupted.] on youtube video upload
body: <p>I was trying to upload videos to youtube using the firebase cloud function.</p>
  
  <p>What I need is when a user uploads a video to firebase cloud storage, functions.storage.object().onFinalize event will get triggered and in that event, I store the file to a temporary location and upload the file to youtube from the temp location to youtube, after uploading I delete both files.</p>
  
  <p>It will work fine for small files. </p>
  
  <p>But if I upload a large file then the function is getting terminated by showing this error</p>
  
  <p><strong>Error: memory limit exceeded. Function invocation was interrupted.</strong></p>
  
  <p>Code for uploading video</p>
  
  <pre><code>   var requestData = {
          'params': {
          'part': 'snippet,status'
          },
          'properties': {
          'snippet.categoryId': '22',
          'snippet.defaultLanguage': '',
          'snippet.description': "docdata.shortDesc",
          'snippet.tags[]': '',
          'snippet.title': "docdata.title",
          'status.embeddable': '',
          'status.license': '',
          'status.privacyStatus': 'public',
          'status.publicStatsViewable': ''
          }, 'mediaFilename': tempLocalFile
      };
  
      insertVideo(tempLocalFile, oauth2Client, requestData);
  </code></pre>
  
  <p>insert video function </p>
  
  <pre><code>function insertVideo( file, oauth2Client, requestData) {
      return new Promise((resolve,reject)=&gt;{
          google.options({ auth: oauth2Client });
          var parameters = removeEmptyParameters(requestData['params']);
          parameters['auth'] = oauth2Client;
          parameters['media'] = { body:  fs.createReadStream(requestData['mediaFilename'])};
          parameters['notifySubscribers'] = false;
          parameters['resource'] = createResource(requestData['properties']);
  
          console.log("INSERT &gt;&gt;&gt; ");
          let req = google.youtube('v3').videos.insert(parameters,  (error, received)=&gt; {
              if (error) {
                  console.log("in error")
                  console.log(error);
                  try {
                      fs.unlinkSync(file);
                  } catch (err) {
                      console.log(err);
                  } finally{
                      // response.status(200).send({ error: error })
                  }
                  reject(error)
              } else {
                  console.log("in else")
                  console.log(received.data)
                  fs.unlinkSync(file);
                  resolve();
              }
          }); 
      })
  
  }
  </code></pre>
  
  <p>code for creating temp local file</p>
  
  <pre><code>           bucket.file(filePath).createReadStream()
              .on('error', (err)=&gt; {
                  reject(err)
              })
              .on('response', (response)=&gt; {
                  console.log(response)
              })
              .on('end', ()=&gt; {
                  console.log("The file is fully downloaded");
                  resolve();
              })
              .pipe(fs.createWriteStream(tempLocalFile));
  </code></pre>
  
  <p>Every file read and write is handled by streams, any idea on why the memory issue is happening</p>