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
Answers:
Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.
Method 1
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 theRESUBMALEURI
as the endpoint. If your actual situation uses this, please modify this. Please be careful about this.
Reference:
All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0