def odp2gslides(drive_service: Resource, slides_service: Resource): """Upload to Google drive and convert odp files to Google slides. """ tempdir = os.fsencode('temp') for file in os.listdir(tempdir): filename = os.fsdecode(file) if filename.endswith('.odp'): file_metadata = { 'name': filename.rstrip('.odp'), 'mimeType': 'application/vnd.google-apps.presentation' } media = MediaFileUpload( f'temp/{filename}', mimetype='application/vnd.oasis.opendocument.presentation', chunksize=256 * 1024, resumable=True) request = drive_service.files().create(body=file_metadata, media_body=media, fields='id') gslide = exponentialBackoff(request) # Close file after upload is done media.stream().close() # Fix text box formatting gslide_id = gslide['id'] print(f'Uploaded {filename}, id: {gslide_id}') fixformatgslide(slides_service, gslide_id)
def upload_file(self, folder_id, file_real_path): file_path, file_name = os.path.split(file_real_path) file_metadata = {'name': file_name, 'parents': [folder_id]} logger.debug('file_metadata:{}'.format(file_metadata)) mimetype = None if not os.path.splitext(file_name)[1]: # Required for files with names like '.astylerc' mimetype = "text/plain" CHUNK_SIZE = 5 * 1024 * 1024 # 5MB file_size = os.path.getsize(file_path) if file_size < CHUNK_SIZE: media = MediaFileUpload(file_real_path, mimetype=mimetype) uploaded_file = self.googledrive_api.files().create(body=file_metadata, media_body=media, fields='id').execute() logger.info('Successfully uploaded %s to googledrive:folder/%s as googledrive:file/%s', file_real_path, folder_id, uploaded_file.get('id')) else: media = MediaFileUpload(file_real_path, mimetype=mimetype, resumable=True) request = self.googledrive_api.files().create(body=file_metadata, media_body=media, fields='id').execute() media.stream() response = None while response is None: status, response = request.next_chunck() if status: logger.debug("Uploaded {}%% to google drive".format(status.progress() * 100)) logger.info('Successfully uploaded %s to googledrive:folder/%s as googledrive:file/%s', file_real_path, folder_id, response['id'])
def upload_files(files, folder_id, drive_service): upload_status = [] for file_ in files: file_metadata = { 'name': file_, 'mimeType': 'application/vnd.google-apps.spreadsheet', 'parents': [folder_id] } print('Doing for file - ', file_) media = MediaFileUpload('{}'.format(file_), mimetype='application/vnd.ms-excel', chunksize=512 * 512, resumable=True) media.stream() file = drive_service.files().create(body=file_metadata, media_body=media, fields='id') response = None while response is None: status, response = file.next_chunk() print('Response - ', response) print('Status - ', status.progress() * 100) logging.info('Uploaded {}'.format(file_)) upload_status.append('File ID: %s' % file.get('id')) return upload_status
def upload_file(service, path, remote_name): file_meta = {"name": remote_name} media = MediaFileUpload(path, resumable=True) media.stream() result = service.files().create(body=file_meta, media_body=media, fields="id").execute() return result
def test_media_file_upload_closes_fd_in___del__(self): file_desc = mock.Mock(spec=io.TextIOWrapper) opener = mock.mock_open(file_desc) if PY3: with mock.patch('builtins.open', return_value=opener): upload = MediaFileUpload(datafile('test_close'), mimetype='text/plain') else: with mock.patch('__builtin__.open', return_value=opener): upload = MediaFileUpload(datafile('test_close'), mimetype='text/plain') self.assertIs(upload.stream(), file_desc) del upload file_desc.close.assert_called_once_with()
def gdrive_upload_file(file_name, file_location, parent_id): service = get_gdrive_service() # upload file file_metadata = { 'name': file_name, 'parents': [parent_id] } print("START UPLOAD") media = MediaFileUpload(file_location, mimetype=mime.from_file(file_location), resumable=True) request = service.files().create(body=file_metadata, media_body=media, fields='id') media.stream() response = None while response is None: status, response = request.next_chunk() if status: print("Uploaded %d%%." % int(status.progress() * 100)) print("END UPLOAD")
def upload_file(self, source_path, path, server, collection, file, representation, site, overwrite=False): """ Uploads single file from 'source_path' to destination 'path'. It creates all folders on the path if are not existing. Args: source_path (string): path (string): absolute path with or without name of the file overwrite (boolean): replace existing file arguments for saving progress: server (SyncServer): server instance to call update_db on collection (str): name of collection file (dict): info about uploaded file (matches structure from db) representation (dict): complete repre containing 'file' site (str): site name Returns: (string) file_id of created/modified file , throws FileExistsError, FileNotFoundError exceptions """ if not os.path.isfile(source_path): raise FileNotFoundError( "Source file {} doesn't exist.".format(source_path)) root, ext = os.path.splitext(path) if ext: # full path target_name = os.path.basename(path) path = os.path.dirname(path) else: target_name = os.path.basename(source_path) target_file = self.file_path_exists(path + "/" + target_name) if target_file and not overwrite: raise FileExistsError("File already exists, " "use 'overwrite' argument") folder_id = self.folder_path_exists(path) if not folder_id: raise NotADirectoryError("Folder {} doesn't exists".format(path)) file_metadata = {'name': target_name} media = MediaFileUpload(source_path, mimetype='application/octet-stream', chunksize=self.CHUNK_SIZE, resumable=True) try: if not target_file: # update doesnt like parent file_metadata['parents'] = [folder_id] request = self.service.files().create(body=file_metadata, supportsAllDrives=True, media_body=media, fields='id') else: request = self.service.files().update(fileId=target_file["id"], body=file_metadata, supportsAllDrives=True, media_body=media, fields='id') media.stream() log.debug("Start Upload! {}".format(source_path)) last_tick = status = response = None status_val = 0 while response is None: if server.is_representation_paused(representation['_id'], check_parents=True, project_name=collection): raise ValueError("Paused during process, please redo.") if status: status_val = float(status.progress()) if not last_tick or \ time.time() - last_tick >= server.LOG_PROGRESS_SEC: last_tick = time.time() log.debug("Uploaded %d%%." % int(status_val * 100)) server.update_db(collection=collection, new_file_id=None, file=file, representation=representation, site=site, progress=status_val) status, response = request.next_chunk() except errors.HttpError as ex: if ex.resp['status'] == '404': return False if ex.resp['status'] == '403': # real permission issue if 'has not granted' in ex._get_reason().strip(): raise PermissionError(ex._get_reason().strip()) log.warning("Forbidden received, hit quota. " "Injecting 60s delay.") time.sleep(60) return False raise return response['id']