简体   繁体   English

python上传文件到与我共享的google drive文件夹

[英]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. 我正在使用Python 2.7,并且试图将文件(* .txt)上传到与我共享的文件夹中。

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. 我将URL转到必须将此文件放置的位置。

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
    )

the API you are using is deprecated. 您使用的API已弃用。 Use google-api-python-client instead. 请改用google-api-python-client

Follow this official python quickstart guide to simply upload a file to a folder. 遵循此官方python快速入门指南 ,只需将文件上传到文件夹即可。 Additionally, send parents parameter in request body like this: body['parents'] = [{'id': parent_id}] 此外,像这样在请求正文中发送parents参数: 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. 或者,您可以使用PyDrive (Python包装器库),该库可以简化许多处理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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 通过 Python 在 Google Drive 中访问“与我共享文件夹” - Access "Shared with me folder" in Google Drive via Python 访问 Google Colab 中与我共享的云端硬盘文件夹 - access to shared with me drive folder in Google Colab 将xlsx上载到Google云端硬盘共享文件夹 - Upload xlsx into Google Drive shared folder 从与我共享的文件夹中从谷歌驱动器下载 zip 文件夹/文件 - Downloading zip folder/file from google drive from shared with me folder 如何使用 Google Drive API 和 Python 将文件上传到特定文件夹? - How to upload file into specific folder with Google Drive API and Python? 在谷歌驱动器上创建一个文件夹(如果不存在)并使用 Python 脚本将文件上传到它 - Create a folder (if not exists) on google drive and upload a file to it using Python script 使用 Python 下载共享的 Google Drive 文件夹 - Download Shared Google Drive Folder with Python 如何使用 Google 云端硬盘 API v3 向 Python 中的共享文件夹进行可续传上传? - How do I do a resumable upload to a shared folder in Python using Google Drive API v3? 无法将文件上传到 Google Drive 中的文件夹 - Unable to upload file to folder in Google Drive Python-将文件上传到Google Team Drive - Python - Upload file to Google Team Drive
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM