简体   繁体   中英

Download file to Firebase Storage via Firebase Functions

I have a program that allows users to upload video files to firebase storage. If the file is not an mp4 I then send it to a third party video converting site to convert it to mp4. They hit a webhook(firebase function) with the URL and other information about the converted file.

Right now I'm trying to download the file to the tmp dir in firebase functions and then send it to firebase storage. I have the following questions.

  1. Can I bypass downloading a file to the functions tmp dir and just save it directly to storage? if so how?

  2. I'm currently having trouble downloading to the functions tmp dir below is my code. The function is returning Function execution took 6726 ms, finished with status: 'crash'

export async function donwloadExternalFile(url:string, fileName:string) {

    try {
      
        const axios  = await import('axios')
        const fs = await import('fs')  

        const workingDir = join(tmpdir(), 'downloadedFiles')
        const tmpFilePath = join(workingDir, fileName) 

        const writer = fs.createWriteStream(tmpFilePath);

        const response = await axios.default.get(url, { responseType: 'stream' })

        response.data.pipe(writer);

        await new Promise((resolve, reject) => {
            writer.on('error', err => {
                writer.close();
                reject(err);
            });

            writer.on('close', () => {
                resolve();
            });
        });

        return 

    } catch (error) {
        throw error 
    }

  }

As mentioned above in the comments section, you can use the Cloud Storage Node.js SDK to effectuate the upload of your file to Cloud Storage.

Please take a look at the SDK Client reference documentation where you can find numerous samples and more information about this Cloud Storage client library.

Also, I'd like to remind you that you can bypass writing to /tmp by using a pipeline. According to the documentation for Cloud Functions, "you can process a file on Cloud Storage by creating a read stream, passing it through a stream-based process, and writing the output stream directly to Cloud Storage."

Last but not least, always delete temporary files from the Cloud Function's local system. Failing to do so can eventually result in out-of-memory issues and subsequent cold starts.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM