Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/google-cloud-platform/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何将谷歌云文本上传到语音API';s对云存储的响应[Node.js]_Node.js_Google Cloud Platform_Google Cloud Storage_Google Text To Speech - Fatal编程技术网

如何将谷歌云文本上传到语音API';s对云存储的响应[Node.js]

如何将谷歌云文本上传到语音API';s对云存储的响应[Node.js],node.js,google-cloud-platform,google-cloud-storage,google-text-to-speech,Node.js,Google Cloud Platform,Google Cloud Storage,Google Text To Speech,我使用Node.js服务器制作了一个简单的音频创建web应用程序。我想使用云文本到语音API创建音频,然后将音频上传到云存储 (我使用Windows 10、Windows Linux子系统、Debian 10.3和Google Chrome浏览器。) 这是Node.js服务器中的代码 const client = new textToSpeech.TextToSpeechClient(); async function quickStart() { //

我使用Node.js服务器制作了一个简单的音频创建web应用程序。我想使用云文本到语音API创建音频,然后将音频上传到云存储

(我使用Windows 10、Windows Linux子系统、Debian 10.3和Google Chrome浏览器。)

这是Node.js服务器中的代码

const client = new textToSpeech.TextToSpeechClient();
        async function quickStart() {

            // The text to synthesize
            const text = 'hello, world!';

            // Construct the request
            const request = {
                input: {text: text},
                // Select the language and SSML voice gender (optional)
                voice: {languageCode: 'en-US', ssmlGender: 'NEUTRAL'},
                // select the type of audio encoding
                audioConfig: {audioEncoding: 'MP3'},
            };

            // Performs the text-to-speech request
            const [response] = await client.synthesizeSpeech(request);
            // Write the binary audio content to a local file
            console.log(response);
我想上传
response
到云存储

我可以直接将
响应
上传到云存储吗?或者我必须在Node.js服务器中保存
响应
,并将其上传到云存储吗


我搜索了互联网,但找不到将
response
直接上传到云存储的方法。所以,如果你有一个提示,请告诉我。提前感谢您。

您应该能够做到这一点,所有代码都在同一个文件中。实现这一点的最佳方法是使用云功能,即将文件发送到云存储但是,是的,您需要使用Node.js保存文件,然后将其上载到Clou存储。

要实现这一点,您需要在本地保存文件,然后将其上载到云存储。正如您可以在另一篇文章中查看完整的教程一样,您需要构建文件,将其保存在本地,然后上传。下面的代码是您需要添加到代码中的主要部分

...
const options = { // construct the file to write
     metadata: {
          contentType: 'audio/mpeg',
          metadata: {
              source: 'Google Text-to-Speech'
          }
     }
};

// copied from https://cloud.google.com/text-to-speech/docs/quickstart-client-libraries#client-libraries-usage-nodejs
const [response] = await client.synthesizeSpeech(request);
// Write the binary audio content to a local file
// response.audioContent is the downloaded file
     return await file.save(response.audioContent, options)
     .then(() => {
         console.log("File written to Firebase Storage.")
         return;
     })
     .catch((error) => {
         console.error(error);
     });
...
一旦实现了此部分,您将下载本地保存的文件并准备好上载。我建议你仔细看看我写的另一篇文章,以防你对如何实现它有更多的疑问


如果这些信息对你有帮助,请告诉我

非常感谢您的详细回答。你的解决方案很有效!