Created
January 8, 2020 21:26
-
-
Save dbrant/cb154961c0366cea2d2f9241c905058a to your computer and use it in GitHub Desktop.
Upload a list of files to Google Drive
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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): | |
logging.debug(msg) | |
def uploadFiles(destPath, files): | |
# If modifying these scopes, delete the file token.pickle. | |
SCOPES = ['https://www.googleapis.com/auth/drive'] | |
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: | |
creds.refresh(Request()) | |
else: | |
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] | |
else: | |
folderSegments = folderName.split("/") | |
parentId = 'root' | |
for segment in folderSegments: | |
log("Checking path segment: " + segment) | |
response = service.files().list(q="mimeType='application/vnd.google-apps.folder' 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/vnd.google-apps.folder', 'parents': [parentId]}, fields='id').execute() | |
folderId = file.get('id') | |
if folderId == '': | |
log("Failed to create folder, so aborting...") | |
return | |
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