0

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

delmin
  • 2,330
  • 5
  • 28
  • 54
  • If you think the sample is out of date, then please log a bug request on GitHub on that repo. – Doug Stevenson Jun 01 '19 at 13:33
  • 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 Jun 01 '19 at 13:34
  • 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 Jun 01 '19 at 13:36
  • @DougStevenson I have edited my question... Please see if there is enough information or if you need more info – delmin Jun 01 '19 at 13:43
  • Sorry, it's still not clear to me what your project directory structure looks like. – Doug Stevenson Jun 01 '19 at 13:45
  • @DougStevenson edited it again. What exactly do you mean my project directory structure? I assume you mean structure of my functions right? – delmin Jun 01 '19 at 13:55
  • @DougStevenson BTW I have tried to log a bug for the outdated example but I don't have an account so there is no way for me to do it and I'm not gonna register because of that now. Can you do it? – delmin Jun 01 '19 at 15:27

1 Answers1

2

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')
Doug Stevenson
  • 297,357
  • 32
  • 422
  • 441
  • Thank you very very much. That works now. I would probably never figure this out without you. Hopefully that will help with the signed url expiration date. Thank you again – delmin Jun 01 '19 at 14:40