Javascript 谷歌云存储桶文件路径
我正在尝试通过云函数与DialogFlow建立连接。为此,我需要从谷歌获得的证书 当我尝试在本地运行代码时,我可以通过指定路径轻松地传递credential.json文件。但是当我从云函数运行代码时,我收到了错误 错误:enoint:没有这样的文件或目录,打开“/workspace/G:/Business/SlipSlop/functions/chatdiologflow/functions/credential.json” 这是预期的,因为在云函数中没有这样的路径。所以我已经将文件上传到谷歌云存储桶,这样我就可以访问该文件。下面是访问铲斗的代码Javascript 谷歌云存储桶文件路径,javascript,google-cloud-functions,google-cloud-storage,dialogflow-es,Javascript,Google Cloud Functions,Google Cloud Storage,Dialogflow Es,我正在尝试通过云函数与DialogFlow建立连接。为此,我需要从谷歌获得的证书 当我尝试在本地运行代码时,我可以通过指定路径轻松地传递credential.json文件。但是当我从云函数运行代码时,我收到了错误 错误:enoint:没有这样的文件或目录,打开“/workspace/G:/Business/SlipSlop/functions/chatdiologflow/functions/credential.json” 这是预期的,因为在云函数中没有这样的路径。所以我已经将文件上传到谷歌云
const storage = new Storage();
const bucket = storage.bucket("d-slslop-bucket-01");
const file = bucket.file("credential.json");
在将文件传递到DialogFlow以建立连接之后,我收到了另一个错误
TypeError[ERR_INVALID_ARG_TYPE]:“path”参数必须是string类型。收到文件的一个实例
我不知道如何传递我猜的文件路径,以便连接到DialogFlow
对话框流量连接代码
async function runSample(input , projectId = "****-****") {
const storage = new Storage();
const bucket = storage.bucket("d-slslop-bucket-01");
const file = bucket.file("credential.json");
console.log("File path: " + file);
// A unique identifier for the given session
const sessionId = uuid.v4();
// Create a new session
const sessionClient = new dialogflow.SessionsClient({keyFilename:
"https://storage.googleapis.com/d-slslop-bucket-01/credential.json"
//file
// "G:/Business/SlipSlop/functions/chatDialogFlow/functions/credential.json"
// "functions/chatDialogFlow/functions/credential.json"
});
const sessionPath = sessionClient.projectAgentSessionPath(projectId, sessionId);
// The text query request.
const request = {
session: sessionPath,
queryInput: {
text: {
// The query to send to the dialogflow agent
text: input,
// The language used by the client (en-US)
languageCode: "en-US",
},
},
};
// Send request and log result
const responses = await sessionClient.detectIntent(request);
console.log("Detected intent");
const result = responses[0].queryResult;
console.log(" Query: "+result.queryText);
console.log(" Response: "+result.fulfillmentText);
if (result.intent) {
console.log(" Intent: "+result.intent.displayName);
} else {
console.log(" No intent matched.");
}
}
这就是我传递credential.json文件的地方
const sessionClient = new dialogflow.SessionsClient({keyFilename:
//file
// "G:/Business/SlipSlop/functions/chatDialogFlow/functions/credential.json"
// "functions/chatDialogFlow/functions/credential.json"
});
如果我通过路径作为
"G:/Business/SlipSlop/functions/chatDialogFlow/functions/credential.json"
我能接通
但是,当我将代码部署到firebase云时,我不知道如何将路径传递到JSON文件。只传递文件名就可以解决问题 而不是这个
"https://storage.googleapis.com/d-slslop-bucket-01/credential.json"
我只定义文件名,因为该文件存在于我的云函数目录中
"credential.json"
如果在SessionClient中不使用keyFileName会发生什么?你试过Firebase的功能吗?通常情况下,您将使用云功能默认服务帐户,这应该足够了。@gaillaume blaquiere感谢您抽出时间。当我只提到文件名而不是完整路径时,问题就解决了。因为该文件存在于云函数目录中。使用该文件将导致您遇到有关机密管理的一些问题。如果你处理这类问题,想想我的评论;-)是的,你是对的,但只要凭证文件在谷歌服务器上,我相信它是安全的。与客户端相同,没有人可以访问该文件。