通过Google Cloud Function将位置路径设置为Firebase存储? [英] Set the location path to Firebase Storage from a Google Cloud Function?

查看:94
本文介绍了通过Google Cloud Function将位置路径设置为Firebase存储?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在努力从Google文本语音转换获取音频文件,然后将其写入Firebase存储.我不知道在哪里指定存储位置的路径.我试过了:

I'm working on getting an audiofile from Google Text-to-Speech and then writing the file to Firebase Storage. I don't understand where I specify the path to the location in Storage. I tried:


    const bucket = storage.bucket('myProject-cd99d.appspot.com/Audio/Spanish/test.ogg');

,但是我收到一条错误消息:TypeError: Path must be a string.这是云功能:

but I get an error message: TypeError: Path must be a string. Here's the cloud function:

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';
        // copied from https://cloud.google.com/text-to-speech/docs/quickstart-client-libraries#client-libraries-usage-nodejs
        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/Audio/Spanish/test.ogg');

        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
        // response.audioContent is the downloaded file
        await bucket.upload(response.audioContent, {
          metadata: 'public, max-age=31536000'
        })
        .then(function() {
          console.log('Uploaded file.');
        })
        .catch(function(error) {
          console.error(error);
        });
      }
      catch (error) {
        console.error(error);
      }
    }
    test();
  } // close if
  return 0;
});

我还需要返回主函数.

推荐答案

在Cloud Storage中,存储桶是指文件所在的容器.它并不指向文件本身.看来您假设存储桶实际上是一个文件,这是不正确的.

In Cloud Storage, a bucket refers to the container where your files go. It doesn't refer to a file itself. It looks like you're assuming that a bucket is actually a file, which is not correct.

您的存储桶名称是:

const bucketName = 'myProject-cd99d.appspot.com'

您在该存储桶中的文件路径是这样的:

Your file path in that bucket is this:

const filePath = '/Audio/Spanish/test.ogg'

您可以像这样构建 Bucket 对象:

You build a Bucket object like this:

const bucket = storage.bucket(bucketName)

然后使用将文件上传到该路径. Bucket上的upload()方法:

bucket.upload(response.audioContent, {
    destination: filePath
})

请务必点击所有这些指向API文档的链接,以便您可以更好地了解API的工作原理.

Be sure to click through to all these links to the API docs so you can better understand how the APIs work.

这篇关于通过Google Cloud Function将位置路径设置为Firebase存储?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆