python - 有没有办法在使用 gdrive API 将文件上传到 Google Drive 时重命名文件?

标签 python google-api google-drive-api pydrive

我自动在 Google 云端硬盘上上传文件(使用 Google 云端硬盘 API)。

我想在上传时重命名文件(因为它的标签原来是文件的完整路径目录,相当长)。

我正在使用此函数将文件上传到 gdrive:

def upload_file_gdrive(file_path, base_currency, gdrive_path_id):

    # Authentication (automated using the config files)
    # The config files should be dropped in the current working directory

    gauth = GoogleAuth()
    drive = GoogleDrive(gauth)

    
    # Upload file on gdrive

    print('Uploading the market data on google drive')

    gfile = drive.CreateFile({'parents': [{'id': gdrive_path_id}]})
    gfile.SetContentFile(file_path)
    gfile.Upload()  # Upload the file.

    print('Market data upload on google drive successful')

我们有办法在上传文件时重命名文件吗?

干杯

最佳答案

创建文件实际上分两部分完成:发送描述文件的元数据,然后发送文件的实际内容。

您只需提供元数据即可更改名称。

gfile = drive.CreateFile({'parents': [{'id': gdrive_path_id}]})
gfile['title'] = 'HelloWorld.txt' # Change title of the file.
gfile.SetContentFile(file_path)
gfile.Upload()  # Upload the file.

Update file metadata

关于python - 有没有办法在使用 gdrive API 将文件上传到 Google Drive 时重命名文件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/67456572/

相关文章:

javascript - Google 日历 API - 无法列出辅助日历中的事件

ruby-on-rails - 无法从 Ruby 中的 google api 获取用户信息

c# - Google Drive Api-MVC IDataStore

android - 判断文件夹是否存在,不存在则创建

python - Django 休息框架 : Embed Viewset Inside Viewset

python - 如何在python中打开文件,阅读注释 ("#"), 找到注释后面的单词并选择它后面的单词?

python - 多个子图的自定义 xticks?

python - 将注释的 Python 脚本包含到 Sphinx 生成的文档中

.net - 用于 Web 表单的 Google.Apis.Auth.OAuth

node.js - 尝试通过 API(v3) 在 Google 云端硬盘上创建文件夹时权限不足