2

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.

enter image description here

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?

Martin Prikryl
  • 188,800
  • 56
  • 490
  • 992
Dimas Aps
  • 71
  • 8

1 Answers1

1

You have at least two problems with your code:

  1. Your immediate problem is, that you are missing the FTP command (RETR) in the Connection.retrbinary call. That's why you get "error_perm: 500 Syntax error, command unrecognized.". It should be:

    ftp.retrbinary("RETR " + filename, myfile.write)
    
  2. 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)
    
Martin Prikryl
  • 188,800
  • 56
  • 490
  • 992