如何在Google Cloud Functions模拟器中读取本地文件?
我的Google Cloud Function需要读取本地文件(图像). 我已经在功能目录树中添加了图像,所以我有:
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")
现在,当函数触发时,我得到了:
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'
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'
我尝试将图像移动到例如我的函数所在的目录(lib/
),但是没有运气.
I've tried moving the image around, eg to the directory where my function resides (lib/
) with no luck.
我还记录了process.cwd()
,该返回了意外:/usr/local/lib/node_modules/@google-cloud/functions-emulator
I've also logged process.cwd()
which returned an unexpected: /usr/local/lib/node_modules/@google-cloud/functions-emulator
有任何提示吗?
好吧,诀窍就是从绝对路径读取文件
Well, the trick is just reading the file from the absolute path
Jimp.read("__dirname+"img/shower.png"
)
Jimp.read("__dirname+"img/shower.png"
)
这是 __dirname