【问题标题】:Set the location path to Firebase Storage from a Google Cloud Function?从 Google Cloud 函数设置到 Firebase 存储的位置路径?
【发布时间】:2019-06-11 03:12:15
【问题描述】:

我正在从 Google Text-to-Speech 获取音频文件,然后将文件写入 Firebase 存储。我不明白我在哪里指定存储位置的路径。我试过了:


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

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

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;
});

我还需要返回main函数。

【问题讨论】:

    标签: firebase google-cloud-platform firebase-storage


    【解决方案1】:

    在 Cloud Storage 中,存储分区是指存放文件的容器。它不引用文件本身。看起来您假设存储桶实际上是一个文件,这是不正确的。

    您的存储桶名称是这样的:

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

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

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

    你像这样构建一个Bucket 对象:

    const bucket = storage.bucket(bucketName)
    

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

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

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

    【讨论】:

    • Doug,bucket.upload() 的 API 文档说第一个参数是要上传的文件:upload(pathString, options, callback) 要上传的文件的完全限定路径到你的桶。您说,“然后使用 Bucket 上的 upload() 方法将文件上传到该路径:”我认为您的意思是“将文件从该路径上传到存储桶”。我写了一个新问题,期待您的回答:stackoverflow.com/questions/54241849/…
    • 我在这里编辑了答案以反映正确的用法。
    猜你喜欢
    • 2021-04-10
    • 2020-02-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-10-03
    • 2019-11-06
    • 1970-01-01
    • 2016-08-18
    相关资源
    最近更新 更多