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

Unable to upload to sharepoint with random name

Open Vahagn2502 opened this issue 3 years ago • 1 comments

I was trying to upload a big file to SharePoint as it is shown in the example, it worked but there was no option to give another name rather than basename.

https://github.com/vgrem/Office365-REST-Python-Client/blob/9f0dc83e61c011c7979619e6b59624512022fbd2/office365/sharepoint/files/file_collection.py

It would be a nice option to have. Something like this:

` def create_upload_session(self, source_path, chunk_size, file_name=None, chunk_uploaded=None, *chunk_func_args): """Upload a file as multiple chunks

:param str source_path: path where file to upload resides
:param int chunk_size: upload chunk size (in bytes)
:param str file_name: file name to save
:param (long)->None or None chunk_uploaded: uploaded event
:param chunk_func_args: arguments to pass to chunk_uploaded function
:return: office365.sharepoint.files.file.File
"""
file_size = os.path.getsize(source_path)
if file_size > chunk_size:
    qry = UploadSessionQuery(self, source_path, chunk_size, file_name, chunk_uploaded, chunk_func_args)
    self.context.add_query(qry)
    return qry.return_type
else:
    with open(source_path, 'rb') as content_file:
        file_content = content_file.read()
    if not file_name:
        file_name = os.path.basename(source_path)
    return self.upload(file_name, file_content)

`

Also, changes are needed here as well:

https://github.com/vgrem/Office365-REST-Python-Client/blob/9f0dc83e61c011c7979619e6b59624512022fbd2/office365/sharepoint/internal/queries/upload_session.py

Vahagn2502 avatar Feb 22 '22 10:02 Vahagn2502

I ran ran in to this issue today. Any way to specify a new file name on the destination. I suppose renaming the local file first can be done, but that feels clunky.

richardschoen avatar Jul 02 '22 21:07 richardschoen

Nowadays it is supported to specify a custom file name via FilesCollection.create_upload_session method, for example:

with open(local_path, "rb") as f:
    uploaded_file = target_folder.files.create_upload_session(
        f, size_chunk, print_upload_progress, "recording.mp4"
    ).execute_query()

vgrem avatar Mar 02 '24 19:03 vgrem