I'm trying to read a file from an SMB server using .net7 api and SMBLibrary, but the read fail with null data and the status :"STATUS_END_OF_FILE". I want to get the stream data to upload it to an s3 aws bucket.
My code is as following(not completed/optimized yet..):
client = new SMB2Client();
bool isConnected = client.Connect("xxxxxxxxxxx", SMBTransportType.DirectTCPTransport);
if (isConnected)
{
NTStatus status = client.Login(string. Empty, "xxxxxxxxxx", "xxxxxxxx");
if (status == NTStatus.STATUS_SUCCESS)
{
//List<string> shares = client.ListShares(out status);
var folderPath = "folder1\\folder2\\folder3";
var shareName = "share";
ISMBFileStore fileStore = client.TreeConnect(shareName: shareName, out status);
if (status == NTStatus.STATUS_SUCCESS)
{
object directoryHandle;
FileStatus fileStatus;
status = fileStore.CreateFile(out directoryHandle, out fileStatus, "", AccessMask.GENERIC_READ, 0, ShareAccess.Read | ShareAccess.Write | ShareAccess.Delete, CreateDisposition.FILE_OPEN, CreateOptions.FILE_DIRECTORY_FILE, null);
status = fileStore.CreateFile(out directoryHandle, out fileStatus, folderPath, AccessMask.GENERIC_READ, 0, ShareAccess.Read, CreateDisposition.FILE_OPEN, CreateOptions.FILE_DIRECTORY_FILE, null);
status = fileStore.CreateFile(out directoryHandle, out fileStatus, "file1.pdf", AccessMask.GENERIC_READ | AccessMask.SYNCHRONIZE, FileAttributes.Normal, ShareAccess.Read, CreateDisposition.FILE_OPEN, CreateOptions.FILE_NON_DIRECTORY_FILE | CreateOptions.FILE_SYNCHRONOUS_IO_ALERT, null);
if (status == NTStatus.STATUS_SUCCESS)
{
MemoryStream stream = new MemoryStream();
byte[] data;
long bytesRead = 0;
while (true)
{
status = fileStore.ReadFile(out data, directoryHandle, bytesRead, (int) client.MaxReadSize);
//returned status : STATUS_END_OF_FILE
SMB Serverand why are you usingSMB2Client? All Windows versions since 2012 use SMB v3. As long as your application runs on Windows or a correctly configured Linux box, you can use File methods to read remote files, the same way you would with local files. There's no need for usernames and passwords that could leak because access uses the current process's account.