Skip to content

Instantly share code, notes, and snippets.

@natoinet
Last active December 14, 2024 23:43
Show Gist options
  • Select an option

  • Save natoinet/bda9bf29d87899dfca9a98303b815830 to your computer and use it in GitHub Desktop.

Select an option

Save natoinet/bda9bf29d87899dfca9a98303b815830 to your computer and use it in GitHub Desktop.

Revisions

  1. natoinet revised this gist Mar 10, 2020. 1 changed file with 6 additions and 7 deletions.
    13 changes: 6 additions & 7 deletions lambda_ftp.py
    Original file line number Diff line number Diff line change
    @@ -27,15 +27,14 @@ def handler(event, context):
    sourcekey = record['s3']['object']['key']

    #Download the file to /tmp/ folder
    download_path = '/tmp/'+ sourcekey
    filename = os.path.basename(sourcekey)
    download_path = '/tmp/'+ filename
    print(download_path)
    s3_client.download_file(sourcebucket, sourcekey, download_path)

    os.chdir("/tmp/")

    with FTP(FTP_HOST, FTP_USER, FTP_PWD) as ftp, open(sourcekey, 'rb') as file:
    ftp.storbinary(f'STOR {FTP_PATH}/{file.name}', file)
    with FTP(FTP_HOST, FTP_USER, FTP_PWD) as ftp, open(filename, 'rb') as file:
    ftp.storbinary(f'STOR {FTP_PATH}{file.name}', file)

    #We don't need the file in /tmp/ folder anymore
    os.remove(sourcekey)

    return {'statusCode': 200, 'body': json.dumps('Done')}
    os.remove(filename)
  2. natoinet created this gist Mar 8, 2020.
    41 changes: 41 additions & 0 deletions lambda_ftp.py
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,41 @@
    import os
    import json
    from ftplib import FTP
    import boto3

    # Source https://github.com/Vibish/FTP_SFTP_LAMBDA/blob/master/FTPThroughLambda.py
    # https://www.edureka.co/community/17558/python-aws-boto3-how-do-i-read-files-from-s3-bucket
    # https://medium.com/better-programming/transfer-file-from-ftp-server-to-a-s3-bucket-using-python-7f9e51f44e35
    # https://github.com/kirankumbhar/File-Transfer-FTP-to-S3-Python/blob/master/ftp_to_s3.py
    # https://dashbird.io/blog/python-aws-lambda-error-handling/

    # For example: FTP_HOST = ftp.your_ftp_host.com
    FTP_HOST = 'YOUR_FTP_HOST'
    FTP_USER = 'YOUR_FTP_USER'
    FTP_PWD = 'YOUR_FTP_PASSWORD'
    # For example: FTP_PATH = '/home/logs/'
    FTP_PATH = 'YOUR_FTP_DESTINATION_PATH'


    s3_client = boto3.client('s3')

    def handler(event, context):

    if event and event['Records']:
    for record in event['Records']:
    sourcebucket = record['s3']['bucket']['name']
    sourcekey = record['s3']['object']['key']

    #Download the file to /tmp/ folder
    download_path = '/tmp/'+ sourcekey
    s3_client.download_file(sourcebucket, sourcekey, download_path)

    os.chdir("/tmp/")

    with FTP(FTP_HOST, FTP_USER, FTP_PWD) as ftp, open(sourcekey, 'rb') as file:
    ftp.storbinary(f'STOR {FTP_PATH}/{file.name}', file)

    #We don't need the file in /tmp/ folder anymore
    os.remove(sourcekey)

    return {'statusCode': 200, 'body': json.dumps('Done')}