简体   繁体   English

如何使用python替换Google云端硬盘文件的内容?

[英]How do I replace the contents of a Google Drive file using python?

I have a Google Drive file that has a specific file id that others rely on so they can open it. 我有一个Google云端硬盘文件,该文件具有其他人依赖的特定文件ID,以便他们可以打开它。 I need to replace the contents of this file (a CSV file) with new content within python. 我需要用python中的新内容替换此文件(CSV文件)的内容。 I can't seem to find any examples of how to do this, though. 不过,我似乎找不到任何执行此操作的示例。 I'm using Google Drive API v3 (googleapiclient). 我正在使用Google Drive API v3(googleapiclient)。 Please help. 请帮忙。

I managed to create a successful test. 我设法创建了一个成功的测试。 I created a file (test.txt) in Google drive with some basic text in it. 我在Google驱动器中创建了一个文件(test.txt),其中包含一些基本文本。 I obtained the file id by sharing a link and extracting the id from the link and then unshared it again. 我通过共享链接并从链接中提取ID来获取文件ID,然后再次取消共享。 This python code successfully replaced the text in that file with the text in _textStream. 此python代码成功用_textStream中的文本替换了该文件中的文本。 My ultimate goal of replacing the CSV information is achieved similarly, except that the BytesIO object will be the CSV data and the _mimeType will be 'text/csv': 除了替换BytesIO对象将是CSV数据和_mimeType将是'text / csv'之外,我实现替换CSV信息的最终目标也达到了类似的目的:

from oauth2client import file, client, tools
from googleapiclient.discovery import build
from httplib2 import Http
from io import BytesIO
from apiclient.http import MediaIoBaseUpload

def gauthenticate():
    # If modifying these scopes, delete the file token.json.
    SCOPES = 'https://www.googleapis.com/auth/drive'
    store = file.Storage('token.json')
    creds = store.get()
    if not creds or creds.invalid:
        flow = client.flow_from_clientsecrets('credentials.json', SCOPES)
        creds = tools.run_flow(flow, store)
    service = build('drive', 'v3', http=creds.authorize(Http()))
    return service

def SaveTxtGDrive(service):
    # Fill in your own file id here:
    fileId = '1kEyXXXXXXXX_gEd6lQq'
    _mimeType = 'text/plain'
    _textStream = BytesIO(b'Here is some arbitrary data\nnew text\nAnother line\n') 
    _media = MediaIoBaseUpload(_textStream, mimetype=_mimeType,
        chunksize=1024*1024, resumable=True)
    _updatedFile = service.files().update(fileId=fileId,
        media_body=_media).execute()

def main():
    _service = gauthenticate()
    SaveTxtGDrive(_service)
    return

if (__name__ == '__main__'):
    main()    

The Google API documentation sure is opaque and v3 has few examples in python! Google API文档确定是不透明的,并且v3中的python示例很少! Obviously, the Drive API key must be set up and the API enabled for Google Drive for this to work. 显然,必须设置Drive API密钥并启用Google Drive的API才能使其正常工作。 I simply followed the error messages that popped up to enable all of that. 我只是遵循弹出的错误消息来启用所有功能。 I hope this helps someone else. 我希望这可以帮助其他人。

暂无
暂无

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

相关问题 如何使用 PyDrive 替换/更新 Google Drive 上的文件? - How to replace/update a file on Google Drive with PyDrive? 如果文本文件的内容不同,如何将它们替换为另一个? - How do I replace a text file's contents with another if they are different? 如何在单个请求中使用 Python 在 Google Drive 中上传文件夹(不是文件)? - How do I upload a folder (not files) in Google Drive using Python at a single request? 我正在尝试使用 python 和来自谷歌云的驱动器 API 将文件上传到谷歌驱动器。 但它的凭据会在一段时间后过期。 该怎么办? - I am trying to upload a file to google drive using python and Drive API from google Cloud. But it's credentials expires after sometime. what to do? 如何使用 Python 在 Google 驱动器上上传大文件? - How to upload a large file on Google drive using Python? **如何在 Python Google Drive API 中列出多个文件夹的内容? - **How to list the contents of Multiple folder in Python Google Drive API? 如何使用 python 在谷歌驱动器中创建目录? - How to create directories in google drive using python? 如何使用python读取Windows历史记录(文件夹)的内容? - How do I read the contents of Windows History (folder) using python? 使用 Python 和 PyDrive 在谷歌驱动器上成功上传文件,但文件已损坏 - Uploaded a File succesfully on google drive using Python with PyDrive but File is damaged 下载文件谷歌驱动器 python - Download file google drive python
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM