微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

有没有办法在使用 gdrive API 将文件上传到谷歌驱动器时重命名文件?

如何解决有没有办法在使用 gdrive API 将文件上传到谷歌驱动器时重命名文件?

我正在自动文件上传到 google drive(使用 google drive 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

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。