Welcome to WuJiGu Developer Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.1k views
in Technique[技术] by (71.8m points)

how to upload to google drive with python

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 google account.

I want to send from the Python file without a JSON file and without requesting a google account.

I want to create a script in google script that will upload the file to the site.

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

But I did not find how to do it with python.

How do I send file with file to this code?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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:


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to WuJiGu Developer Q&A Community for programmer and developer-Open, Learning and Share
...