我需要通过URL将文件保存在S3数字海洋空间中。它不能下载然后保存到S3,因为我们使用firebase函数,并且限制使用文件系统。有没有办法直接从url或其他方式保存它?例如通过流?
const fileName = 'image.jpg';
const url = 'imageUrl';
const res = await https.get(url, (stream) => stream.pipe(res));
await s3
.upload({
Bucket: 'name',
Key: `content/${fileName}`,
Body: res,
ACL: 'public-read'
})
.promise();
所有问题都在/tmp
目录的错误路径中。文档。Firebase函数的最终代码:
const fs = require('fs');
const s3 = require('../../services/storage');
const download = require('download');
const saveMediaItemToStorage = async (sourceId, item) => {
// * creating file name
const fileName = `${item.id}.${item.extension}`;
// * saving file to /tmp folder
await download(item.originalMediaUrl, '/tmp', { filename: fileName });
const blob = fs.readFileSync(`/tmp/${fileName}`);
// * saving file to s3
await s3
.upload({
Bucket: 'name',
Key: `content/${sourceId}/${fileName}`,
Body: blob,
ACL: 'public-read'
})
.promise();
// * remove file from temp folder
fs.unlink(`/tmp/${fileName}`, function (err) {
if (err) return console.log(err);
console.log('file deleted successfully');
});
};
module.exports = saveMediaItemToStorage;