I am the lead developer for the Recovery Data Partnership. Since 2020 we have ingested data from various third party vendors and APIs and published them internally to city agencies using Sharepoint. I have been using the Office365 Python Rest Client library(see https://pypi.org/project/Office365-REST-Python-Client/) as the last step in the Python pipeline: publishing to Sharepoint so other agencies can authenticate with SSO and access the files anywhere inside the city network. This is a great, secure way to share files with analysts and data scientists. Uploading a file is very simple using office365-rest-python-client:
|
1 2 3 4 5 6 7 8 |
from office365.sharepoint.client_context import ClientContext from office365.runtime.auth.client_credential import ClientCredential ctx = ClientContext(URL).with_credentials(ClientCredential(Client_ID), ClientSecret)) target_folder = ctx.web.get_folder_by_server_relative_url(sharepoint_folder) with open(blob_path, 'rb') as blob: target_folder.upload_file(filename, blob) ctx.execute_query() |
Unfortunately I encountered an error when uploading large files.
Rest API post file failure. 'The request message is too big. The server does not allow messages larger than 262144000 bytes.'
I spoke with Microsoft and they said that although Sharepoint supports files up to 2 GB, you may not upload files greater than 261.144 MB. Instead, you must use the chunked upload API.
So implemented a function to recursively upload chunks using the Office365-rest-python-client to form the basis for the connection. I hope to have this implemented in the Office365-rest-python-client library as a standard feature.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 |
def sharepoint_upload_chunked(blob_path: Path, filename: str, sharepoint_folder: str, chunk_size: int): ''' input: blob_path : path to binary file to upload filename : filename you want to name the upload sharepoint_folder : the name of the folder you want to upload to chunk_size : size of the chunks in bytes. Used in recursion. ''' #log in ctx = ClientContext(URL).with_credentials( ClientCredential(CLIENT_ID, CLIENT_SECRET)) with open(blob_path, 'rb') as f: first_chunk = True size_previous_chunk = 0 offset = 0 filesize = os.path.getsize(blob_path) URL = https://myorg.sharepoint.com/sites/myapp #take url after "sites". You are already logged in to myorg.sharepoint.com via ctx (context) file_url = URL[29:] + f"/{sharepoint_folder}" + filename sharepoint_folder_long = url[29:] + f"/{sharepoint_folder}" #each upload needs a guid. You will reference this guid as you upload. upload_id = uuid.uuid4() #consume the data in chunks. while chunk := f.read(chunk_size): #see GitHub for progress bar code. This is for large uploads so it really helps. progressbar(offset, filesize, 30,'■') #start upload if first_chunk: #you need to initialize an empty file to upload into. print("adding empty file") endpoint_url = f"{url}/_api/web/getfolderbyserverrelativeurl('{sharepoint_folder_long}')/files/add(url='{filename}', overwrite=true)" upload_data(ctx, endpoint_url, bytes()) endpoint_url = f"{url}/_api/web/getfilebyserverrelativeurl('{file_url}')/startupload(uploadID=guid'{upload_id}')" response = upload_data(ctx, endpoint_url, chunk) first_chunk=False #Finish upload. if the current chunk is smaller than the previous chunk, it must be the last chunk. elif len(chunk) < size_previous_chunk: endpoint_url = f"{url}/_api/web/getfilebyserverrelativeurl('{file_url}')/finishupload(uploadID=guid'{upload_id}',fileOffset={offset})" progressbar(filesize, filesize, 30,'■') response = upload_data(ctx, endpoint_url, chunk) print(response) #continue upload. else : #continue to consume the chunks and upload. endpoint_url = f"{url}/_api/web/getfilebyserverrelativeurl('{file_url}')/continueupload(uploadID=guid'{upload_id}',fileOffset={offset})" response = upload_data(ctx, endpoint_url, chunk) #length in characters, not in bytes) size_previous_chunk = len(chunk) offset = offset + size_previous_chunk |
By implementing chunked uploads, we can now upload large CSVs to SharePoint to share with agencies using Office-365-REST-Python-Client.
You may find the full code at: https://github.com/SteveScott/office-365-python-rest-client-chunked-upload-example/blob/main/sharepoint_upload.py
Leave A Reply