def folder_create(auth, name, parent=None): body = { 'name': name, 'parents': [parent] if parent else [], 'mimeType': 'application/vnd.google-apps.folder' } return API_Drive(auth).files().create(body=body, fields='id').execute()
def file_delete(config, auth, name, parent=None): drive_id = file_id(config, auth, name) if drive_id: API_Drive(config, auth).files().delete(fileId=drive_id).execute() return True else: return False
def file_exists(config, auth, name): drive_id = file_id(config, auth, name) if drive_id: try: API_Drive(config, auth).files().get(fileId=drive_id).execute() return True except HttpError: return False return False
def file_find(auth, name, parent=None): query = "trashed = false and name = '%s'" % name if parent: query = "%s and '%s' in parents" % (query, parent) try: return next(API_Drive(auth, iterate=True).files().list(q=query).execute()) except StopIteration: return None
def file_copy(auth, source_name, destination_name): destination_id = file_id(auth, destination_name) if destination_id: if project.verbose: print('Drive: File exists.') return file_get(auth, destination_id) else: source_id = file_id(auth, source_name) if source_id: body = {'visibility': 'PRIVATE', 'name': destination_name} return API_Drive(auth).files().copy(fileId=source_id, body=body).execute() else: return None
def file_get(config, auth, drive_id): return API_Drive(config, auth).files().get(fileId=drive_id).execute()
def about(config, auth, fields='importFormats'): response = API_Drive(config, auth).about().get(fields=fields).execute() return response
def file_create(config, auth, name, filename, data, parent=None): """ Checks if file with name already exists ( outside of trash ) and if not, uploads the file. Determines filetype based on filename extension and attempts to map to Google native such as Docs, Sheets, Slides, etc... For example: - ```file_create('user', 'Sample Document', 'sample.txt', BytesIO('File contents'))``` - Creates a Google Document object in the user's drive. - ```file_Create('user', 'Sample Sheet', 'sample.csv', BytesIO('col1,col2\nrow1a,row1b\n'))```` - Creates a Google Sheet object in the user's drive. See: https://developers.google.com/drive/api/v3/manage-uploads ### Args: - * auth: (string) specify 'service' or 'user' to toggle between credentials used to access - * name: (string) name of file to create, used as key to check if file exists - * filename: ( string) specified as "file.extension" only to automate detection of mime type. - * data: (BytesIO) any file like object that can be read from - * parent: (string) the Google Drive to upload the file to ### Returns: - * JSON specification of file created or existing. """ # attempt to find the file by name ( not in trash ) drive_file = file_find(config, auth, name, parent) # if file exists, return it, prevents obliterating user changes if drive_file: if config.verbose: print('Drive: File exists.') # if file does not exist, create it else: if config.verbose: print('Drive: Creating file.') # file mime is used for uplaod / fallback # drive mime attempts to map to a native Google format file_mime = mimetypes.guess_type(filename, strict=False)[0] drive_mime = about(config, auth, 'importFormats')['importFormats'].get( file_mime, file_mime)[0] if config.verbose: print('Drive Mimes:', file_mime, drive_mime) # construct upload object, and stream upload in chunks body = { 'name': name, 'parents': [parent] if parent else [], 'mimeType': drive_mime, } media = MediaIoBaseUpload( BytesIO(data or ' '), # if data is empty BAD REQUEST error occurs mimetype=file_mime, chunksize=CHUNKSIZE, resumable=True) drive_file = API_Drive(config, auth).files().create(body=body, media_body=media, fields='id').execute() return drive_file