Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
Upload a list of files to Google Drive
import os
import sys
import pickle
from googleapiclient.discovery import build
from googleapiclient.http import MediaFileUpload
from google_auth_oauthlib.flow import InstalledAppFlow
from google.auth.transport.requests import Request
directoryIndexMap = {}
def log(msg):
def uploadFiles(destPath, files):
# If modifying these scopes, delete the file token.pickle.
SCOPES = ['']
creds = None
# The file token.pickle stores the user's access and refresh tokens, and is
# created automatically when the authorization flow completes for the first time.
if os.path.exists(abs_path('token.pickle')):
with open(abs_path('token.pickle'), 'rb') as token:
creds = pickle.load(token)
# If there are no (valid) credentials available, let the user log in.
if not creds or not creds.valid:
if creds and creds.expired and creds.refresh_token:
flow = InstalledAppFlow.from_client_secrets_file(abs_path('credentials.json'), SCOPES)
creds = flow.run_local_server(port=0)
# Save the credentials for the next run
with open(abs_path('token.pickle'), 'wb') as token:
pickle.dump(creds, token)
service = build('drive', 'v3', credentials=creds, cache_discovery=False)
for fileFullPath in files:
log('uploading: ' + fileFullPath)
fileName = fileFullPath.split("/")[-1]
folderName = destPath
folderId = ''
if folderName in directoryIndexMap:
folderId = directoryIndexMap[folderName]
folderSegments = folderName.split("/")
parentId = 'root'
for segment in folderSegments:
log("Checking path segment: " + segment)
response = service.files().list(q="mimeType='application/' and trashed=false and '" + parentId + "' in parents and name='" + segment + "'", spaces='drive', fields='files(id, name)').execute()
folderId = ''
for file in response.get('files', []):
folderId = file.get('id')
log('Found folder: %s (%s)' % (file.get('name'), folderId))
if folderId == '':
log("Folder doesn't exist, so creating with parent id " + parentId)
file = service.files().create(body={'name': segment, 'mimeType': 'application/', 'parents': [parentId]}, fields='id').execute()
folderId = file.get('id')
if folderId == '':
log("Failed to create folder, so aborting...")
parentId = folderId
directoryIndexMap[folderName] = folderId
log('Folder ID: %s' % folderId)
file_metadata = {'name': fileName, 'parents': [folderId]}
media = MediaFileUpload(fileFullPath, mimetype='application/octet-stream')
file = service.files().create(body=file_metadata, media_body=media, fields='id').execute()
log('File ID: %s' % file.get('id'))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.