我在我的應用程式中集成了 google drive API,并且我正在使用可恢復上傳方法,但我無法弄清楚在可恢復回應正文中上傳完成后如何獲取檔案 ID
這是我獲取可恢復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')
這是我上傳檔案塊的代碼
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)
這是上傳完成后我從 api 得到的回應
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
}
}
}
有什么選項我可以添加到 resubmale uri 的請求或上傳請求本身以最終回傳檔案 ID
uj5u.com熱心網友回復:
當我看到您的腳本和您的附加回應值時,我擔心您可能已經使用console.log(response)from檢索了回應值const response = await fetch(RESUBMALEURI, options)。如果我的理解是正確的,那么在那種情況下,不幸的是,得到了這樣的回應。
如果您的可恢復上傳檔案的腳本作業正常并且上傳完全完成,那么以下修改如何?
從:
const response = await fetch(RESUBMALEURI, options)
到:
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.
通過此修改,在最后一次請求時,您可以獲得以下值。
{ kind: 'drive#file', id: '###', name: '###', mimeType: '###' }
筆記:
- 順便說一句,在您的第一個請求中,該位置被放入變數
resumableURI. 但是在您上傳檔案內容的腳本中,您使用的RESUBMALEURI是端點。如果你的實際情況使用這個,請修改這個。請注意這一點。
參考:
- 節點獲取
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/439210.html
