Transfer file from SFTP to S3 using Paramiko

Viewed 3067

I am using Paramiko to access a remote SFTP folder, and I'm trying to write code that transfers files from a path in SFTP (with a simple logic using the file metadata to check it's last modified date) to AWS S3 bucket. I have set the connection to S3 using Boto3, but I still can't seem to write a working code that transfers the files without downloading them to a local directory first. Here is some code I tried using Paramiko's getfo() method. But it doesn't work.

for f in files:
    # get last modified from file metadata
    last_modified = sftp.stat(remote_path + f).st_mtime
    last_modified_date = datetime.fromtimestamp(last_modified).date()
    if last_modified_date > date_limit:  # check limit
       print('getting ' + f)
       full_path = f"{folder_path}{f}"
       fo = sftp.getfo(remote_path + f,f)
       s3_conn.put_object(Body=fo,Bucket=s3_bucket, Key=full_path)

Thank you!

1 Answers
Related