Office365-REST-Python-Client icon indicating copy to clipboard operation
Office365-REST-Python-Client copied to clipboard

Getting 404/500 while uploading files to sharepoint using "ensure_folder_path" api

Open SUBHASHRIQAZ opened this issue 3 years ago • 4 comments

Using below logic for uploading large files. target_url = "Shared Documents/testing" target_folder = ctx.web.ensure_folder_path(target_url) size_chunk = 1000000 local_path = "../../../tests/data/big_buck_bunny.mp4"

file_size = os.path.getsize(local_path)

def print_upload_progress(offset): print("Uploaded '{0}' bytes from '{1}'...[{2}%]".format(offset, file_size, round(offset / file_size * 100, 2)))

uploaded_file = target_folder.files.create_upload_session(local_path, size_chunk, print_upload_progress).execute_query() print('File {0} has been uploaded successfully'.format(uploaded_file.serverRelativeUrl))

Error: -2130575338, Microsoft.SharePoint.SPException', 'The file does not exist.', "404 Client Error: Not Found for url: -2146232832, Microsoft.SharePoint.Utilities.SPBITSSessionNotFoundException', 'Error in the application.', "500 Server Error:

Someone knows how I could manage this case?

SUBHASHRIQAZ avatar Dec 28 '21 09:12 SUBHASHRIQAZ

Hi, I'm getting the same error (getting with get_folder_by_server_relative_path or get_folder_by_server_relative_url), this code:

target_folder = ctx.web.get_folder_by_server_relative_url(
    "{}".format(remotepath)
).get().execute_query()
if target_folder.properties['Exists'] is True:
    file = target_folder.upload_file(filename, file_content)
    ctx.execute_query()

ends with:

raise HTTPError(http_error_msg, response=self)
requests.exceptions.HTTPError: 404 Client Error: Not Found for url: https://tenant.sharepoint.com/sites/mysite/Shared Documents/_api/Web/getFolderById('UUID')/Files/add(overwrite=true,url='SAMPLE')

I'm confirmed that I have permission to upload files from the Web, also confirmed that the folder exists (if target_folder.properties['Exists'] is True:) and also I can read files on that folder.

if I change from get_folder_by_server_relative_url to "ensure_folder_path" like this:

target_folder = ctx.web.ensure_folder_path(
    "/{}".format(remotepath)
).get().execute_query()

Error is completely different, I'm getting an "Access Denied":

raise HTTPError(http_error_msg, response=self) requests.exceptions.HTTPError: 403 Client Error: Forbidden for url: https://tenant.sharepoint.com/sites/mysite/_api/Web/RootFolder/Folders

phenobarbital avatar Feb 11 '22 17:02 phenobarbital

I am still getting below error after upgrading the library to 2.3.11.

{"message": "('-2130575338, Microsoft.SharePoint.SPException', 'The file does not exist.', "404 Client Error: Not Found for url: /sites/mysite/_api/Web/GetFileByServerRelativeUrl

SUBHASHRIQAZ avatar Feb 22 '22 15:02 SUBHASHRIQAZ

For access denied error using ensure_folder_path(), make sure the path you're passing it only starts from the library root and not from the /sites root. i.e. use: destination = 'Shared Documents/testfolder' ctx.web.ensure_folder_path(destination).execute_query()

and don't use: destination = 'sites/mysite/Shared Documents/testfolder' (or tenant.sharepoint.com/sites/mysite/Shared Documents/testfolder) ctx.web.ensure_folder_path(destination).execute_query()

rickymedrano avatar Aug 08 '22 15:08 rickymedrano

I get 403 Access Denied Client Errors using the relative server path, but the folder does in fact get created. This seems like a bug in the error handling, but not the actual creation of the folders. I am using app authentication with certificates and have granted write permission to the site collections.

Here is a my output:

# note here I am using my own wrapper library, so the 2 ctx are from that. Most normal opps would just have ctx.web
folder = ctx.ctx.web.ensure_folder_path('Shared Documents/folder1/folder2').execute_query()
Traceback (most recent call last):
  File "...\office365\runtime\client_request.py", line 58, in execute_query
    response.raise_for_status()
  File "...\requests\models.py", line 953, in raise_for_status
    raise HTTPError(http_error_msg, response=self)
requests.exceptions.HTTPError: 403 Client Error: Forbidden for url: {sharepoint_site_url}/_api/Web/RootFolder/Folders/Add('Shared%20Documents')
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
  File "...\IPython\core\interactiveshell.py", line 3444, in run_code
    exec(code_obj, self.user_global_ns, self.user_ns)
  File "<ipython-input-2-f171bd5959a6>", line 1, in <module>
    folder = ctx.ctx.web.ensure_folder_path('/Shared Documents/folder1/folder2').execute_query()
  File "...\office365\runtime\client_object.py", line 44, in execute_query
    self.context.execute_query()
  File "...\office365\runtime\client_runtime_context.py", line 161, in execute_query
    self.pending_request().execute_query(qry)
  File "...\office365\runtime\client_request.py", line 62, in execute_query
    raise ClientRequestException(*e.args, response=e.response)
office365.runtime.client_request_exception.ClientRequestException: ('-2147024891, System.UnauthorizedAccessException', 'Access denied.', "403 Client Error: Forbidden for url: {sharepoint_site_url}/_api/Web/RootFolder/Folders/Add('Shared%20Documents')")

# my own method to check if folder exists (just a wrapper around other office365 function calls)
ctx.folder_exists('folder1/folder2')
Out[3]: True

cbryant42 avatar May 05 '23 13:05 cbryant42