簡體   English   中英

使用Node fs.writeFile將文件從Google Cloud Function寫入Firebase存儲嗎?

[英]Write a file from a Google Cloud Function to Firebase Storage using Node fs.writeFile?

我從Google文本語音轉換獲取音頻文件,然后將文件寫入Firebase存儲。 我從本文檔復制了Text-to-Speech代碼,並從此處復制了Firebase Storage代碼。 這是我的Google Cloud Function:

    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';

            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');
            const file = bucket.file('Audio/' + longLanguage + '/' + myWordFile);

            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
            const writeFile = util.promisify(fs.writeFile);
            await writeFile(file, response.audioContent, 'binary');
          }
          catch (error) {
            console.error(error);
          }
        }
        test();
      } // close if
      return 0;
    });

問題在這里:

    const file = bucket.file('Audio/' + longLanguage + '/' + myWordFile);
    const writeFile = util.promisify(fs.writeFile);
    await writeFile(file, response.audioContent, 'binary');

fs.writeFile期望filestringBufferURLinteger 我不喜歡加入Google Cloud Storage功能。 我應該放在那兒?

Node的fs模塊僅用於寫入本地文件系統,因此您不能使用它來寫入Cloud Storage。 如果要寫入Cloud Storage,則必須使用Cloud Storage SDK (或包裝Cloud Storage SDK的Admin SDK)。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM