'How to get file location path from server but not copy it?

I have my NAS server located and i am able to get/upload files on it. Now i have suituation where i need to read .png files location from server and pass it on UI thread to show the image. Right now i am only aware of method get which needs local location to save. I don't want file to be save on my local machine but just i shall be able to show that image on my application.

I have gone through this http://docs.paramiko.org/en/2.1/api/sftp.html but didn't found relevant method to use

Code is :-

import paramiko
paramiko.util.log_to_file(r'D:\TechnoThrone\Builds\paramiko.log')
# Open a transport
host = "stedgela01.TechnoThrone.com"
port = 2222
transport = paramiko.Transport((host, port))
# Auth
password = "xxx"
username = "xxxx"
transport.connect(username = username, password = password)
# Go!
sftp = paramiko.SFTPClient.from_transport(transport)
# Download
filepath = '/A/B/C/pic_ex.png'
localpath = r'D:\picfolder\pic_ex.png'
sftp.get(filepath, localpath)


Solution 1:[1]

I don't quite get the problem, so I will try to guess a bit.
You should be able to look at content in paths in the remote server without need to download locally the file.
get is not the right method if you don't want to download because as per documentation:

get(remotepath, localpath, callback=None) Copy a remote file (remotepath) from the SFTP server to the local host as localpath. Any exception raised by operations will be passed through. This method is primarily provided as a convenience.

Parameters: remotepath (str) – the remote file to copy localpath (str) – the destination path on the local host callback (callable) – optional callback function (form: func(int, int)) that accepts the bytes transferred so far and the total bytes to be transferred

There are other methods which can get filenames in a remote directory and attributes of those without the need to download.
These are listdir, listdir_attr and listdir_iter for example.

For example, listdir_attr will

Return a list containing SFTPAttributes objects corresponding to files in the given path. The list is in arbitrary order. It does not include the special entries '.' and '..' even if they are present in the folder.

The returned SFTPAttributes objects will each have an additional field: longname, which may contain a formatted string of the file’s attributes, in unix format. The content of this string will probably depend on the SFTP server implementation.

Parameters: path (str) – path to list (defaults to '.')
Returns: list of SFTPAttributes objects

You could use something along these lines:

list_png_files = []
for file_attributes in sftp.listdir_attr("remote_path"):
    if file_attributes.filename.endswith('.png'):
        list_png_files.append(file_attributes.filename)

Check if it will give you relative or absolute path of course.
Similar you could try with listdir, etc.

Solution 2:[2]

def _list_remote_dir(self, dir="."):
    sftp_session = self.ssh_session.open_sftp() # ssh_session is paramiko.SSHClient()
    sftp_session.chdir(".")
    cwd = sftp_session.getcwd()
    print(cwd)

    remote_file_attrs = sftp_session.listdir_attr()
    for i in remote_file_attrs:
        logging.info(i.filename)

this snipet may help, you got the current working directory, and you got the file name, you can get the abs-path of files on server.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 fedepad
Solution 2 Xi Gou