0
votes

I'm trying to include service account key into my storage function to be able to get long lived signed url by following out of date example here

https://github.com/firebase/functions-samples/blob/b404482342906ee1b46dddb4c75667685ab098a1/generate-thumbnail/functions/index.js#L21

I have downloaded my key from IAM which is in JSON format. I have tried to save it right next to my function

-functions/storage/resizeProfileImg.js

-functions/storage/service-account-credentials.json

-functions/index.js

-functions/admin.js

where resizeProfileImg.js is my function and call it like this

const { Storage } = require('@google-cloud/storage');
const storage = new Storage({ projectId: projectId ,keyFilename: './service-account-credentials.json'})

but after deployment when the function is triggered then I get an error

Error: ENOENT: no such file or directory, open '/srv/service-account-credentials.json'

I have even tried to add it in constant like this

const serviceAccountCredentials = require('./accountKey/service-account-credentials.json')

const { Storage } = require('@google-cloud/storage');
const storage = new Storage({ projectId: projectId ,keyFilename: serviceAccountCredentials})

but then I get an error

TypeError: Path must be a string. Received { type: 'service_account',...

Any idea how to do this properly

1
If you think the sample is out of date, then please log a bug request on GitHub on that repo.Doug Stevenson
Could you edit the question to show exactly what you've done that doesn't work the way you expect? Be specific about how your project is organized and where you've placed the files. Anyone should be able to reproduce the problem with the steps you show.Doug Stevenson
There are more examples out of date. The only problem is with const gcs = require('@google-cloud/storage') should be like this *** const { Storage } = require('@google-cloud/storage'); const storage = new Storage({ projectId: projectId})**delmin
@DougStevenson I have edited my question... Please see if there is enough information or if you need more infodelmin
Sorry, it's still not clear to me what your project directory structure looks like.Doug Stevenson

1 Answers

1
votes

In Cloud Functions, the current directory . isn't where your source file is located. It's where the functions folder was deployed. Since your credentials file is in a subdirectory called "storage", you will need to use that in the path.

const serviceAccountCredentials = require('./storage/service-account-credentials.json')