微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

Google Cloud Bucket 文件路径

如何解决Google Cloud Bucket 文件路径

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

当我尝试在本地运行代码时,我可以通过指定路径轻松传递 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]:“路径”参数必须是字符串类型。收到一个文件实例

我不知道如何传递我猜的文件路径,以便我可以连接到 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 文件

解决方法

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

代替这个

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

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

"credential.json"

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。