1

I am pulling a file from a 3rd party system and it is returning the data in the response as a string. Now when using that string to upload to drive it is uploading but the file is not opening. I tried converting the string to Blob but it is not possible in node.js. How can I upload correctly using that string of data?

let res=await axios.get("getting the file from 3rd party system",headers);
  
  let fileToUpload=res.data;   // storing the file in variable

  
 let data={
  name:'image.png',
  parents:['1BD50Xdf5456ghec8Ccwa5YNEkpgPaOxdE_M']
};
let head={
      headers: {
        'Content-Type': 'application/json; charset=UTF-8',
        'Content-Length':127494,
        'Authorization': 'Bearer '+driveaccessToken
    }
};
  //request to upload metadata
 res=await axios.post('https://www.googleapis.com/upload/drive/v3/files?uploadType=resumable',JSON.stringify(data),head);

  //getting the content upload url from response
 let uploadUrl=res.headers.location;
 
 //uploading the content
  res = await axios.put(uploadUrl,fileToUpload,head);
  console.log(res.status);
General Grievance
  • 4,555
  • 31
  • 31
  • 45
  • Is there a reason to not use the official [Node.js client](https://developers.google.com/drive/api/v3/quickstart/nodejs)? – Martí Sep 08 '21 at 12:09
  • yes I already have my accessToken ready and so It's easy for me to upload using request instead using client –  Sep 13 '21 at 06:54
  • When you say "string of data" what does this string look like? Is it an encoded image? What format is this in? base64? Is there a reason you need a resumable upload? What do you mean exactly by the file is uploading but not opening? What do you mean that converting to a blob is not possible in node.js? Have you read [this](https://developers.google.com/drive/api/v3/manage-uploads#http_1)? – iansedano Sep 13 '21 at 09:03
  • the data is utf8 encoded and I converted it to buffer of base64 encoding. Creating a blob is not possible like it is in client-side javascript. –  Sep 14 '21 at 03:55
  • Can you confirm if its an encoded image? Is there a reason you need a resumable upload? What do you mean exactly by the file is uploading but not opening? There are many packages to deal with blobs in npm. – iansedano Sep 14 '21 at 07:13
  • It can be any media file not just an image. Resumable upload because the file can be larger than 6mb. It's uploading successfully but not opening, maybe not uploading in the correct encoding that google drive requires –  Sep 15 '21 at 08:04
  • I think you need to give a [Minimal Reproducible Example](https://stackoverflow.com/help/minimal-reproducible-example). There could be many things wrong but without being able to reproduce your example there is not much we can do. Can you provide the 3rd party endpoint for instance? I would recommend using the client library, you are making your life difficult if not. See https://github.com/googleapis/google-api-nodejs-client#media-uploads, https://stackoverflow.com/questions/36280818/how-to-convert-file-to-base64-in-javascript https://nodejs.org/api/fs.html#fs_fs_createreadstream_path_options – iansedano Sep 16 '21 at 07:47

0 Answers0