[英]How to upload Google Cloud text to speech API's response to Cloud Storage [Node.js]
我使用 Node.js 服务器制作了一个简单的音频创建 web 应用程序。 我想使用云文本到语音 API 创建音频,然后将该音频上传到云存储。
(我使用 Windows 10、Windows 子系统 Linux、Z6FE8B98C53E8D 和 099686Z3D6732 浏览器。3)
这是 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
上传到 Cloud Storage。
我可以直接将response
上传到 Cloud Storage 吗? 或者我是否必须在 Node.js 服务器中保存response
并将其上传到云存储?
我在网上搜索,但没有找到直接将response
上传到云存储的方法。 所以,如果你有提示,请告诉我。 先感谢您。
您应该能够做到这一点,所有代码都在同一个文件中。 实现这一目标的最佳方式是使用 Cloud Function,它将文件发送到您的云存储。 但是,是的,您需要使用 Node.js 保存文件,然后将其上传到 Clou Storage。
为此,您需要将文件保存在本地,然后将其上传到云存储。 您可以在此处查看另一篇文章中的完整教程,您需要构建文件,将其保存在本地,然后上传。 下面的代码是您需要在代码中添加的主要部分。
...
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);
});
...
实现此部分后,您将下载保存在本地的文件并准备好上传。 我建议您更好地查看我提到的另一篇文章,以防您对如何实现它有更多疑问。
让我知道这些信息是否对您有帮助!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.