pyfilesystem2 icon indicating copy to clipboard operation
pyfilesystem2 copied to clipboard

FTPFS upload issues

Open adyanth opened this issue 4 years ago • 0 comments

Let me preface this by saying I don't know the server side configuration of the FTP server. It is maintained by the company I work at, and I provide working and non working samples below.

When using the ftplib directly:

from ftplib import FTP_TLS

FTP_TLS.port = 990     # Server requires FTPES, does not matter to the code sample
ftps = FTP_TLS('server.company.com')
ftps.login(username,password)
ftps.prot_p()    # Secure TLS

# Non working scenario
ftps.storbinary('STOR /folder1/folder2/file.txt', open(filename, 'rb'))

# Working scenario
ftps.cwd('/folder1/folder2')
ftps.storbinary('STOR file.txt', open(filename, 'rb'))

ftps.close()

In the non working scenario, I get a ftp 550 permission denied error. I am pretty sure this is due to the parent folders not having write access for some reason

So, to make the FTPFS work in this case, I modified the FTPFS.upload() method to cwd to the folder first and then do the storbinary operation:

from fs.ftpfs import FTPFS, ftp_errors


def upload(self, path, file, chunk_size=None, **options):
    _path = self.validatepath(path)
    with self._lock:
        with self._manage_ftp() as ftp:
            with ftp_errors(self, path):
                file_name = path.split('/')[-1]
                path = '/'.join(path.split('/')[:-1])
                ftp.cwd(path)
                ftp.storbinary(
                    str("STOR ") + file_name, file
                )


FTPFS.upload = upload

PS: Ignore my bad attempt at getting the filename split from the path.

So, I suggest to always cwd to the directory before calling storbinary unless there is a known issue/behaviour that requires directly storing the file?

(Only reason I can think of for directly storing is if the folder has d-w- permissions, or maybe cwd works without listing files, not sure)

adyanth avatar Apr 26 '20 12:04 adyanth