3

I have an image URLs. But these URL can be removed soon. So I should store them into my server.

I choose Firebase Storage as the image server. And I have a Node.js on AppEngine.

Actually, I am a newbie of Node.js, Web Dev. (I am an Android developer.)

I found the image download code.

var fs = require('fs'),
request = require('request');

var download = function(uri, filename, callback){
  request.head(uri, function(err, res, body){
    console.log('content-type:', res.headers['content-type']);
    console.log('content-length:', res.headers['content-length']);

    request(uri).pipe(fs.createWriteStream(filename)).on('close', callback);
  });
};

download('https://www.google.com/images/srpr/logo3w.png', 'google.png', function(){
  console.log('done');
});

After that, I can upload this image into the Firebase Storage.

But in this case I think it stores the image data into AppEngine too, right? I don't want to it. I want to store it only into the Firebase Storage.

I can remove the file on AppEngine. But is there any more clear way?

I think the best case (what I want) is

  1. download the image from URL into the Byte Array on memory

  2. ANd upload it to Firebase Storage.

How can I do this?

yoonhok
  • 2,575
  • 2
  • 30
  • 58
  • Since you know that you want to "download the image from URL into the Byte Array on memory", I think you're looking for this: https://stackoverflow.com/a/18265122 – Frank van Puffelen Oct 05 '18 at 14:05
  • Possible duplicate of [how to load an image from url into buffer in nodejs](https://stackoverflow.com/questions/18264346/how-to-load-an-image-from-url-into-buffer-in-nodejs) – Vega Apr 19 '19 at 04:16

1 Answers1

1

The proper solution given at stackoverflow.com/a/18265122 by @puf (so you can prevent to download the picture to app engine and instead work with the buffer to upload the picture to Firebase Storage), I'd only add any of the examples you can look at the Firebase official documentation to upload the picture to Firebase storage in the callback body.

var request = require('request').defaults({ encoding: null });

request.get(yourUrl, function (err, res, buffer) {
    //you can call a function (promise maybe) that will upload your picture to firebase storage
});
chinoche
  • 335
  • 1
  • 8