Search code examples
pythonaws-lambdaparamikofile-transferftplib

Store retrbinary output to fileobject - Python


Is there a way I can store the output of retrbinary function from ftplib into a file object in python? The idea is to retrieve files from ftp server and upload them to s3 bucket using lambda function. Want to know if this can be done using ftplib alone without using paramiko library.

import ftplib
server = ftplib.FTP()
server.connect(xxxx)
server.login()
with open('test.xlsx',wb) as fp:
     server.retrbinary('RETR test.xlsx',fp.write)
server.quit()

The above code download the test.xlsx file to local but instead I need to store it into a fileobject.


Solution

  • You can use a in-memory bytes buffer!. And you can pass buffer.write function as the callback to retrbinary method.

    >>> from io import BytesIO
    >>> buffer = BytesIO()
    >>> buffer.write(b"Hello")
    5
    >>> buffer.getvalue()
    b'Hello'
    

    So you could change your existing code to something like this,

    >>> import ftplib
    >>> from io import BytesIO
    >>>
    >>> buffer = BytesIO()
    >>> server = ftplib.FTP()
    >>> server.connect(xxxx)
    >>> server.login()
    >>> server.retrbinary('RETR test.xlsx', buffer.write)
    >>> server.quit()
    

    So after the retrbinary call is completed, the buffer gets populated with bytes values, which you can confirm by calling buffer.getvalue() method.