Upload image from URL to Firebase Storage

后端 未结 4 2305
慢半拍i
慢半拍i 2020-12-03 06:26

I\'m wondering how to upload file onto Firebase\'s storage via URL instead of input (for example). I\'m scrapping images from a website and retrieving their URLS. I want to

4条回答
  •  清歌不尽
    2020-12-03 06:38

    This answer is similar to @HalesEnchanted's answer but with less code. In this case it's done with a Cloud Function but I assume the same can be done from the front end. Notice too how createWriteStream() has an options parameter similar to bucket.upload().

    const fetch = require("node-fetch");
    
    const bucket = admin.storage().bucket('my-bucket');
    const file = bucket.file('path/to/image.jpg');
    
    fetch('https://example.com/image.jpg').then((res: any) => {
      const contentType = res.headers.get('content-type');
      const writeStream = file.createWriteStream({
        metadata: {
          contentType,
          metadata: {
            myValue: 123
          }
        }
      });
      res.body.pipe(writeStream);
    });
    

提交回复
热议问题