def get_gauth(self): gauth = None packge_path, _ = os.path.split(__file__) client_config = os.path.join(packge_path, 'client_secrets.json') credentials_file = os.path.join(packge_path, 'drive_credentials') if os.path.exists(credentials_file): try: gauth = GoogleAuth() gauth.LoadClientConfigFile(client_config_file=client_config) gauth.LoadCredentialsFile(credentials_file=credentials_file) return gauth except Exception as e: t.log_message(str(e)) gauth = None if self.config.gdrive_folders is not None and self.config.telegram_channels is not None and self.telegram_bot is not None: try: gauth = self.start_auth_telegram(client_config=client_config) gauth.SaveCredentialsFile(credentials_file=credentials_file) except Exception as e: t.log_message(str(e)) gauth = None elif self.config.gdrive_folders is not None and self.telegram_bot is None or self.config.telegram_channels is None: try: gauth = GoogleAuth() gauth.LoadClientConfigFile(client_config_file=client_config) gauth.CommandLineAuth() gauth.SaveCredentialsFile(credentials_file=credentials_file) except Exception as e: t.log_message(str(e)) gauth = None return gauth
def upload_and_replace_file(Directory_file, ID_file_for_replace_content_file, initDict): """ :param Directory_file: Directory file local to replace content a file in google drive :param ID_file_for_replace_content_file: ID a file in google drive :return: """ gauth = GoogleAuth() # load cline credentials with path dir, you must change the name of client_secrets.json gauth.LoadClientConfigFile(initDict['pathClientSecret']) # Try to load saved client credentials gauth.LoadCredentialsFile(initDict['pathTokenDrive']) if gauth.credentials is None: # Authenticate if they're not there gauth.LocalWebserverAuth() elif gauth.access_token_expired: # Refresh them if expired gauth.Refresh() else: # Initialize the saved creds gauth.Authorize() # Save the current credentials to a file gauth.SaveCredentialsFile(initDict['pathTokenDrive']) drive = GoogleDrive(gauth) # read file from google drive with ID name read_data = drive.CreateFile({'id': ID_file_for_replace_content_file}) # function for replace content a file in google drive with content file local read_data.SetContentFile(Directory_file) read_data.Upload({'convert': True})
def main(): args = parse_args() gauth = GoogleAuth() gauth.LoadClientConfigFile(os.environ["DRIVE_SECRET_PATH"]) drive = GoogleDrive(gauth) if args.upload_file: file = drive.CreateFile({ "title": args.upload_name, 'parents': [{ 'id': args.parent_id }] }) file.SetContentFile(os.path.join(args.file_path, args.file_name)) file.Upload() print("You have successfully uploaded the file") if args.upload_folder: create_folder(drive, args.folder_name, args.parent_id, args.folder_path) if args.download_file: download_file(drive, args.file_id, args.download_name, args.file_path)
def uploadfile(path='../itemlist', filename='20180223.csv'): dir = os.path.dirname(os.path.abspath(__file__)).split('userutil')[0] gauth = GoogleAuth() gauth.LoadClientConfigFile(dir + GAUTHFILE) drive = GoogleDrive(gauth) file_list = drive.ListFile({ 'q': "'%s' in parents and trashed=false" % FID }).GetList() for file1 in file_list: # print('title: %s, id: %s' % (file1['title'], file1['id'])) if (file1['title'] == filename): print("file exits") return metadata = { 'title': filename, 'parents': [{ "kind": "drive#stockitemlist", "id": FID }] } f = drive.CreateFile(metadata) f.SetContentFile(path + '/' + filename) f.Upload() print('File Uploaded: %s, mimeType: %s' % (f['title'], f['mimeType']))
def start_auth_telegram(self, client_config): if self.telegram_bot is None: t.log_message('telegram bot is None. Telegram auth canceled.') return auth = GoogleAuth() auth.LoadClientConfigFile(client_config_file=client_config) if auth.flow is None: auth.GetFlow() auth.flow.redirect_uri = OOB_CALLBACK_URN self.telegram_bot.send_message( 'Please go to the following link in your browser and send me a Google verification code. \nAuth url: ' + auth.GetAuthUrl()) dirty = False code = None save_credentials = auth.settings.get('save_credentials') if auth.credentials is None and save_credentials: auth.LoadCredentials() if auth.credentials is None: code = self.telegram_bot.get_code() dirty = True else: if auth.access_token_expired: if auth.credentials.refresh_token is not None: auth.Refresh() else: code = self.telegram_bot.get_code() dirty = True if code is not None: auth.Auth(code) if dirty and save_credentials: auth.SaveCredentials() return auth
def getGDrive(clientSecretPath): tmpCredsPath = os.getenv("TMP_SECRET_PATH") if( tmpCredsPath is None ): tmpCredsPath = "gdrive_secrets.bin" gauth = GoogleAuth() gauth.LoadClientConfigFile(clientSecretPath) #try to load saved client credetials gauth.LoadCredentialsFile(tmpCredsPath) if gauth.credentials is None: gauth.GetFlow() gauth.flow.params.update({'access_type': 'offline'}) gauth.flow.params.update({'approval_prompt': 'force'}) gauth.LocalWebserverAuth() elif gauth.access_token_expired: gauth.Refresh() else: gauth.Authorize() drive = GoogleDrive(gauth) gauth.SaveCredentialsFile(tmpCredsPath) return drive
def run(args): gauth = GoogleAuth() gauth.LoadClientConfigFile(args.client_secret) gauth.LocalWebserverAuth() credentials_file = os.path.join(args.dest, 'gdrive-credentials.json') gauth.SaveCredentialsFile(credentials_file) print('Credentials file saved to {}'.format(credentials_file))
def __init__(self): # OAuth認証を行う gauth = GoogleAuth( settings_file=os.path.join(SETTINGS_PATH, "setting.yaml")) gauth.LoadClientConfigFile( os.path.join(SETTINGS_PATH, "client_secrets.json")) drive = GoogleDrive(gauth) # OAuth認証が成功したインスタンスを用いる。 self.drive = drive
def upload_file(): google_auth = GoogleAuth() google_auth.LoadClientConfigFile(client_config_file=client_secrets_file) google_auth.LoadCredentialsFile(credentials_file=credentials_file) drive = GoogleDrive(google_auth) file_list = drive.ListFile({"q": "'root' in parents"}).GetList() for file1 in file_list: print("title: {}, id: {}".format(file1["title"], file1["id"]))
def authorize_google_drive(): global GAUTH global DRIVE if GAUTH is None or DRIVE is None: GAUTH = GoogleAuth() # Creates local webserver and auto handles authentication. GAUTH.LoadClientConfigFile(client_config_file=GRDIVE_CONFIG_PATH) GAUTH.LocalWebserverAuth() DRIVE = GoogleDrive(GAUTH) return True else: return True
def sendToGoogleDrive(fileName,filePath,pathToClientSecrets="/home/burrussmp/Documents/DeepNNCar/credentials.json"): gauth = GoogleAuth() gauth.LoadClientConfigFile(pathToClientSecrets) # <----- drive = GoogleDrive(gauth) folder_id = '1OqZVOYfXlEPRKfL1Q_mPWAmEUBlzlt7C' file1 = drive.CreateFile({ 'title': 'anomaly.csv', "mimeType": "text/csv", 'parents': [{'id': folder_id}] }) # Create GoogleDriveFile instance with title 'Hello.txt'. file1.SetContentFile(filePath) file1.Upload() print("Dataset uploaded") print(filePath)
def uploadToGdrive(fileName, mimeType): gauth = GoogleAuth() # 必須使用 desktop 版本的 client_secrets.json gauth.LoadClientConfigFile("./../gdrive_desktop_client_secrets.json") drive = GoogleDrive(gauth) ''' # View all folders and file in your Google Drive fileList = drive.ListFile({'q': "'root' in parents and trashed=false"}).GetList() for file in fileList: print('Title: %s, ID: %s' % (file['title'], file['id'])) # Get the folder ID that you want # 檔案會上傳到根目錄下的 uploaded 目錄中 if(file['title'] == "uploaded"): fileID = file['id'] ''' # GDrive 上 uploaded 目錄的 fileID with open("./../gdrive_uploaded_id.txt", 'r') as content_file: fileID = content_file.read() # 由上述目錄外的檔案讀取 uploaded 目錄對應 ID #fileID = "your_folder_file_ID" # 上傳檔案名稱為輸入變數 #fileName = "DemoFile.pdf" filePath = _curdir + "/downloads/" # parents 為所在 folder, 亦即 uploaded 目錄, fileID 為 uploaded 目錄的 ID file1 = drive.CreateFile({ "mimeType": mimeType, "parents": [{ "kind": "drive#fileLink", "id": fileID }], "title": fileName }) file1.SetContentFile(filePath + fileName) file1.Upload() # Upload the file. # 傳回與上傳檔案對應的 GDrive ID, 將會存入資料庫 gdiveID 欄位 return file1['id'] #print('Created file %s with mimeType %s' % (file1['title'], file1['mimeType'])) #print("upload fileID:" + str(file1['id'])) # 以下為下載檔案測試 # file2 = drive.CreateFile({'id': file1['id']}) #file2.GetContentFile('./test/downloaded_ModernC.pdf') # Download file as 'downloaded_ModernC.pdf under directory test'. '''
def __init__(self, *args, **kwargs): super(GDriveWriter, self).__init__(*args, **kwargs) from pydrive.auth import GoogleAuth from pydrive.drive import GoogleDrive gauth = GoogleAuth() files_tmp_path = tempfile.mkdtemp() client_secret_file = os.path.join(files_tmp_path, 'secret.json') with open(client_secret_file, 'w') as f: f.write(json.dumps(self.read_option('client_secret'))) gauth.LoadClientConfigFile(client_secret_file) credentials_file = os.path.join(files_tmp_path, 'credentials.json') with open(credentials_file, 'w') as f: f.write(json.dumps(self.read_option('credentials'))) gauth.LoadCredentialsFile(credentials_file) shutil.rmtree(files_tmp_path) self.drive = GoogleDrive(gauth) self.set_metadata('files_counter', Counter()) self.set_metadata('files_written', [])
def read_data_google_drive(File_ID_GoogleDrive, Title_File, dict): """ function for read data from google drive,base on name ID file. :param File_ID_GoogleDrive: code/name id file from google drive :param Title_File: path + Title of file from google drive :return: """ gauth = GoogleAuth() # load cline credentials with path dir, you must change the name of client_secrets.json gauth.LoadClientConfigFile(dict['pathClientSecret']) # Try to load saved client credentials gauth.LoadCredentialsFile(dict['pathTokenDrive']) if gauth.credentials is None: # Authenticate if they're not there gauth.LocalWebserverAuth() elif gauth.access_token_expired: # Refresh them if expired gauth.Refresh() else: # Initialize the saved creds gauth.Authorize() # Save the current credentials to a file gauth.SaveCredentialsFile(dict['pathTokenDrive']) drive = GoogleDrive(gauth) # read file from google drive with ID name File = drive.CreateFile({'id': File_ID_GoogleDrive}) # find out title of the file, and the mimetype print('title: %s, mimeType: %s' % (File['title'], File['mimeType'])) # you can change the mimetype according the output of the mimetype file if '.xlsx' in Title_File: File.GetContentFile( Title_File, mimetype= 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet' ) elif '.csv' in Title_File: File.GetContentFile(Title_File, mimetype='text/csv') else: File.GetContentFile( Title_File, mimetype= 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet' )
def connect_to_google_drive() -> GoogleDrive: gauth = GoogleAuth() gauth.LoadClientConfigFile(client_secrets_path()) # Try to load saved client credentials credentials_file = credentials_path() gauth.LoadCredentialsFile(credentials_file) if gauth.credentials is None: # Authenticate if they're not there gauth.LocalWebserverAuth() elif gauth.access_token_expired: # Refresh them if expired gauth.Refresh() else: # Initialize the saved creds gauth.Authorize() # Save the current credentials to a file gauth.SaveCredentialsFile(credentials_file) return GoogleDrive(gauth)
def upload_reports(drive_cleint_config, token_path, folder_id, report_paths): g_auth = GoogleAuth() g_auth.LoadClientConfigFile(drive_cleint_config) g_auth.LoadCredentialsFile(token_path) drive = GoogleDrive(g_auth) for report_path in report_paths: with open(report_path, "r") as file: title = os.path.basename(file.name) file_drive = drive.CreateFile({ "title": title, "parents": [{ "kind": "drive#fileLink", "id": folder_id }] }) file_drive.SetContentString(file.read()) file_drive.Upload() print("Upload file: %s" % (title))
def actualizar_cloud(file_name_cloud,file_name_local): g_login = GoogleAuth() # Actualmente funciona con mi local g_login.LoadCredentialsFile('cred/cred_dominum.txt') g_login.LoadClientConfigFile('cred/client_secrets.json') drive = GoogleDrive(g_login) # R1 Folder no se edita dado que es la carpeta fija para este proyecto folder = drive.ListFile({'q': "title = 'Job_Benchmarking_Files' and trashed=false"}).GetList()[0] # Buscamos el Folder con el nombre 'title' en GDrive # BUSCANDO ARCHIVO | POSTULANTES_COMPLETE_DASHBOARD_HR # R2 file_to_update = drive.ListFile({'q': f"title = '{file_name_cloud}' and trashed=false"}).GetList()[0] # Buscamos el Folder con el nombre 'title' en GDrive #len(drive.ListFile({'q': f"title = '{file_name_cloud}' and trashed=false"}).GetList()) #file_csv = drive.CreateFile({'title':'postulantes_complete.csv', 'mimeType':'text/csv', 'parents':[{'id':folder['id']}]}) # Seteamos a que folder irá # R3 print(file_to_update['webContentLink']) file_csv = drive.CreateFile({'id':file_to_update['id'], 'mimeType':'text/csv', 'parents':[{'id':folder['id']}]}) # Seteamos a que folder irá # R4 file_csv.SetContentFile(str(file_name_local)) # ¿Que archivo local subiremos? # R5 file_csv.Upload() #return print('El link de descarga del archivo actualizado es:'+file_csv.metadata['webContentLink']) return print('archivo '+str(file_name_local)+' fue subido a la nube')
def __init__(self, client_config_file: str = None): gauth = GoogleAuth() gauth.LoadClientConfigFile(client_config_file) self._ignorepatt = "^$" super().__init__(gauth) ## super sets self.auth = gauth
from pydrive.auth import GoogleAuth from pydrive.drive import GoogleDrive gauth = GoogleAuth() gauth.LoadClientConfigFile("./../../../../eisunwater_credentials.json") #gauth.LocalWebserverAuth() # client_secrets.json need to be in the same directory as the script drive = GoogleDrive(gauth) ''' # View all folders and file in your Google Drive fileList = drive.ListFile({'q': "'root' in parents and trashed=false"}).GetList() for file in fileList: print('Title: %s, ID: %s' % (file['title'], file['id'])) # Get the folder ID that you want # 檔案會上傳到根目錄下的 uploaded 目錄中 if(file['title'] == "uploaded"): fileID = file['id'] ''' # GDrive 上 uploaded 目錄的 fileID with open("./../../../../gdrive_uploaded_id.txt", 'r') as content_file: fileID = content_file.read() #fileID = "your_folder_file_ID" fileName = "sat_file_format.pdf" filePath = "./../" file1 = drive.CreateFile({ "mimeType": "application/pdf", "parents": [{ "kind": "drive#fileLink", "id": fileID }],
def update_gauth(): """Update gauth, save credentials to yml""" gauth = GoogleAuth() gauth.LoadClientConfigFile('../client_secrets.json') gauth.LocalWebserverAuth() gauth.SaveCredentialsFile("../gauth.yml")
def __init__(self, parsed_url): duplicity.backend.Backend.__init__(self, parsed_url) try: global pydrive import httplib2 from apiclient.discovery import build from pydrive.auth import GoogleAuth from pydrive.drive import GoogleDrive from pydrive.files import ApiRequestError, FileNotUploadedError except ImportError as e: raise BackendException(u"""\ PyDrive backend requires PyDrive installation. Please read the manpage for setup details. Exception: %s""" % str(e)) # let user get by with old client while he can try: from oauth2client.client import SignedJwtAssertionCredentials self.oldClient = True except: from oauth2client.service_account import ServiceAccountCredentials from oauth2client import crypt self.oldClient = False if u'GOOGLE_DRIVE_ACCOUNT_KEY' in os.environ: account_key = os.environ[u'GOOGLE_DRIVE_ACCOUNT_KEY'] if self.oldClient: credentials = SignedJwtAssertionCredentials( parsed_url.username + u'@' + parsed_url.hostname, account_key, scopes=u'https://www.googleapis.com/auth/drive') else: signer = crypt.Signer.from_string(account_key) credentials = ServiceAccountCredentials( parsed_url.username + u'@' + parsed_url.hostname, signer, scopes=u'https://www.googleapis.com/auth/drive') credentials.authorize(httplib2.Http()) gauth = GoogleAuth() gauth.credentials = credentials elif u'GOOGLE_DRIVE_SETTINGS' in os.environ: gauth = GoogleAuth( settings_file=os.environ[u'GOOGLE_DRIVE_SETTINGS']) gauth.CommandLineAuth() elif (u'GOOGLE_SECRETS_FILE' in os.environ and u'GOOGLE_CREDENTIALS_FILE' in os.environ): gauth = GoogleAuth() gauth.LoadClientConfigFile(os.environ[u'GOOGLE_SECRETS_FILE']) gauth.LoadCredentialsFile(os.environ[u'GOOGLE_CREDENTIALS_FILE']) if gauth.credentials is None: gauth.CommandLineAuth() elif gauth.access_token_expired: gauth.Refresh() else: gauth.Authorize() gauth.SaveCredentialsFile(os.environ[u'GOOGLE_CREDENTIALS_FILE']) else: raise BackendException( u'GOOGLE_DRIVE_ACCOUNT_KEY or GOOGLE_DRIVE_SETTINGS environment ' u'variable not set. Please read the manpage to fix.') self.drive = GoogleDrive(gauth) # Dirty way to find root folder id file_list = self.drive.ListFile({ u'q': u"'Root' in parents and trashed=false" }).GetList() if file_list: parent_folder_id = file_list[0][u'parents'][0][u'id'] else: file_in_root = self.drive.CreateFile({u'title': u'i_am_in_root'}) file_in_root.Upload() parent_folder_id = file_in_root[u'parents'][0][u'id'] file_in_root.Delete() # Fetch destination folder entry and create hierarchy if required. folder_names = parsed_url.path.split(u'/') for folder_name in folder_names: if not folder_name: continue file_list = self.drive.ListFile({ u'q': u"'" + parent_folder_id + u"' in parents and trashed=false" }).GetList() folder = next( (item for item in file_list if item[u'title'] == folder_name and item[u'mimeType'] == u'application/vnd.google-apps.folder'), None) if folder is None: folder = self.drive.CreateFile({ u'title': folder_name, u'mimeType': u"application/vnd.google-apps.folder", u'parents': [{ u'id': parent_folder_id }] }) folder.Upload() parent_folder_id = folder[u'id'] self.folder = parent_folder_id self.id_cache = {}
def auth(self) -> None: gauth = GoogleAuth(settings_file=config.DRIVE_SETTINGS_PATH) gauth.LoadCredentialsFile(config.DRIVE_CREDENTIALS_PATH) gauth.LoadClientConfigFile(config.DRIVE_SECRET_PATH) self.drive = GoogleDrive(gauth)
from pydrive.auth import GoogleAuth from pydrive.drive import GoogleDrive import os g_login = GoogleAuth() g_login.LoadClientConfigFile("client_secrets.json") #g_login.LocalWebserverAuth() g_login.CommandLineAuth() #g_login.Authorize() drive = GoogleDrive(g_login) for i in os.listdir(os.getcwd() + "/downloads"): try: print(os.getcwd() + "/downloads/" + i) if os.path.isfile(os.getcwd() + "/downloads/" + i): print("file") upload = drive.CreateFile({'title': i}) upload.SetContentFile(os.getcwd() + "/downloads/" + i) upload.Upload() print('uploade d') except Exception as e: print(e)
def __init__(self, parsed_url): duplicity.backend.Backend.__init__(self, parsed_url) try: global pydrive import httplib2 from apiclient.discovery import build from oauth2client.client import SignedJwtAssertionCredentials from pydrive.auth import GoogleAuth from pydrive.drive import GoogleDrive except ImportError: raise BackendException( 'PyDrive backend requires PyDrive installation' 'Please read the manpage to fix.') if 'GOOGLE_AUTH_MODE' not in os.environ: raise BackendException( 'GOOGLE_AUTH_MODE environment variable not set. Please read the manpage to fix.' ) auth_mode = os.environ['GOOGLE_AUTH_MODE'] if auth_mode != 'managed' and auth_mode != 'personal': raise BackendException( 'GOOGLE_AUTH_MODE environment variable not set to either "managed" or "personal". Please read the manpage to fix.' ) if auth_mode == 'managed': if 'GOOGLE_DRIVE_ACCOUNT_KEY' not in os.environ: raise BackendException( 'GOOGLE_DRIVE_ACCOUNT_KEY environment variable not set. Please read the manpage to fix.' ) account_key = os.environ['GOOGLE_DRIVE_ACCOUNT_KEY'] credentials = SignedJwtAssertionCredentials( parsed_url.username + '@' + parsed_url.hostname, account_key, scope='https://www.googleapis.com/auth/drive') credentials.authorize(httplib2.Http()) gauth = GoogleAuth() gauth.credentials = credentials else: if 'GOOGLE_SECRETS_FILE' not in os.environ: raise BackendException( 'GOOGLE_SECRETS_FILE environment variable not set. Please read the manpage to fix.' ) secrets_file = os.environ['GOOGLE_SECRETS_FILE'] if 'GOOGLE_CREDENTIALS_FILE' not in os.environ: raise BackendException( 'GOOGLE_CREDENTIALS_FILE environment variable not set. Please read the manpage to fix.' ) credentials_file = os.environ['GOOGLE_CREDENTIALS_FILE'] gauth = GoogleAuth() gauth.LoadClientConfigFile(secrets_file) gauth.LoadCredentialsFile(credentials_file) if gauth.credentials is None: gauth.CommandLineAuth() if gauth.access_token_expired: gauth.Refresh() else: gauth.Authorize() gauth.SaveCredentialsFile(credentials_file) self.drive = GoogleDrive(gauth) # Dirty way to find root folder id file_list = self.drive.ListFile({'q': "'Root' in parents"}).GetList() if file_list: parent_folder_id = file_list[0]['parents'][0]['id'] else: file_in_root = self.drive.CreateFile({'title': 'i_am_in_root'}) file_in_root.Upload() parent_folder_id = file_in_root['parents'][0]['id'] # Fetch destination folder entry and create hierarchy if required. folder_names = string.split(parsed_url.path, '/') for folder_name in folder_names: if not folder_name: continue file_list = self.drive.ListFile({ 'q': "'" + parent_folder_id + "' in parents" }).GetList() folder = next( (item for item in file_list if item['title'] == folder_name and item['mimeType'] == 'application/vnd.google-apps.folder'), None) if folder is None: folder = self.drive.CreateFile({ 'title': folder_name, 'mimeType': "application/vnd.google-apps.folder", 'parents': [{ 'id': parent_folder_id }] }) folder.Upload() parent_folder_id = folder['id'] self.folder = parent_folder_id
class GDriveStorage(AbstractStorage): def __init__(self, params): """ Creates an instance of the Google Drive storage. If ``client_secrets`` is not null, will be used that path to look for the secrets. If ``auth_tokens.json`` is not null, will be used that path to store the authentication tokens. If the authentication tokens doesn't exist, the utility will stop and it will show (in the terminal) an URL. That will allow you to log in into an account through a browser. """ client_secrets = params.get('clientSecrets', 'config/client_secrets.json') auth_tokens = params.get('authTokens', 'config/auth_tokens.json') self.__log = logging.getLogger(__name__) self.__gauth = GoogleAuth() self.__gauth.LoadClientConfigFile(client_secrets) if not os.path.exists(auth_tokens): self.__gauth.CommandLineAuth() self.__gauth.SaveCredentialsFile(auth_tokens) else: self.__gauth.LoadCredentialsFile(auth_tokens) self._drive = GoogleDrive(self.__gauth) self.__id_cache = {'/': 'root'} def _traverse(self, path: Path, parent_id: str = None, part: int = 0): """ Traverses the path to look for a file or folder in Google Drive. If exists will return the GoogleDriveFile, if not, returns None. """ len_parts = len(path.parts) if len_parts == part: return self._drive.CreateFile({'id': parent_id}) folder_id = path.parts[part] if folder_id == '/': if len_parts > 1: part += 1 folder_id = path.parts[part] else: return self._drive.CreateFile({'id': 'root'}) if parent_id is None: parent_id = 'root' sub_path = '/' + '/'.join(path.parts[1:(part + 1)]) if sub_path in self.__id_cache: return self._traverse(path, self.__id_cache[sub_path], part + 1) file_list = self._drive.ListFile({ 'q': f"'{parent_id}' in parents and trashed=false" }).GetList() for file1 in file_list: self.__id_cache[sub_path] = file1['id'] if file1['title'] == folder_id: return self._traverse(path, file1['id'], part + 1) def get(self, path): """ Gets the GoogleDriveFile instance for the path specified as argument. The path must be a string or Path. """ if isinstance(path, str): path = Path(path) elif isinstance(path, GoogleDriveFile): return path elif not isinstance(path, Path): raise TypeError('Expected Path or str, received ' + str(type(path))) return self._traverse(path) def list_directory(self, path: Union[str, Path]) -> List[str]: """ Returns a list of of GoogleDriveFile for the folder in that path. The path must exist and must be a directory. """ if isinstance(path, Path) or isinstance(path, str): drive_file = self.get(path) if drive_file is None: raise FileNotFoundError(str(path)) elif isinstance(path, GoogleDriveFile): drive_file = path else: raise TypeError( 'Expected Path, str or GoogleDriveFile, received ' + str(type(path))) if drive_file.metadata is None or drive_file.metadata == {}: drive_file.FetchMetadata() if drive_file.metadata[ 'mimeType'] != 'application/vnd.google-apps.folder': raise NotADirectoryError(drive_file.metadata['mimeType']) files = self._drive.ListFile({ 'q': f"'{drive_file.metadata['id']}' in parents and trashed=false" }).GetList() return [str(Path(path, f.metadata['title'])) for f in files] def create_folder(self, name: str, parent: Union[Path, str] = None) -> str: """ Creates a folder with name ``name`` in the root folder or in the folder specified in ``parent``. Returns the GoogleDriveFile instance for the folder. """ parent_drive = self.get(parent if parent is not None else '/') if parent_drive is None: raise FileNotFoundError(parent) dir1 = self._drive.CreateFile({ 'title': name, 'parents': [{ 'id': parent_drive['id'] }], 'mimeType': 'application/vnd.google-apps.folder', }) self.__log.info( f'Creating folder {name} [parent "{parent_drive.metadata["title"]}" {parent_drive["id"]}]' ) dir1.Upload() return str(Path(parent, name)) def upload_file(self, file_path: Path, parent: GoogleDriveFile = None): """ Uploads a file to google drive, in the root folder or in the folder specified in ``parent``. """ if parent is None: parent = self.get('/') elif not isinstance(parent, GoogleDriveFile): parent = self.get(parent) if parent is None: raise FileNotFoundError(parent) if isinstance(file_path, str): file_path = Path(file_path) elif not isinstance(file_path, Path): raise TypeError( 'Expected file path to be a str or Path, received ' + str(type(file_path))) mime_type = magic.from_file(str(file_path), mime=True) if mime_type == 'inode/x-empty': mime_type = 'application/octet-stream' if file_path.stat().st_size == 0: return file1 = self._drive.CreateFile({ 'title': file_path.parts[-1], 'mimeType': mime_type, 'parents': [{ 'id': parent['id'] }], }) self.__log.info( f'Uploading file {file_path} with attributes {file1.items()} ' f'[parent "{parent.metadata["title"]}" {parent["id"]}]') file1.SetContentFile(file_path) file1.Upload() def upload(self, path, parent=None): """ Uploads something to Google Drive. """ if parent is None: parent = self.get('/') elif not isinstance(parent, GoogleDriveFile): parent = self.get(parent) if isinstance(path, str): path = Path(path) elif not isinstance(path, Path): raise TypeError( 'Expected file path to be a str or Path, received ' + str(type(path))) if path.is_dir(): new_parent = self.create_folder(path.parts[-1], parent) for child_file in path.iterdir(): self.upload(child_file, new_parent) elif path.is_file(): self.upload_file(path, parent) def delete(self, path: Union[Path, str, GoogleDriveFile]): if isinstance(path, Path): drive_file = self.get(path) elif isinstance(path, str): drive_file = self.get(Path(path)) else: drive_file = path drive_file.Trash()