1

I'm using some API, that can give me file with 2 ways:

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

I decided to make request with first method, get FILE in response. I'm trying to save the file in firebase storage, from firebase cloud function:

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

In resp.data i have something like that:

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

As i can see its file by itself, but how to handle it, and how upload it into firebase storage? Also, i'm not sure, that i'm correct creating a bucket and using storage api.

Please feel free to make request, you can get response without auth. That file must be mp3 type

Here is the logs with headers about what i'm getting from response:

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�
WhoIsDT
  • 695
  • 2
  • 9
  • 27

1 Answers1

1

Well, it seems that there are a couple of tasks to do here :)

According to the definition of the upload method and their examples, using node js as client library (that makes total sense). You will be able to Upload a file to the bucket. But to be honest, I am not very clear about the usage of the pathString parameter since it points to a local path. Maybe the URL option that you mentioned would be great to test as a feature.

Just pasting the example from docs:

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

Another way to accomplish this, is to make a POST request with the JSON API to the upload method of Cloud Storage. As I understand, the objects are saved as "RAW" in Cloud Storage.

If you are concerned about the conversion to MP3 file, I think this answer explains very well. Basically, if you want to perform the conversion in the Firebase Function, maybe is not the best scenario according to the size/time of the files.

Good Luck!

Mario
  • 406
  • 2
  • 6