I need to send data from an FTP server into an S3 bucket without saving the file to my local drive. On the internet, I found that we can use io.BytesIO()
as buffer. But my code fails with:
error_perm: 500 Syntax error, command unrecognized.
The script:
ftp = ftplib.FTP(ipaddr)
ftp.login(usr,pswd)
ftp.cwd(folder)
myfile = io.BytesIO()
buffer = ftp.retrbinary(filename, myfile.write)
myfile.seek(0)
s3_resource.Object(bucket_name, folder "/" filename).put(Body=buffer)
ftp.quit()
Aany one can help me please?
CodePudding user response:
You have at least two problems with your code:
Your immediate problem is, that you are missing the FTP command (
RETR
) in theConnection.retrbinary
call. That's why you get "error_perm: 500 Syntax error, command unrecognized.". It should be:ftp.retrbinary("RETR " filename, myfile.write)
Once you solve that, you will see that the contents won't make it to the S3, as you are passing FTP response (
buffer
), instead of the downloaded contents (myfile
) to S3, as @dreamca4er commented. It should be:s3_resource.Object(bucket_name, folder "/" filename).put(Body=myfile)