Google Cloud Bucket 文件路径

Google Cloud Bucket filepath

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

收到的凭据

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

Error: ENOENT: no such file or directory, open '/workspace/G:/Business/SlipSlop/functions/chatDialogFlow/functions/credential.json'

这是预期的,因为云函数中没有这样的路径。所以我已将文件上传到 google 云存储桶,这样我就可以访问该文件。下面是访问存储桶的代码

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

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

TypeError [ERR_INVALID_ARG_TYPE]: The "path" argument must be of type string. Received an instance of 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 文件。

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

而不是这个

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

我只定义文件名,因为该文件存在于我的云功能目录中

"credential.json"