我使用的是一个名为opentok的服务,它将视频存储在他们的云上,并在文件准备好时给我一个回调url,这样我就可以下载它并存储在我的云提供商上。
我们在工作的地方使用gcloud,我需要下载文件,然后使用firebase云函数将其存储在我的gcloud存储桶中。
下面是我的代码:
const archiveFile = await axios.get(
'https://sample-videos.com/video701/mp4/720/big_buck_bunny_720p_2mb.mp4'
);
console.log('file downloaded from opentokCloud !');
fs.writeFile('archive.mp4', archiveFile, err => {
if (err) throw err;
// success case, the file was saved
console.log('File Saved in container');
});
await firebaseBucket.upload('archive.mp4', {
gzip: true,
// destination: `archivedStreams/${archiveInfo.id}/archive.mp4`,
destination: 'test/lapin.mp4',
metadata: {
cacheControl: 'no-cache',
},
});我尝试将下载的文件直接放在upload()中,但它不起作用,我必须提供一个字符串(我的文件的路径),我如何在云函数中有我下载的文件的路径?它是仍然在我的容器的RAM中还是在缓存文件夹中?
如您所见,我尝试使用FS编写,但在云函数的容器中没有写访问权限
提前感谢社区
发布于 2020-03-30 18:25:08
如果有人在未来寻找这个问题,这里是我解决这个问题的方法:
使用Firebase云函数,您可以在/tmp中编写临时文件(有关更多信息https://cloud.google.com/functions/docs/concepts/exec#file_system,请参阅此处)
我通过使用node-fetch包和Node.JS的writeStream函数解决了这个问题:
const fetch = require('node-fetch');
const fs = require('fs');
await fetch(archiveInfo.url).then(res => {
console.log('start writing data');
const dest = fs.createWriteStream('/tmp/archive.mp4');
res.body.pipe(dest);
//Listen when the writing of the file is done
dest.on('finish', async () => {
console.log('start uploading in gcloud !');
await firebaseBucket.upload('/tmp/archive.mp4', {
gzip: true,
destination: `{pathToFileInBucket}/archive.mp4`,
metadata: {
cacheControl: 'no-cache',
},
});
console.log('uploading finished');
});
});firebaseBucket是我在别处配置的gcloud存储桶,使用@google-cloud/storage定义您自己的存储桶
由于我的函数是由link触发的,所以不要忘记发出响应并捕获错误,以避免云函数超时(无用运行,无用计费:D)
https://stackoverflow.com/questions/60889839
复制相似问题