使用pysmb将文件存储到本地服务器 [英] Store file to local server with pysmb

查看:446
本文介绍了使用pysmb将文件存储到本地服务器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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).

来自pysmb的文档是这个

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

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

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

这是我的尝试

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

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

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?

推荐答案

storeFilepath参数必须包含应创建/覆盖的文件的路径和名称.为了使功能尽可能灵活,请不要从file_obj中获取名称(文件状对象甚至可能没有名称).

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).

这篇关于使用pysmb将文件存储到本地服务器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆