Firebase cloud function for file upload

后端 未结 2 554
北海茫月
北海茫月 2020-12-03 07:13

I have this cloud function that I wrote to upload file to google cloud storage:

const gcs = require(\'@google-cloud/storage\')({keyFilename:\'2fe4e3d2bfdc.js         


        
2条回答
  •  孤街浪徒
    2020-12-03 08:13

    I managed this by downloading the file to the tmp instead.

    You will need:

    const mkdirp = require('mkdirp-promise');
    

    Then, inside onChange. I created tempLocalDir like so:

    const LOCAL_TMP_FOLDER = '/tmp/';
    const fileDir = (the name of the file); //whatever method you choose to do this
    const tempLocalDir = `${LOCAL_TMP_FOLDER}${fileDir}`;
    

    Then I use mkdirp to make the temp directory

    return mkdirp(tempLocalDir).then(() => {
    // Then Download file from bucket.
    const bucket = gcs.bucket(object.bucket);
    return bucket.file(filePath).download({
      destination: tempLocalFile
    }).then(() => {
        console.log('The file has been downloaded to', tempLocalFile);
        //Here I have some code that converts images then returns the converted image
        //Then I use
        return bucket.upload((the converted image), {
            destination: (a file path in your database)
        }).then(() => {
            console.log('JPEG image uploaded to Storage at', filePath);
        })//You can perform more actions of end the promise here
    

    I think my code achieves that you were trying to accomplish. I hope this helps; I can offer more code if necessary.

提交回复
热议问题