如何使用文件夹父ID直接将文件上传到指定文件夹

时间:2019-02-15 22:05:52

标签: python-2.7 python-requests google-api-client

弄清楚如何上传大于512MB的文件后,我发现了另一个问题,我不知道如何使用文件夹父ID将文件上传到指定的文件夹。

在之前的代码中,我可以将文件直接上传到文件夹中,但是现在我不能

import os
import httplib2
import zipfile
import ntpath
import oauth2client
from googleapiclient.discovery import build
from googleapiclient.http import MediaFileUpload
from oauth2client.client import OAuth2WebServerFlow

# Copy your credentials here
_CLIENT_ID = 'YOUR_CLIENT_ID'
_CLIENT_SECRET = 'YOUR_CLIENT_SECRET'
_REFRESH_TOKEN = 'YOUR_REFRESH_TOKEN'
_PARENT_FOLDER_ID = 'YOUR_PARENT_FOLDER_ID'

# ====================================================================================

# Upload file to Google Drive
def UploadFile(client_id, client_secret, refresh_token, local_file, parent_folder_id):
    cred = oauth2client.client.GoogleCredentials(None,client_id,client_secret,refresh_token,None,'https://accounts.google.com/o/oauth2/token',None)
    http = cred.authorize(httplib2.Http())
    drive_service = build('drive', 'v2', http=http)
    media_body = MediaFileUpload(local_file, mimetype='application/octet-stream', chunksize=10485760, resumable=True)
    body = {
        'title': (ntpath.basename(local_file)),
        'parents': [parent_folder_id], # <-- Here is the problem, actualy i don't know how to make it upload to specified folder directly
        'mimeType': 'application/octet-stream'
    }
    request = drive_service.files().insert(body=body, media_body=media_body)
    response = None
    while response is None:
        status, response = request.next_chunk()
        if status:
                print "Uploaded %.2f%%" % (status.progress() * 100)
    print "Upload Complete!"

# ====================================================================================


if __name__ == '__main__':
    UploadFile(_CLIENT_ID, _CLIENT_SECRET, _REFRESH_TOKEN, 'bigfile.zip', _PARENT_FOLDER_ID)

1 个答案:

答案 0 :(得分:1)

在您的脚本中,发现您正在尝试使用drive_service = build('drive', 'v2', http=http)和请求正文中的Drive API v2。我认为您可以通过两种方式进行修改。

模式1:

使用Drive API v2时,请进行以下修改。

发件人:

'parents': [parent_folder_id]

收件人:

'parents': [{'id': parent_folder_id}]}

模式2:

使用Drive API v3时,请进行以下修改。在这种情况下,您可以使用'parents': [parent_folder_id]。但是需要对v3进行修改的其他部分。

发件人:

drive_service = build('drive', 'v2', http=http)
media_body = MediaFileUpload(local_file, mimetype='application/octet-stream', chunksize=10485760, resumable=True)
body = {
    'title': (ntpath.basename(local_file)),
    'parents': [parent_folder_id],
    'mimeType': 'application/octet-stream'
}
request = drive_service.files().insert(body=body, media_body=media_body)

收件人:

drive_service = build('drive', 'v3', http=http)  # Modified
media_body = MediaFileUpload(local_file, mimetype='application/octet-stream', chunksize=10485760, resumable=True)
body = {
    'name': (ntpath.basename(local_file)),  # Modified
    'parents': [parent_folder_id],
    'mimeType': 'application/octet-stream'
}
request = drive_service.files().create(body=body, media_body=media_body)  # Modified

注意:

  • 关于your previous code,在此脚本中,使用了Drive API v3。但是,当我看到向您介绍的线程时,就使用了Drive API v2。对不起,使您感到困惑。

参考文献:

如果此修改不起作用,我深表歉意。