Put S3 file to FTP server using storbinary without downloading to a local temporary file using Lambda or Glue

Question:

I am trying to put an S3 file to an FTP server using a Glue Python shell. Below given is the code. I am stuck after getting the file from S3. Tried few ways of storbinary but in vain. It is easy if we try from local using Python but since this is from Glue/Lambda, the file handling part is bit tricky.

import ftplib
import boto3
from io import BytesIO,StringIO
ftp = ftplib.FTP()
ftp.connect('xxx',yyy)
ftp.login('uname','passwd')
ftp.cwd('path')
s3 = boto3.client('s3')
file = s3.get_object(Bucket='my_bucket',Key='myfolder/myfile.txt')
data = file['Body'].read()
with open(data,"rb") as f:
    ftp.storbinary('STOR test12.txt',f)

After the line file = s3.get_object(Bucket='my_bucket',Key='myfolder/myfile.txt'), bit confused on the next steps. Any help on how this can be done is appreciated.

Asked By: Ludwig

||

Answers:

You will need to use something like BytesIO/StringIO file-like objects:

b = BytesIO(file['Body'].read())
ftp.storbinary('STOR test12.txt', b)

(untested, just a concept)

Answered By: Martin Prikryl