2

I can successfully save my uploaded image in my public uploads folder and integrate google drive api but it seems that it is uploading an empty file.

What should I put in the body parameter of the Google Drive API from my req.files data

{name: 'country.jpg',data: <Buffer ff d8 ff e0 00 10 4a 46 49 46 00 01 01 01 00 48 00 48 00 00 ff ed 26 0c 50 68 6f 74 6f 73 68 6f 70 20 33 2e 30 00 38 42 49 4d 04 04 00 00 00 00 00 22 ... 246290 more bytes>,size: 246340,encoding: '7bit',tempFilePath: '',truncated: false,mimetype: 'image/jpeg',md5: '8890c8336c58d854d490b41fa6ec0ad4',mv: [Function: mv]}

Here's my Google Drive API call after auth.

const drive = google.drive({ version: "v3", auth });
drive.files.create({
 media: {
            mimeType: "image/jpeg",
            body: // WHAT TO PUT HERE
        },
        resource: {
            name: "photo.jpg"
            // if you want to store the file in the root, remove this parents
            //parents: ['folder id in which he file needs to be stored.']
        },
        fields: "id"
    })
    .then(function(resp) {
        //console.log("RESPONSE");
        console.log(resp, "resp");
    })
    .catch(function(error) {
        console.log("ERROR");
        console.log(error);
    });
Linda Lawton - DaImTo
  • 106,405
  • 32
  • 180
  • 449

2 Answers2

2

Just like @DalmTo mentioned, you need to send your data to the Google Drive API somehow.

If you were sending a file in your filsystem you could use the code she provided:

var media = {
  mimeType: 'image/jpeg',
  body: fs.createReadStream(FILEPATH)
};

However, since you are not trying to save the file to your filesystem before uploading you have to adapt to your situation.

I'm assuming this files reaches your application thru a form upload since you are using Express. If you can user multer as well, you can get your files as a Buffer

You can then convert that Buffer to a Stream.

Your code for treating this would look like this:

Copyvar Readable = require('stream').Readable; 

function bufferToStream(buffer) { 
  var stream = new Readable();
  stream.push(buffer);
  stream.push(null);

  return stream;
}

app.post('/upload', upload.single('picture'), function (req, res, next) {
  // req.file is the `picture` file
  var media = {
    mimeType: 'image/jpeg',
    body: bufferToStream(req.file.buffer)
  };
})
ZektorH
  • 2,680
  • 1
  • 7
  • 20
0

Thats because you are only uploading the file metadata you need to actually upload the file.

var media = {
        mimeType: 'image/jpeg',
        //PATH OF THE FILE FROM YOUR COMPUTER
        body: fs.createReadStream('C:/Users/me/Downloads/photo.jpg')
};
Linda Lawton - DaImTo
  • 106,405
  • 32
  • 180
  • 449
  • I am not saving the image file in my uploads folder. I want it directly to go to Google Drive. I cannot use fs.createReadStream(FILE_PATH). How do I do that body: // WHAT TO PUT HERE? – Rio Marie A. Callos Nov 14 '19 at 09:01
  • Go directly to google drive from where then? You cant upload a file if you dont have the file stream to send to google. Get a file stream for your file and upload it. You will of course need the user to select the file they wish to upload this will give you the location of the file on their machine. – Linda Lawton - DaImTo Nov 14 '19 at 09:45
  • I'm only receiving the request in Express API app with the file content how should I receive it to get a stream? – Rio Marie A. Callos Nov 14 '19 at 09:59
  • You probably cant. Your question is "what you need to send to upload a file" The anwser is "you need to send a file stream of the file that the user wishes to upload". If this Express API doesnt send you a file stream of the file to upload then you cant upload it. Maybe ask another question about how to get a file stream from Express API? – Linda Lawton - DaImTo Nov 14 '19 at 11:54