abc.dat我使用下面的腳本來獲取通過我的 Apps 腳本專案生成的檔案 blob 。使用 Drive 服務,這很容易。使用的 oauthScope 是https://www.googleapis.com/auth/drive.readonly
function ReadData() {
var files;
var folders = DriveApp.getFoldersByName("Holder");
if (folders.hasNext()) {
var folder = folders.next();
var files = folder.getFiles();
while (files.hasNext()){
file = files.next();
if(file.getName()=='abc.dat'){
var content = file.getBlob().getDataAsString();
return content;
}
}
}
return '';
}
為了減少認證范圍,現在我正在修改代碼以完全移除oauthScopehttps://www.googleapis.com/auth/drive.readonly并僅使用https://www.googleapis.com/auth/drive.fileoauthScope。
使用 Drive api,我沒有找到獲取檔案 blob 的直接方法。我使用下面的腳本來獲取 word 檔案檔案的 blob。但它不適用于有錯誤的 .dat 檔案fileNotExportable, Export only supports Docs Editors files, code 403
function getBlob(fileID, format){
var url = "https://www.googleapis.com/drive/v3/files/" fileID "/export?mimeType=" format;
var blob = UrlFetchApp.fetch(url, {
method: "get",
headers: {"Authorization": "Bearer " ScriptApp.getOAuthToken()},
muteHttpExceptions: true
}).getBlob();
return blob;
}
找到這篇文章并嘗試更改export. 現在回傳給“未找到”。geturlblob.getDataAsString()
我在創建 abc.dat 檔案時使用的 mimeType 是application/octet-stream .dat. 但是當檢查生成的檔案時,它的 mimeType 是text/plain. 所以我使用'text/plain'作為getBlob函式中'format'引數的輸入。
.dat 檔案創建代碼:
var connectionsFile = {
title: filename,
mimetype: "application/octet-stream .dat",
parents: [{'id':folder.getId()}],
};
var blobData = Utilities.newBlob(contents);
file = Drive.Files.insert(connectionsFile,blobData);
}
如何修改此代碼以從檔案中獲取 blob?還是有其他方法?
提前致謝!
uj5u.com熱心網友回復:
我認為在您的情況下,需要使用get方法而不是export方法。因為export方法用于 Google Docs 檔案(檔案、電子表格、幻燈片等)。當你的腳本被修改時,下面的修改呢?
修改后的腳本:
function getBlob(fileID) {
var url = "https://www.googleapis.com/drive/v3/files/" fileID "?alt=media";
var blob = UrlFetchApp.fetch(url, {
method: "get",
headers: { "Authorization": "Bearer " ScriptApp.getOAuthToken() },
muteHttpExceptions: true
}).getBlob();
return blob;
}
參考:
- 下載檔案
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/442368.html
上一篇:Google表格水平單選復選框
