【问题标题】:Google Cloud Bucket filepath谷歌云存储桶文件路径
【发布时间】:2021-01-20 13:33:44
【问题描述】:

我正在尝试通过云功能连接到 DialogFlow。为此,我需要从 google 收到的凭据。

当我尝试在本地运行代码时,我可以通过指定路径轻松地传递 credential.json 文件。但是当我从云函数运行代码时,我收到了错误。

错误:ENOENT:没有这样的文件或目录,打开 '/workspace/G:/Business/SlipSlop/functions/chatDialogFlow/functions/credential.json'

这是预期的,因为云功能中没有这样的路径。所以我已将文件上传到谷歌云存储桶,以便我可以访问该文件。下面是访问bucket的代码

const storage = new Storage();
const bucket = storage.bucket("d-slslop-bucket-01");
const file = bucket.file("credential.json"); 

在将文件传递给 DialogFlow 以建立连接后,我收到另一个错误

TypeError [ERR_INVALID_ARG_TYPE]:“路径”参数必须是字符串类型。收到一个 File 实例

我不知道如何传递我猜想的文件路径,以便我可以连接到 DialogFlow

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 文件。

【问题讨论】:

  • 如果您不在 SessionsClient 中使用 keyFileName 会发生什么?您是否尝试过 Firebase 功能?通常您将使用 Cloud Functions 默认服务帐户,应该足够了。
  • @gaillaume blaquiere 感谢您抽出宝贵时间。当我只提到文件名而不是完整路径时,问题就解决了。由于该文件存在于云函数目录中。
  • 使用这个文件会导致你遇到一些关于秘密管理的问题。如果您处理此类问题/问题,请考虑我的评论;-)
  • 是的,你是对的,但只要凭证文件在谷歌服务器中,我相信它是安全的。与客户端一样,没有人可以访问该文件。

标签: javascript google-cloud-functions google-cloud-storage dialogflow-es


【解决方案1】:

只传递文件名即可解决问题。

而不是这个

"https://storage.googleapis.com/d-slslop-bucket-01/credential.json"

我只定义文件名,因为文件存在于我的云函数目录中

"credential.json"

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-12-21
    • 2023-02-13
    • 1970-01-01
    • 2016-01-31
    • 2019-08-24
    • 1970-01-01
    • 2020-06-01
    • 2017-05-13
    相关资源
    最近更新 更多