我做了一个简单的音频创建web应用程序使用Node.js服务器。我想要创建音频使用云文本到语音API,然后上传到云存储音频。
(我使用Windows 10,Windows子系统用于Linux,Debian10.3和Google浏览器。)
这是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到云存储。
我能直接把response上传到云存储吗?还是必须将response保存在Node.js服务器中并将其上传到云存储中?
我在互联网上搜索,但找不到直接将response上传到云存储的方法。所以,如果你有提示,请告诉我。提前谢谢你。
发布于 2020-05-26 06:24:41
您应该能够这样做,所有的代码都在同一个文件中。要做到这一点,最好的方法就是使用云函数将文件发送到云存储。,但是,是的,您需要使用Node.js保存您的文件,因此,您将把它上传到Clou .
为此,您需要在本地保存您的文件,然后将其上传到云存储中。由于您可以在另一篇文章here中签入完整的教程,您需要构造该文件,在本地保存它,然后上传它。下面的代码是您需要在代码中添加的主要部分。
...
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,以防您对如何实现它有更多的疑问。
如果这些信息对你有帮助,请告诉我!
https://stackoverflow.com/questions/62014847
复制相似问题