添加文件夹(如果不存在),并使用Python在Google云端硬盘中添加文件

时间:2018-11-23 14:12:23

标签: python google-drive-api

我正在尝试创建一个不存在的文件夹,然后在该目录下添加一个文件。以下脚本可以正常运行,并且看起来没有问题,但是没有创建任何文件夹,也没有文件添加到gdrive。

def SyncDrive(newFile, parentFolderId, subFolder): #auth stuff here SCOPES = 'https://www.googleapis.com/auth/drive' credentials = ServiceAccountCredentials.from_json_keyfile_name('myserver.json', SCOPES) http_auth = credentials.authorize(Http()) DRIVE = discovery.build('drive', 'v3', http=http_auth)

# create subfolder if it doesn't exist subdir_metadata = { 'name': subFolder, 'mimeType': 'application/vnd.google-apps.folder', 'parents':[{'id': parentFolderId}] } result = DRIVE.files().create(body=subdir_metadata,fields='id').execute() subdir_id = result.get('id') file_metadata = { 'name': newFile, 'parents': [{'id': subdir_id}] } result = DRIVE.files().create(body=file_metadata, media_body=file_path, fields='id').execute()

0 个答案:

没有答案