是否有办法在python中使用其谷歌驱动器文件id获得谷歌驱动器文件的标题?



我有许多文件的谷歌驱动器id,我想下载。然而,下载谷歌驱动器文件的api也需要文件名,并以该名称保存文件。

是否有一种方法在python中从谷歌驱动器文件ID中获取文件的标题/名称?

如果有,请帮忙分享一个示例代码。

文件。Get方法下载文件不需要文件名,只需要发送文件id。

# Call the Drive v3 API
# get the file media data
request = service.files().get_media(fileId=FILEID)
fh = io.BytesIO()
downloader = MediaIoBaseDownload(fh, request)
done = False
while done is False:
status, done = downloader.next_chunk()
print("Download %d%%" % int(status.progress() * 100))

当你想把它保存到你的系统时,才需要一个名字。

# The file has been downloaded into RAM, now save it in a file
fh.seek(0)
with open(file_name, 'wb') as f:
shutil.copyfileobj(fh, f, length=131072)

你可以做一个文件。首先获取文件的元数据,然后当你想保存文件时可以使用它。

# Call the Drive v3 API
# Get file name, so we can save it as the same with the same name.
file = service.files().get(fileId=FILEID).execute()
file_name = file.get("name")
print(f'File name is: {file_name}')

最新更新