def upload_file_session(context, local_path, target_folder_url,chunk_size): upload_id = str(uuid.uuid4()) f = open(local_path, 'rb') st = os.stat(local_path) # 1. create an empty file first info = FileCreationInformation() info.content = "" info.url = os.path.basename(local_path) info.overwrite = True target_folder = context.web.get_folder_by_server_relative_url(target_folder_url) target_file = target_folder.files.add(info) context.execute_query() # 2. upload a file via session target_file_url = os.path.basename(local_path) f_pos = 0 for piece in read_in_chunks(f, size=chunk_size): if f_pos == 0: upload_result = target_folder.files.get_by_url(target_file_url).start_upload(upload_id, piece) context.execute_query() elif f_pos + len(piece) < st.st_size: upload_result = target_folder.files.get_by_url(target_file_url).continue_upload(upload_id, f_pos, piece) context.execute_query() else: upload_result = target_folder.files.get_by_url(target_file_url).finish_upload(upload_id, f_pos, piece) context.execute_query() f_pos += len(piece)
def build_query(self, files): st = os.stat(self._source_path) file_name = os.path.basename(self._source_path) # 1. create an empty target file info = FileCreationInformation() info.url = file_name info.overwrite = True self._target_file = files.add(info) # 2. upload a file in chunks f_pos = 0 fh = open(self._source_path, 'rb') for piece in read_in_chunks(fh, size=self._chunk_size): if f_pos == 0: upload_result = files.get_by_url(file_name).start_upload( self._upload_id, piece) elif f_pos + len(piece) < st.st_size: upload_result = files.get_by_url(file_name).continue_upload( self._upload_id, f_pos, piece) else: self._target_file = files.get_by_url(file_name).finish_upload( self._upload_id, f_pos, piece) f_pos += len(piece) if self._chunk_uploaded is not None: files.context.pending_request.after_execute_request( self._process_chunk_upload)
def upload_file(list_object, url, content): info = FileCreationInformation() info.content = content info.url = url info.overwrite = True uploaded_file = list_object.rootFolder.files.add(info) list_object.context.execute_query() return uploaded_file
def upload_file_into_library(target_library, name, content): context = target_library.context info = FileCreationInformation() info.content = content info.url = name info.overwrite = True target_file = target_library.root_folder.files.add(info) context.execute_query() return target_file
def test_1_upload_file(self): info = FileCreationInformation() with open(self.source_path, 'r') as content_file: info.content = content_file.read() info.url = os.path.basename(self.source_path) info.overwrite = True #upload file upload_file = self.target_library.root_folder.files.add(info) self.context.execute_query() self.assertEquals(upload_file.properties["Name"], info.url)
def upload_file(self, name, content): """Upload a file into folder :type name: str :type content: str """ info = FileCreationInformation() info.content = content info.url = name info.overwrite = True target_file = self.files.add(info) return target_file
def test_1_upload_file(self): info = FileCreationInformation() info.content = self.file_content info.url = os.path.basename(self.source_path) info.overwrite = True # upload file upload_file = self.target_library.root_folder.files.add(info) self.context.execute_query() self.assertEqual(upload_file.properties["Name"], info.url) self.assertEqual(upload_file.properties["ServerRelativeUrl"], self.server_relative_url)
def upload_file(context): path = "../../tests/data/SharePoint User Guide.docx" with open(path, 'rb') as content_file: file_content = content_file.read() list_title = "Documents" target_folder = context.web.lists.get_by_title(list_title).rootFolder info = FileCreationInformation() info.content = file_content info.url = os.path.basename(path) info.overwrite = True target_file = target_folder.files.add(info) context.execute_query() print("File url: {0}".format(target_file.properties["ServerRelativeUrl"]))
def upload_file(context, file_content, filename, subfolder): list_title = "EOC Documents" library = context.web.lists.get_by_title(list_title) filecontext = library.context info = FileCreationInformation() info.content = file_content info.url = filename info.overwrite = True ##upload file to subfolder 'eoctest' target_file = library.root_folder.folders.get_by_url(subfolder).files.add( info) filecontext.execute_query()
def upload_file_alt(self, target_folder, name, content): '''method that actually uploads the file to the sharepoint puts it in the target folder, with the name and content, content being the actual data in the file it returns the sharepoint info about the file so we can find it again''' context = target_folder.context info = FileCreationInformation() info.content = content info.url = name info.overwrite = True target_file = target_folder.files.add(info) context.execute_query() return target_file
def __upload_file(files: list, path: str, ctx: ClientContext, segment_by_value: str = None) -> None: for file in files: file_info = FileCreationInformation() with open(file, 'rb') as content_file: file_info.content = content_file.read() file_name = content_file.name if segment_by_value is not None: file_name = file_name.replace(".xlsx", "_" + segment_by_value + ".xlsx") file_info.url = os.path.basename(path + '\\' + file_name) file_info.overwrite = True ctx.web.get_folder_by_server_relative_url(path).files.add(file_info) ctx.execute_query() content_file.close()
def _create_empty_file(path): file_name = os.path.basename(path) info = FileCreationInformation() info.url = file_name info.overwrite = True return info
from office365.runtime.auth.authentication_context import AuthenticationContext from office365.sharepoint.client_context import ClientContext from office365.sharepoint.file_creation_information import FileCreationInformation import os tenant_url = "https://xxxxx.sharepoint.com" site_url = "https://xxxx.sharepoint.com/sites/testprivate" ctx_auth = AuthenticationContext(tenant_url) ctx_auth.acquire_token_for_user("*****@*****.**", "xxxx") ctx = ClientContext(site_url, ctx_auth) path = r"D:\dest.txt" with open(path, 'rb') as content_file: file_content = content_file.read() list_title = "Documents" target_folder = ctx.web.lists.get_by_title(list_title).rootFolder info = FileCreationInformation() info.content = file_content info.url = os.path.basename(path) info.overwrite = True target_file = target_folder.files.add(info) ctx.execute_query() print(target_file)
ctx_auth = AuthenticationContext(url=site_url) if ctx_auth.acquire_token_for_user( username=settings['user_credentials']['username'], password=settings['user_credentials']['password']): ctx = ClientContext(site_url, ctx_auth) upload_id = str(uuid.uuid4()) size_4k = 1024 * 4 local_path = "../data/SharePoint User Guide.docx" f = open(local_path, 'rb') st = os.stat(local_path) f_pos = 0 target_folder_url = "/Shared Documents" # 1. create an empty file first info = FileCreationInformation() info.content = "" info.url = os.path.basename(local_path) info.overwrite = True target_folder = ctx.web.get_folder_by_server_relative_url( target_folder_url) target_file = target_folder.files.add(info) ctx.execute_query() # 2. upload a file via session target_file_url = os.path.basename(local_path) for piece in read_in_chunks(f, size=size_4k): if f_pos == 0: upload_result = target_folder.files.get_by_url( target_file_url).start_upload(upload_id, piece) ctx.execute_query()