問題描述
我想使用 Google Script 和 Python 將文件上傳到 Google Drive.
I want to upload files to Google Drive with Google Script and Python.
我不想使用 API 執(zhí)行此操作,因為它使用 JSON 文件并請求 Google 帳戶的密碼.
I don't want to do this with API because it's with JSON file and requesting a password to google account.
我想從沒有 JSON 文件且不請求谷歌帳戶的 Python 文件發(fā)送.
I want to send from the Python file without a JSON file and without requesting a google account.
我想在谷歌腳本中創(chuàng)建一個腳本,將文件上傳到網(wǎng)站.
I want to create a script in google script that will upload the file to the site.
我找到了如何使用 html 來做到這一點(diǎn):
I found how to do it with html:
function saveFile(data,name,folderName) {
var contentType = data.substring(5,data.indexOf(';'));
var file = Utilities.newBlob(
Utilities.base64Decode(data.substr(data.indexOf('base64,')+7)),
contentType,
name
); //does the uploading of the files
DriveApp.getFolderById(childFolderIdA).createFile(file);
}
但是我沒有找到如何用 python 來做.
But I did not find how to do it with python.
如何將帶有文件的文件發(fā)送到此代碼?
How do I send file with file to this code?
推薦答案
您可以通過發(fā)布一個網(wǎng)絡(luò)應(yīng)用程序以我"(您)的身份運(yùn)行并訪問:任何人,甚至匿名".
You can do this by publishing a web app to run as "Me"(you) with access: "Anyone, even anonymous".
function doPost(e){
const FOLDER_ID = '###FOLDER_ID###';
var name = e.parameter.name;
saveFile(e.postData.contents,name,FOLDER_ID);
return 'Success';
}
function saveFile(data,name,id) {
data = Utilities.newBlob(Utilities.base64DecodeWebSafe(data));
DriveApp.getFolderById(id)
.createFile(data.setName(name))
}
客戶端:
import requests, base64
url = '###WEB_APP_PUBLISHED_URL###'
name = '###FILENAME###'
requests.post(url+'?name='+name,data=base64.urlsafe_b64encode(open('##UPLOAD FILE PATH##','rb').read()))
注意:
- 知道網(wǎng)絡(luò)應(yīng)用網(wǎng)址的任何人都可以上傳到您的驅(qū)動器
- 網(wǎng)絡(luò)應(yīng)用指南
- 請求庫
這篇關(guān)于如何在沒有身份驗證的情況下使用 python 上傳到谷歌驅(qū)動器?的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網(wǎng)!