使用pysmb将文件存储到本地服务器

时间:2019-01-28 00:43:53

标签: python pysmb

我正在编写脚本以将文件夹与本地Synology nas上的共享文件夹同步。我能够连接,读取文件并清除所有问题,但卡在副本(存储文件)上。

pysmb的文档是这个

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

我似乎无法传递文件obj的正确类型。我收到的主要错误是

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

这是我的尝试

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

我也尝试使用io.BYTESIO。根据我的结论,我必须传递一个字节对象而不必先打开它,因为它试图这样做,但是如何从驱动器中获取该字节文件呢?有什么想法吗?

1 个答案:

答案 0 :(得分:1)

path的{​​{1}}参数必须包含应创建/覆盖的文件的路径和名称。为了使功能尽可能灵活,请不要从storeFile中获取名称(类似文件的对象甚至可能没有名称)。