how to upload to google drive with python

后端 未结 2 1835
迷失自我
迷失自我 2021-01-20 17:38

I want to upload files to Google Drive with Google Script and Python.

I don\'t want to do this with API because it\'s with JSON file and requesting a password to goog

2条回答
  •  北海茫月
    2021-01-20 18:18

    You can do this by publishing a web app to run as "Me"(you) with access: "Anyone, even anonymous".

    Server side:

    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))
    }
    

    Client side:

    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()))
    

    Note:

    • Anyone who knows the web app url can upload to your drive

    References:

    • Webapp guide
    • Requests library

提交回复
热议问题