数据库触发firebase功能从URL下载图像并将其保存到存储 [英] database triggers firebase function to download images from URL and save it to storage

查看:138
本文介绍了数据库触发firebase功能从URL下载图像并将其保存到存储的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想下载图片并将其保存到存储中,当我的数据库使用'photo_url'字段进行更新时

 出口.saveToStorage = functions.database.ref(`/ images / $ {itemImageRef}`)
.onWrite(event => {
const filePath = event.data.val();
const(filename)filePath.split('/')。pop();

var download = request.get(filePath).on('error',(err)=> {
)console.log(err)
})
.pipe(fs.createWriteStream(filename));

download.on('finish',()=> {
const bucket = gcs.bucket('id.appspot.com');
const storagePath =`images / $ {filename}`;

return bucket.upload(download ,({destination:storagePath})
.then(()=> {
console.log('success upload');
});
});
});

它会记录错误:EROFS:只读文件系统,在错误时打开'image.jpg' (本机)。我想我无法检索由createWriteStream保存的文件?
那么我应该如何从网上下载图片?

解决方案

使用@Jobsamuel建议的帖子,代码现在可以运行:

($ / $ / $ {itemImageRef}`)
.onWrite(event => {

$ const filePath = event.data.val();
const filename = filePath.split('/')。pop();

const bucket = gcs.bucket('id .appspot.com');
const remoteWriteStream = bucket.file(filename).createWriteStream({
metadata:{contentType:'image / jpeg'}
});

request(filePath).pipe(remoteWriteStream)
.on('error',(err)=> console.log(err))
.on('finish',() => console.log('success save image'));

});

通过将请求结果直接传递给存储桶,可以通过跳过写入本地文件,我怀疑是我原来的代码失败的原因。另外,不要忘记为图片设置contentType。


I want to download the image and save it to storage when my database is updated with the 'photo_url' field

exports.saveToStorage = functions.database.ref(`/images/${itemImageRef}`)
.onWrite(event => {
  const filePath = event.data.val();
  const filename = filePath.split('/').pop();

  var download = request.get(filePath).on('error', (err) => {
    console.log(err)
  })
  .pipe(fs.createWriteStream(filename));

  download.on('finish', () => {
    const bucket = gcs.bucket('id.appspot.com');
    const storagePath = `images/${filename}`;

    return bucket.upload(download, { destination: storagePath })
    .then(() => {
      console.log('success upload');
    });
  });
});

it logs "Error: EROFS: read-only file system, open 'image.jpg' at Error (native)." I suppose I cannot retrieve the file saved by createWriteStream? So how should I download images from the web?

解决方案

with the post suggested by @Jobsamuel, the code now works:

exports.saveToStorage = functions.database.ref(`/images/${itemImageRef}`)
.onWrite(event => {

  const filePath = event.data.val();
  const filename = filePath.split('/').pop();

  const bucket = gcs.bucket('id.appspot.com');
  const remoteWriteStream = bucket.file(filename).createWriteStream({
    metadata: { contentType: 'image/jpeg' }
  });

  request(filePath).pipe(remoteWriteStream)
  .on('error', (err) => console.log(err))
  .on('finish', () => console.log('success save image'));

});

By pipe the request result directly to the bucket, it solves the problem by skipping the step writing to a local file, which I suspect is the reason my original code fails. Also, don't forget to set contentType for images.

这篇关于数据库触发firebase功能从URL下载图像并将其保存到存储的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆