How to read local files in the Google Cloud Functions emulator?

岁酱吖の 提交于 2020-06-27 20:28:12

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!