I am trying to upload a file from a Django server to another FTP Server using ftplib
This is what I am trying to do in views.py
@background(schedule=1)
def uploadToFTP(folder):
"""
Async function to upload the images to FTP Server.
"""
print("-------------------Start FTP ----------------------------")
#establish ftp connection
ftp = FTP(conf_settings.FTP_DOMAIN,conf_settings.FTP_USER, conf_settings.FTP_PASSWORD)
file = os.path.join(folder, filename)
ftp.storbinary('STOR ' filename, file,102400) # send the file
I am getting all sorts of errors like this:
ftp.storbinary('STOR ' filename, file,102400)
File "/opt/anaconda3/lib/python3.8/ftplib.py", line 489, in storbinary
buf = fp.read(blocksize)
AttributeError: 'str' object has no attribute 'read'
So I have tried many methods, but nothing works. Is this even posible.
I also tried like this:
with Image.open(os.path.join(folder, i)) as file:
b = io.BytesIO()
file.save(b, "JPEG")
b.seek(0)
#upload activity
ftp.storbinary('STOR ' i, file,102400)
Still not successful
CodePudding user response:
Here is a manual that you should check first
Here is a same question that you should check second
Here is an example of correct code:
from ftplib import FTP with FTP( conf_settings.FTP_DOMAIN, conf_settings.FTP_USER, conf_settings.FTP_PASSWORD ) as ftp: with open(os.path.join(folder, filename), 'rb') as file: ftp.storbinary(f'STOR {filename}', file)