def createDriveFolder(name, parent_id=None): # This if statement allows creation of folder in main drive if # parent_id is not specified, because the API requires parent_id # to be sent in a list if parent_id != None: parent_id = [parent_id] file_metadata = {'name': name, 'mimeType': 'application/vnd.google-apps.folder', 'parents': parent_id} DRIVE.files().create(body=file_metadata, fields='id').execute()
def uploadFile(filename, filepath, mType, parent_id): fileMetadata = {'name': filename, 'parents':[parent_id]} media = MediaFileUpload(filepath, mimetype=mType) file = DRIVE.files().create(body=fileMetadata, media_body=media, fields='id').execute()
def searchChildrenByParent(parent_id): results = DRIVE.files().list( q="'{}' in parents and trashed=false and not fullText contains '.DS_Store'".format(parent_id), pageSize=1000, fields='nextPageToken, files(id, name, modifiedTime, mimeType, parents)').execute() items = results.get('files', []) return items
def getFileID(wd, fileName): items = DRIVE.files().list( q="name = '{}' and '{}' in parents and trashed=false".format( fileName, wd.getDirIDs()[-1]), pageSize=1000, fields='files(id)').execute().get('files', []) return items[0]['id']
def listFolders(parent_id): items = DRIVE.files().list( q="'{}' in parents and trashed=false and mimeType = 'application/vnd.google-apps.folder' and not fullText contains '.DS_Store'" .format(parent_id), pageSize=1000, fields='files(name, mimeType)').execute().get('files', []) names = [i['name'] for i in items] return sorted(names)
def downloadFile(file_id, filepath): request = DRIVE.files().get_media(fileId=file_id) fh = io.BytesIO() downloader = MediaIoBaseDownload(fh, request) done = False while done is False: status, done = downloader.next_chunk() print("Download %d%%." % int(status.progress() * 100)) with io.open(filepath, 'wb') as f: fh.seek(0) f.write(fh.read())
def listFiles(parent_id): results = DRIVE.files().list( q="'{}' in parents and trashed=false and not fullText contains '.DS_Store'" .format(parent_id), pageSize=1000, fields='files(name)').execute() items = results.get('files', []) names = [i['name'] for i in items] return sorted(names)
def getFileName(file_id): metadata = DRIVE.files().get(fileId=file_id, fields='name').execute() return metadata['name']