概述
笔者准备在Electron框架下使用samba-client进行远程交互,结果发觉存在基本的使用问题,连基本的连接都有问题:samba-client 代码地址。
猜测可能与samba-server端使用版本有关。(难道samba-client不支持samba2?貌似没看到代码文档说明。)
smb2 (bchelli/mode-smb2)
bchelli/mode-smb2 Github地址
该库较为久远,所支持也很有限,尤其是其支持的uri地址,都比较像Windows专用,果然在测试了某些基本接口后发现如下类似问题:
smb2 read P3 err = Error: STATUS_ACCESS_DENIED (0xC0000022) : {Access Denied} A process has requested access to an object but has not been granted those access rights.
在github上找到了对应issue:https://github.com/bchelli/node-smb2/issues/17
This issue seems to be fixed here:
https://github.com/Node-SMB/marsaud-smb2 at least it worked with
“readdir” for me. For reference: Node-SMB#18 The docs show var SMB2 =
require(‘smb2’); but I had to use var SMB2 = require(‘@marsaud/smb2’);
to reference the package.
@marsaud/smb2 (Node-SMB/marsaud-smb2)
Node-SMB/marsaud-smb2 Github地址
该库从 bchelli/mode-smb2 Github地址 fork而来,作者近期还在维护,可靠度还是蛮高的。
使用如下指令安装即可:
npm install -g @marsaud/smb2@latest
简单测试了下对应的读取文件、下载文件、上传文件、创建目录等基本功能,还是比较靠谱的。唯独在测试上传文件(createWriteStream)时,总是会出现如下报错:
smb2 createWriteStream err = Error: STATUS_ACCESS_DENIED (0xC0000022): {Access Denied} A process has requested access to an object but has not been granted those access rights.
检查后发现,服务器中已经存在对应文件时,则会出现上述问题。经测试,该库基本功能完善,而且还提供了一些更灵活的功能:
smb2Client.open('path\\to\\the\\file', function(err, fd) {
if (err) throw err;
smb2Client.read(
Buffer.alloc(10), // buffer where to store the data
0, // offset in the buffer
10, // number of bytes to read
0, // offset in the file
function(err, bytesRead, buffer) {
smb2Client.close(fd, function() {});
if (err) throw cb(err);
console.log(bytesRead, buffer);
}
);
});
This API is modeled after Node's fs module.
Note: be careful of autoCloseTimeout with this process as it is not intended to cover multiple method calls, you should set it to 0 and manually disconnect().
可以根据实际需求自行扩展。