寶慕林4294392
2022-11-11 14:03:02
我有一個(gè)程序允許用戶將視頻文件上傳到 Firebase 存儲(chǔ)。如果文件不是 mp4,我會(huì)將其發(fā)送到第三方視頻轉(zhuǎn)換站點(diǎn)以將其轉(zhuǎn)換為 mp4。他們使用 URL 和有關(guān)轉(zhuǎn)換文件的其他信息點(diǎn)擊了一個(gè) webhook(firebase 函數(shù))。現(xiàn)在我正在嘗試將文件下載到 firebase 函數(shù)中的 tmp 目錄,然后將其發(fā)送到 firebase 存儲(chǔ)。我有以下問(wèn)題。我可以繞過(guò)將文件下載到函數(shù) tmp 目錄并直接將其保存到存儲(chǔ)中嗎?如果是這樣怎么辦?我目前無(wú)法下載到函數(shù) tmp dir 下面是我的代碼。函數(shù)正在返回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 } }
1 回答

躍然一笑
TA貢獻(xiàn)1826條經(jīng)驗(yàn) 獲得超6個(gè)贊
如上文評(píng)論部分所述,您可以使用Cloud Storage Node.js SDK將文件上傳到 Cloud Storage。
請(qǐng)查看SDK 客戶端參考文檔,您可以在其中找到有關(guān)此 Cloud Storage 客戶端庫(kù)的大量示例和更多信息。
另外,我想提醒您,您可以使用管道繞過(guò)寫(xiě)入/tmp 。根據(jù) Cloud Functions 的文檔,“您可以通過(guò)創(chuàng)建讀取流、將其傳遞給基于流的進(jìn)程并將輸出流直接寫(xiě)入 Cloud Storage 來(lái)處理 Cloud Storage 上的文件?!?/em>
最后但同樣重要的是,始終從 Cloud Function 的本地系統(tǒng)中刪除臨時(shí)文件。不這樣做最終會(huì)導(dǎo)致內(nèi)存不足問(wèn)題和隨后的冷啟動(dòng)。
添加回答
舉報(bào)
0/150
提交
取消