Cloud Functions: how to upload additional file for use in code?

11,149

Solution 1

You'd like to upload 3 files to deploy your Cloud Function:

  • index.js
  • package.json
  • prediction_service.proto

In order to do so via the Developer Console, you'll need to:

  1. Go to the Google Cloud Developer Console > Cloud Functions > Create Function
  2. In the "Source Code" field, choose either:
    • "ZIP upload" and select a zip file including your 3 files,
    • "ZIP from Cloud Storage" and select file location on GCS,
    • "Cloud Source repository" and provide your repo details
  3. Fill in the remaining fields and click "Create"

Once deployed, in the source tab for your function you'll see the three files displayed.

Alternatively, you can use gcloud to deploy your files via the following command:

gcloud beta functions deploy <functionName> --source=SOURCE

where source can be a ZIP file on Google Cloud Storage, a reference to source repository or a local filesystem path. I'd recommend to have a look at the doc for this command for full details.

Solution 2

I find this way the easiest when it comes to Firebase Functions:

  1. Put your .proto file into functions folder of your firebase project (where index.js and package.json is located).
  2. Deploy your functions as normal with the CLI command firebase deploy --only functions

As you can see here the file is automatically added to the project in the GCP:

enter image description here

And you can access it in your node.js project:

protobuf.load(__dirname + '/schema.proto')

Solution 3

When you are using Firebase Cloud Functions with TypeScript (your code is in functions/src/index.ts), you need to put the additional files in functions/lib

Solution 4

While it is possible to use GCS, it's simple to include files in your source.

  1. Put your package.json, index.js (or whatever file is specified in package.json's 'main' field) and other dependent files in a directory.
  2. When you create your function, provide that directory including your other files via the ZIP upload or Cloud Source repository.
  3. Your other files are available at path.join(__dirname, 'your/path/file.ext')
Share:
11,149
kkost
Author by

kkost

Updated on July 23, 2022

Comments

  • kkost
    kkost almost 2 years

    I need to get access to protoc file in my code. Locally I just put it in the folder but how to get this file from deployed Firebase functions?

    const grpc = require('grpc');
    const PROTO_PATH = __dirname + '\\protos\\prediction_service.proto';
    
    exports.helloWorld = functions.https.onRequest((request, response){
        var tensorflow_serving = grpc.load(PROTO_PATH).tensorflow.serving;
    ...
    }
    
  • kkost
    kkost about 6 years
    Thanks for reply, bu could you describe more detail about main field? I don't have such one
  • David
    David about 6 years
    It's optional. You can add 'main': 'yourfile.js' to package.json if you want to use a different filename for your entry javascript file. It's documented here and discussed in this SO question
  • kkost
    kkost about 6 years
    amazing! Do we have any ability to upload using a command line and do not create archive every time?
  • LundinCast
    LundinCast about 6 years
    Yes to both questions. I've edited my answer to include this info.
  • Ralpharoo
    Ralpharoo almost 4 years
    To ensure a smooth process, you can update the package.json build command to run a custom script to copy the files in. I.e. "build": "node copy-deps && tsc". This means the standard "firebase deploy --only functions" will automatically copy the files in