首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何上传Google文本到语音API对云存储的响应[Node.js]

如何上传Google文本到语音API对云存储的响应[Node.js]
EN

Stack Overflow用户
提问于 2020-05-26 04:46:06
回答 1查看 631关注 0票数 0

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

(我使用Windows 10,Windows子系统用于Linux,Debian10.3和Google浏览器。)

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

代码语言:javascript
复制
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到云存储。

我能直接把response上传到云存储吗?还是必须将response保存在Node.js服务器中并将其上传到云存储中?

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

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2020-05-26 06:24:41

您应该能够这样做,所有的代码都在同一个文件中。要做到这一点,最好的方法就是使用云函数将文件发送到云存储。,但是,是的,您需要使用Node.js保存您的文件,因此,您将把它上传到Clou .

为此,您需要在本地保存您的文件,然后将其上传到云存储中。由于您可以在另一篇文章here中签入完整的教程,您需要构造该文件,在本地保存它,然后上传它。下面的代码是您需要在代码中添加的主要部分。

代码语言:javascript
复制
...
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);
     });
...

一旦实现了此部分,您将获得保存在本地下载并准备上载的文件。我建议您更好地查看另一篇文章I mentioned,以防您对如何实现它有更多的疑问。

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

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/62014847

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档