在Google Drive Python 3上上传txt文件

时间:2017-10-01 03:55:42

标签: python python-3.x google-drive-api

我在Google云端硬盘上上传txt文件时遇到了一些问题。当我运行代码时,一切顺利,但是当我转到Google云端硬盘时,没有上传任何内容。有什么想法吗?

json_key = json.load(open('jsonkey.json')
scope = ['https://www.googleapis.com/auth/drive']
credentials = ServiceAccountCredentials.from_json_keyfile_name('/home/pi/Downloads/jsonkey.json',scope)

http = httplib2.Http()
http = credentials.authorize(http)

drive_service = build('drive', 'v2', http=http)

media_body1 = MediaFileUpload('sensor1Text.txt', mimetype='text/plain', resumable=True)
body1 = {'title': 'Sensor1','description': 'A test document',
      'mimeType': 'text/plain'}

sensor1File = drive_service.files().insert(body=body1, media_body=media_body1).execute()
pprint.pprint(sensor1File)

media_body2 = MediaFileUpload('sensor2Text.txt', mimetype='text/plain', resumable=True)
body2 = {'title': 'Sensor2','description': 'A test document',
      'mimeType': 'text/plain'}

sensor2File = drive_service.files().insert(body=body2, media_body=media_body2).execute()
pprint.pprint(sensor2File)

media_bodyCV = MediaFileUpload('cumulativeViewText.txt', mimetype='text/plain', resumable=True)
bodyCV = {'title': 'CumulativeView','description': 'A test document',
      'mimeType': 'text/plain'}

CVFile = drive_service.files().insert(body=bodyCV, media_body=media_bodyCV).execute()
pprint.pprint(CVFile)

1 个答案:

答案 0 :(得分:0)

使用create代替insert [为所有人]。

示例:

CVFile = drive_service.files().create(body=bodyCV, media_body=media_bodyCV, fields='id').execute()