marvin ralph
marvin ralph

Reputation: 1280

How To Access Cloud Storage In Cloud Functions

Am trying to access Firebase Cloud Storage in my Cloud functions but i keep getting the following error in the cloud functions log:

Error: Failed to import the Cloud Storage client library for Node.js. Make sure to install the "@google-cloud/storage" npm package. Original error: SyntaxError: Unexpected token {
    at new FirebaseError (/srv/node_modules/firebase-admin/lib/utils/error.js:43:28)
    at new Storage (/srv/node_modules/firebase-admin/lib/storage/storage.js:65:19)
    at /srv/node_modules/firebase-admin/lib/firebase-app.js:255:20
    at FirebaseApp.ensureService_ (/srv/node_modules/firebase-admin/lib/firebase-app.js:376:23)
    at FirebaseApp.storage (/srv/node_modules/firebase-admin/lib/firebase-app.js:253:21)
    at FirebaseNamespace.fn (/srv/node_modules/firebase-admin/lib/firebase-namespace.js:292:45)
    at exports.initializeNewVideoUpload.functions.firestore.document.onCreate (/srv/index.js:383:24)
    at cloudFunction (/srv/node_modules/firebase-functions/lib/cloud-functions.js:134:23)
    at /worker/worker.js:825:24
    at <anonymous>

Below is my code:

const admin = require('firebase-admin'); const functions = require('firebase-functions');

admin.initializeApp();
const db = admin.firestore();
exports.initializeNewVideoUpload = functions.firestore.document('new-videos-uploads/{id}').onCreate(async (snapshot, context) => {
      const data = snapshot.data();
      const fileNames = data.file_names;
      const bucket = admin.storage().bucket('trumateappdev.appspot.com');
    
      fileNames.forEach(async fileName => {
        const actualFile = bucket.file(fileName);
        const resumableUpload = await actualFile.createResumableUpload();
        const uploadEndpointUrl = resumableUpload[0];
    
        data.resumable_urls.push(uploadEndpointUrl);
      });
    
      await admin.firestore().collection('new-videos-uploads').doc(context.params.id).update(data);
    });

Below are the dependencies in my package.json:

"@google-cloud/storage": "^5.5.0",
"firebase-admin": "^9.2.0",
"firebase-functions": "^3.11.0"

PS: Am on the Spark Plan

Upvotes: 1

Views: 409

Answers (1)

ThienLD
ThienLD

Reputation: 741

I found this solution here. Add this to your package.json:

"engines": {"node": "8"}

Apparently this problem occurs because your node.js version is too old

Upvotes: 2

Related Questions