问题
My Google Cloud Function needs to read a local file (an image). I've added the image in the function directory tree, so I have:
packages.json
src/functions.js
lib/functions.js
img/shower.png
This is the code I'm using to read it:
var img = await new Jimp.read("../img/shower.png")
Now, when the function triggers, I'm getting this:
2017-03-30T03:00:25.118Z - error: (node:1685) UnhandledPromiseRejectionWarning: Unhandled promise rejection (rejection id: 1): Error: ENOENT: no such file or directory, open '../img/shower.png'
I've tried moving the image around, eg to the directory where my function resides (lib/) with no luck.
I've also logged process.cwd() which returned an unexpected: /usr/local/lib/node_modules/@google-cloud/functions-emulator
Any hints?
回答1:
Well, the trick is just reading the file from the absolute path
Jimp.read("__dirname+"img/shower.png")
This is the documentation for __dirname
来源:https://stackoverflow.com/questions/43117124/how-to-read-local-files-in-the-google-cloud-functions-emulator