MediaFileUpload在我的Google驱动器上的何处创建文件?

2024-04-26 21:22:11 发布

您现在位置:Python中文网/ 问答频道 /正文

我刚开始使用googledriveapi,在理解如何使用该API时遇到了很多困难。在搜索了整个网络之后,我终于找到了一个工作示例here,并获得了以下代码:

from oauth2client.service_account import ServiceAccountCredentials
from googleapiclient.discovery import build
from googleapiclient.http import MediaFileUpload

#Set up a credentials object I think
creds = ServiceAccountCredentials.from_json_keyfile_name('client_secrets.json', ['https://www.googleapis.com/auth/drive'])

#Now build our api object, thing
drive_api = build('drive', 'v3', credentials=creds)

file_name = "test"
print("Uploading file " + file_name + "...")

#We have to make a request hash to tell the google API what we're giving it
body = {'name': file_name, 'mimeType': 'application/vnd.google-apps.document'}

#Now create the media file upload object and tell it what file to upload,
#in this case 'test.html'
media = MediaFileUpload('test.csv', mimetype = 'text/csv')

#Now we're doing the actual post, creating a new file of the uploaded type
fiahl = drive_api.files().create(body=body, media_body=media).execute()

#Because verbosity is nice
print("Created file '%s' id '%s'." % (fiahl.get('name'), fiahl.get('id')))

当我运行上述代码时,我得到以下结果:

Uploading file test...
Created file 'test' id '12kV2WUPg64_asVgVaVFF0X3lFAA_RkVJGtL-eDnexxM'.

这意味着代码执行成功,所以我希望看到测试.csv在我的谷歌硬盘上的某个地方,但事实并非如此,而且该文件在哪里都找不到。所以,有几个问题:

  1. 我应该在哪里找那个文件?你知道吗
  2. 是否在Google驱动器中指定一个目录,这样代码就可以将文件放在那个特定的目录中?你知道吗

任何帮助都是非常感谢的,因为几乎不可能在一个地方找到这个googleapi的所有工作部件。你知道吗


Tags: theto代码namefromtestimportbuild