【问题标题】:Write a file from a Google Cloud Function to Firebase Storage using Node fs.writeFile?使用 Node fs.writeFile 将文件从 Google Cloud Function 写入 Firebase Storage?
【发布时间】:2019-06-11 00:30:20
【问题描述】:

我从 Google Text-to-Speech 获取音频文件,然后我想将这些文件写入 Firebase 存储。我从 this documentation 复制了 Text-to-Speech 代码,从 here 复制了 Firebase 存储代码。这是我的 Google Cloud 函数:

    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。我不喜欢我放入谷歌云存储功能。我应该放什么?

【问题讨论】:

    标签: node.js firebase google-cloud-storage firebase-storage fs


    【解决方案1】:

    Node 的fs 模块仅用于写入本地文件系统,因此您不能使用它来写入云存储。如果要写入 Cloud Storage,则必须使用 Cloud Storage SDK(或封装了 Cloud Storage SDK 的 Admin SDK)。

    【讨论】:

    猜你喜欢
    • 2019-02-14
    • 2020-05-05
    • 2021-04-25
    • 1970-01-01
    • 1970-01-01
    • 2019-11-12
    • 2021-12-28
    • 2018-09-07
    相关资源
    最近更新 更多