【问题标题】:How to upload file in firebase cloud function into firebase storage from url?如何将firebase云功能中的文件从url上传到firebase存储中?
【发布时间】:2020-07-05 03:17:37
【问题描述】:

我正在使用一些 API,它可以通过 2 种方式给我文件:

- I can get FILE in response
- I can get direct URL to the file to download

我决定用第一种方法发出请求,得到 FILE 作为响应。我正在尝试通过 firebase 云功能将文件保存在 firebase 存储中:

exports.test = functions.region('europe-west1').https.onRequest(async (req, res) => {
  const axios = require('axios').default

  axios({
    method: 'get',
    url: `https://cloudpbx.beeline.ru/apis/portal/v2/records/06365a27-f8d1-4c51-bba3-a08802429964/9052948777%40mpbx.sip.beeline.ru/download`
  }).then((resp) => {
    console.log('resp', resp.data)

    const bucket = admin.storage().bucket('cardbox-1.appspot.com/mcun/calls')
    bucket.upload(resp.data)
  })
})

resp.data 我有类似的东西:

��H�Xing��h  $&)+-.179:>ACEGINRTW[_acegjmoqtwz|����������������������������������������������������PLAME3.100(,�$!��h�6���H��V]@mdE�E��"@���}�8kwT��&���>:P�2��>?�����8 ����������*���Z��XVC�(b�k�
D�G��

我可以自己看到它的文件,但是如何处理它,以及如何将它上传到 Firebase 存储?另外,我不确定创建存储桶和使用存储 api 是否正确。

请随时提出请求,无需身份验证即可获得响应。该文件必须是 mp3 类型

这里是关于我从响应中得到的标题的日志:

HTTP/1.1 200 OK
Server: nginx/1.6.2
Date: Tue, 24 Mar 2020 16:19:55 GMT
Content-Type: application/octet-stream;charset=utf-8
Content-Length: 48744
Connection: close
Content-Disposition: attachment; filename=file.mp3

��H�Xing��h  $&)+-.179:>ACEGINRTW[_acegjmoqtwz|����������������������������������������������������PLAME3.100(,�$!��h�6���H��V]@mdE�E��"@���}�8kwT��&���>:P�2��>?�����8 ����������*���Z��XVC�(b�k�
D�G��
C���al�ꝰ�z�KY�Q�]ЪF�8�^���W;,�LUteA�%�u�&��.���
0�q��Cڟ#���6��@�K�h��Itᠠ|[�q�{j'�+  �ʤS<F�  Y�5D]?�����nlƦBC�S�Zx'�e�D�Fi_��Vl��4����H��E�z���SMVПQ�P� AT<���(�x�+3�ТB����e�ZC}t��Ї�#֗����2���Kve���.�ԏ�UH���u����ʘfbC��C� ��C�+ġ��̺��R#��|x5hq`Х3�?�N(���.5��� c+��`�Y�F8�d�B&53��¥�L�cx4��3��q�JHԓJ�43Ώ�(к�4r ��a �D
Dɍ0�

【问题讨论】:

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


    【解决方案1】:

    嗯,看来这里有几个任务要做:)

    根据upload method 的定义及其示例,使用 node js 作为客户端库(这完全有意义)。您将能够将文件上传到存储桶。但老实说,我对 pathString 参数的用法不是很清楚,因为它指向的是本地路径。也许您提到的 URL 选项可以很好地作为一项功能进行测试。

    只需粘贴文档中的示例:

      // Imports the Google Cloud client library
      const {Storage} = require('@google-cloud/storage');
    
      // Creates a client
      const storage = new Storage();
    
      async function uploadFile() {
        // Uploads a local file to the bucket
        await storage.bucket(bucketName).upload(filename, {
          gzip: true,
          metadata: {            
            Content-Type: 'audio/mpeg'            
          },
        });
    
            console.log(`${filename} uploaded to ${bucketName}.`);
      }
    
      uploadFile().catch(console.error);
    

    实现此目的的另一种方法是使用JSON API 向 Cloud Storage 的上传方法发出 POST 请求。据我了解,这些对象在云存储中是 saved 作为“RAW”。

    如果您担心转换为 MP3 文件,我认为answer 解释得很好。基本上,如果您想在 Firebase 函数中执行转换,根据文件的大小/时间,可能不是最佳方案。

    祝你好运!

    【讨论】:

      猜你喜欢
      • 2017-12-21
      • 2021-10-28
      • 2021-04-21
      • 1970-01-01
      • 2019-08-08
      • 2018-11-17
      • 2017-12-19
      • 2021-10-16
      相关资源
      最近更新 更多