代码之家  ›  专栏  ›  技术社区  ›  Thomas David Kehoe

从Google云函数设置Firebase存储的位置路径?

  •  0
  • Thomas David Kehoe  · 技术社区  · 7 年前

    我正在把一个音频文件从Google文本转换成语音,然后把这个文件写到Firebase存储中。我不明白我在哪里指定了存储位置的路径。我试过:

    
        const bucket = storage.bucket('myProject-cd99d.appspot.com/Audio/Spanish/test.ogg');
    
    

    但我收到一条错误信息: TypeError: Path must be a string . 以下是云函数:

    exports.Google_T2S = functions.firestore.document('Users/{userID}/Spanish/T2S_Request').onUpdate((change, context) => { 
      if (change.after.data().word != undefined) {
    
        // Performs the Text-to-Speech request
        async function test() {
          try {
            const word = change.after.data().word; // the text
            const longLanguage = 'Spanish';
            const audioFormat = '.mp3';
            // copied from https://cloud.google.com/text-to-speech/docs/quickstart-client-libraries#client-libraries-usage-nodejs
            const fs = require('fs');
            const util = require('util');
            const textToSpeech = require('@google-cloud/text-to-speech'); // Imports the Google Cloud client library
            const client = new textToSpeech.TextToSpeechClient(); // Creates a client
    
            let myWordFile = word.replace(/ /g,"_"); // replace spaces with underscores in the file name
            myWordFile = myWordFile.toLowerCase(); // convert the file name to lower case
            myWordFile = myWordFile + audioFormat; // append .mp3 to the file name;
    
            // boilerplate copied from https://cloud.google.com/blog/products/gcp/use-google-cloud-client-libraries-to-store-files-save-entities-and-log-data
            const {Storage} = require('@google-cloud/storage');
            const storage = new Storage();
            const bucket = storage.bucket('myProject-cd99d.appspot.com/Audio/Spanish/test.ogg');
    
            const request = {     // Construct the request
              input: {text: word},
              // Select the language and SSML Voice Gender (optional)
              voice: {languageCode: 'es-ES', ssmlGender: 'FEMALE'},
              // Select the type of audio encoding
              audioConfig: {audioEncoding: 'MP3'},
            };
    
            const [response] = await client.synthesizeSpeech(request);
            // Write the binary audio content to a local file
            // response.audioContent is the downloaded file
            await bucket.upload(response.audioContent, {
              metadata: 'public, max-age=31536000'
            })
            .then(function() {
              console.log('Uploaded file.');
            })
            .catch(function(error) {
              console.error(error);
            });
          }
          catch (error) {
            console.error(error);
          }
        }
        test();
      } // close if
      return 0;
    });
    

    我还需要返回main函数。

    1 回复  |  直到 7 年前
        1
  •  1
  •   Doug Stevenson    7 年前

    你的名字是:

    const bucketName = 'myProject-cd99d.appspot.com'
    

    const filePath = '/Audio/Spanish/test.ogg'
    

    你建立了一个 Bucket 对象如下:

    const bucket = storage.bucket(bucketName)
    

    upload() 铲斗上的方法:

    bucket.upload(response.audioContent, {
        destination: filePath
    })
    

    一定要点击到所有这些API文档的链接,这样您就可以更好地理解API是如何工作的。