如何在Google Cloud Functions模拟器中读取本地文件? [英] How to read local files in the Google Cloud Functions emulator?

查看:90
本文介绍了如何在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

这篇关于如何在Google Cloud Functions模拟器中读取本地文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆