Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

python upload file to google drive folder that is shared with me

I am using Python 2.7 and I am trying to upload a file (*.txt) into a folder that is shared with me.

So far I was able to upload it to my Drive, but how to set to which folder. I get the url to where I must place this file.

Thank you

this is my code so far

def Upload(file_name, file_path, upload_url):

    upload_url = upload_url
    client = gdata.docs.client.DocsClient(source=upload_url)
    client.api_version = "3"
    client.ssl = True
    client.ClientLogin(username,  passwd, client.source)

    filePath = file_path
    newResource = gdata.docs.data.Resource(filePath,file_name)

    media = gdata.data.MediaSource()
    media.SetFileHandle(filePath, 'mime/type')

    newDocument = client.CreateResource(
        newResource,
        create_uri=gdata.docs.client.RESOURCE_UPLOAD_URI,
        media=media
    )
like image 568
Yebach Avatar asked Feb 14 '23 21:02

Yebach


1 Answers

the API you are using is deprecated. Use google-api-python-client instead.

Follow this official python quickstart guide to simply upload a file to a folder. Additionally, send parents parameter in request body like this: body['parents'] = [{'id': parent_id}]

Or, you can use PyDrive, a Python wrapper library which simplifies a lot of works dealing with Google Drive API. The whole code is as simple as this:

from pydrive.auth import GoogleAuth
from pydrive.drive import GoogleDrive

gauth = GoogleAuth()
drive = GoogleDrive(gauth)

f = drive.CreateFile({'parent': parent_id})
f.SetContentFile('cat.png') # Read local file
f.Upload() # Upload it
like image 193
JunYoung Gwak Avatar answered Apr 07 '23 02:04

JunYoung Gwak