如何使用文件夹父id直接上传文件到指定文件夹
How to make file upload to specified folder directly using folder parent id
在弄清楚如何上传大于 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)
在您的脚本中,我们发现您正在尝试使用来自 drive_service = build('drive', 'v2', http=http)
的 Drive API v2 和请求正文。我认为有2种模式供您修改。
模式 1:
当您使用Drive API v2时,请修改如下。
发件人:
'parents': [parent_folder_id]
收件人:
'parents': [{'id': parent_folder_id}]}
模式二:
使用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,在此脚本中,使用了驱动器API v3。但是看到我给大家介绍的跟帖,用的是Drive API v2。很抱歉让你感到困惑。
参考文献:
如果此修改无效,我深表歉意。
在弄清楚如何上传大于 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)
在您的脚本中,我们发现您正在尝试使用来自 drive_service = build('drive', 'v2', http=http)
的 Drive API v2 和请求正文。我认为有2种模式供您修改。
模式 1:
当您使用Drive API v2时,请修改如下。
发件人:
'parents': [parent_folder_id]
收件人:
'parents': [{'id': parent_folder_id}]}
模式二:
使用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,在此脚本中,使用了驱动器API v3。但是看到我给大家介绍的跟帖,用的是Drive API v2。很抱歉让你感到困惑。
参考文献:
如果此修改无效,我深表歉意。