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

问题描述 投票:0回答:1

在我弄清楚如何上传大于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)
python-2.7 python-requests google-api-client
1个回答
1
投票

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

模式1:

使用Drive API v2时,请修改如下。

From:

'parents': [parent_folder_id]

To:

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

模式2:

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

From:

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)

To:

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

Note:

  • 关于your previous code,在此脚本中,使用了Drive API v3。但是,当我看到我介绍给您的主题时,使用了Drive API v2。我很抱歉让你感到困惑。

References:

如果这个修改不起作用,我道歉。

© www.soinside.com 2019 - 2024. All rights reserved.