After I figured out how to upload a file bigger than 512MB, I found another one problem, I don't know how to make my file upload to a specified folder using folder parent id.
In my previous code I could upload the file directly to the folder, but now I can't
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)
In your script, it is found that you are trying to use Drive API v2 from drive_service = build('drive', 'v2', http=http)
and the request body. I think that there are 2 patterns for your modification.
When you use Drive API v2, please modify as follows.
'parents': [parent_folder_id]
'parents': [{'id': parent_folder_id}]}
When you use Drive API v3, please modify as follows. In this case, you can use 'parents': [parent_folder_id]
. But other parts are required to modify for 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