Search code examples
pythonpysmb

Store file to local server with pysmb


I'm writing a script to sync a folder with a shared folder on a local synology nas. I am able to connect, read the files, and remove no problem but am stucking at copy (storefile).

The docs from pysmb are this

storeFile(service_name, path, file_obj, timeout=30)
  Store the contents of the file_obj at path on the service_name. If 
  the file already exists on the remote server, it will be truncated 
  and overwritten.

Parameters: 
  service_name (string/unicode) – the name of the shared folder for the 
 path
 path (string/unicode) – Path of the file on the remote server. If the 
 file at path does not exist, it will be created. Otherwise, it will 
 be overwritten. If the path refers to a folder or the file cannot be 
 opened for writing, an OperationFailure will be raised.
 file_obj – A file-like object that has a read method. Data will read 
 continuously from file_obj until EOF.

 Returns:   
 Number of bytes uploaded

I can't seem to pass the correct type of file obj. The main error I'm receiving is this

smb.smb_structs.OperationFailure: Failed to store  on andrews-itunes: Unable to open file

Here is my attempt

    with open(start_path + f, 'rb', buffering=0) as file_obj:
        conn.storeFile(server_path, '/', file_obj)
    file_obj.closed

I also tried using io.BYTESIO. From my conclusion is that I have to pass a byte object without opening it already because it tries to do that, but how could I get that byte file from drive? Any ideas?


Solution

  • The path parameter of storeFile must include path and name of the file that should be created/overwritten. To make the function as flexible as possible, the name is not taken from the file_obj (the file-like object may not even have a name).