简体   繁体   中英

Google Cloud Bucket filepath

I am trying to make a connection to DialogFlow through cloud function. For that, I need credentials that I received from google.

When I try to run code locally I can easily pass the credential.json file by specifying the path. But when I run code from the cloud function I received the error.

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

which is expected as there is no such path in cloud function. So i have uploaded file to google cloud bucket so i can have access to the file. Below is the code to access the bucket

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

after passing file to the DialogFlow to make connection i received another error

TypeError [ERR_INVALID_ARG_TYPE]: The "path" argument must be of type string. Received an instance of File

I don't know how can i pass the filepath i guess so that i can make connection to the DialogFlow

DialogFlow Connection code

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.");
  }
  
} 

This is where I pass the credential.json file

const sessionClient = new dialogflow.SessionsClient({keyFilename:
        
        //file
        // "G:/Business/SlipSlop/functions/chatDialogFlow/functions/credential.json"
       //  "functions/chatDialogFlow/functions/credential.json"
      
      });

What is working locally , if I pass the path as

"G:/Business/SlipSlop/functions/chatDialogFlow/functions/credential.json"

I am able to make the connection.

But when i deploy my code to the firebase cloud i don't know how to pass the path to JSON file.

Passing only the name of the file solves the problem.

instead of this

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

I only define file name as the file was present in my cloud function directory

"credential.json"

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM