Home > Software engineering >  return file id after resumable upload finished with google drive
return file id after resumable upload finished with google drive

Time:03-07

I'm integrating google drive API in my application and I'm using resumable upload approach but i can't figure out how can i get file id after upload finished in the resumable response body

here is my code to get resumable uri

const body = JSON.stringify({
        name:file.originalname,
        mimeType:file.mimetype,
        parents:[parent folder id]
    })

    const url = 'https://www.googleapis.com/upload/drive/v3/files?uploadType=resumable'
    const option = {
        method: 'post',
        body,
        headers: {
          Authorization: `Bearer ${access_token}`,
         'Content-Type': 'application/json; charset=UTF-8',
        }
    }
    const response = await fetch(url, option)
    const resumableURI = response.headers.get('location')

and this is my code to upload my file chunks

const options = {
            method: 'PUT',
            headers: {
            'Content-Length': file.size,
            'Content-Range': `bytes ${start}-${end - 1}/${length}`,
            },
            body: file.buffer, // contents of the chunk
        };
 
        const response  = await fetch(RESUBMALEURI, options)

this is the response i got from api after upload complete

response: Response {
size: 0,
timeout: 0,
[Symbol(Body internals)]: { body: [PassThrough], disturbed: false, error: null },
[Symbol(Response internals)]: {
     url: 'https://www.googleapis.com/upload/drive/v3/files? 
           uploadType=resumable&upload_id=ADPycdvajyaSOgYUjPmFgZIbi- 
          vbjM0U7xHe0yLy1eahXOC1Zq06K7ZFN2O0c_IKRdEJfa-WFc8DwE814cjV0nhCv0U',
    status: 200,
    statusText: 'OK',
    headers: [Headers],
     counter: 0
    }
}

}

is there any option i can add to request of resubmale uri or upload request itself to return the file id in the end

CodePudding user response:

When I saw your script and your additional response value, I'm worried that you might have retrieved the response value using console.log(response) from const response = await fetch(RESUBMALEURI, options). If my understanding is correct, in that case, unfortunately, such a response is obtained.

If your script for the resumable upload the file works fine and the upload is completely finished, how about the following modification?

From:

const response  = await fetch(RESUBMALEURI, options)

To:

const response  = await fetch(RESUBMALEURI, options)
const res = await response.json();
console.log(res);
// console.log(res.id); // If you want to retrieve only the file ID, you can use this.
  • By this modification, when at the last request, you can obtain the following value.

      {
        kind: 'drive#file',
        id: '###',
        name: '###',
        mimeType: '###'
      }
    

Note:

  • By the way, in your 1st request, the location is put in the variable of resumableURI. But in your script for uploading the file content, you are using the RESUBMALEURI as the endpoint. If your actual situation uses this, please modify this. Please be careful about this.

Reference:

  • Related