Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to set AWS S3 credentials in a Google Firebase cloud function?

I want my Firebase cloud function to access AWS S3. To do that, in my index.js file I am trying to tell AWS to look at the awscred.json file to get the connection credentials like this:

// Load the SDK for JavaScript
var AWS = require('aws-sdk');
// Set the Region
AWS.config.loadFromPath('./awscred.json');

This works fine when I test it locally, but when I try to deploy to the cloud I get this error:

Error: ENOENT: no such file or directory, open './awscred.json'

How can I add the file 'awscred.json' to the cloud function package? Do I need to add something to the package.json file?

like image 216
Mark Avatar asked Dec 15 '19 15:12

Mark


2 Answers

Just put the awscred.json file in your function source code deployment folder along with the source code. It will be deployed along with all the other source code so that it can be used while being run in Cloud Functions. If you want to load it from the relative path "./awecred.json" then it should exist right next to the source file.

like image 71
Doug Stevenson Avatar answered Oct 20 '22 07:10

Doug Stevenson


So it turned out that during the deployment process (ie. from local machine to Google's servers) the index.js file is run on my local machine. The error was generated because there was no 'awscred.json' file in the directory I was running my 'firebase deploy' command, so the solution was to just execute that command in my functions directory instead.

like image 29
Mark Avatar answered Oct 20 '22 07:10

Mark