Repository navigation
Expand file tree
/
Copy pathaws_lambda_ftp_function.py
More file actions
78 lines (66 loc) · 2.43 KB
/
Copy pathaws_lambda_ftp_function.py
File metadata and controls
78 lines (66 loc) · 2.43 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
from __future__ import print_function
import boto3
import os
import ftplib
# AWS S3 Bucket name
bucket = ""
# FTP Credentials
ip = ""
username = ""
password = ""
remote_directory = ""
local_directory = ""
ftp_archive_folder = ""
# This function will check if a given name/path is a folder to avoid downloading it
def is_ftp_folder(ftp, filename):
try:
res = ftp.sendcmd('MLST ' + filename)
if 'type=dir;' in res:
return True
else:
return False
except:
return False
def lambda_handler(event, context):
# Connecting to FTP
try:
ftp = ftplib.FTP(ip)
ftp.login(username, password)
except:
print("Error connecting to FTP")
try:
ftp.cwd(remote_directory)
except:
print("Error changing to directory {}".format(remote_directory))
try:
if not is_ftp_folder(ftp, ftp_archive_folder):
print("Creating archive directory {}".format(ftp_archive_folder))
ftp.mkd(ftp_archive_folder)
except:
print("Error creting {} directory".format(ftp_archive_folder))
if not os.path.exists(local_directory):
os.makedirs(local_directory)
print("Created local directory {}".format(local_directory))
for file in files:
if not is_ftp_folder(ftp, file):
try:
if os.path.isfile(local_directory + "/" + file):
print("File {} exists locally, skip".format(file))
try:
ftp.rename(file, ftp_archive_folder + "/" + file)
except:
print("Can not move file {} to archive folder".format(file))
else:
print("Downloading {} ....".format(file))
ftp.retrbinary("RETR " + file, open(local_directory + "/" + file, 'wb').write)
try:
s3_client.meta.client.upload_file(local_directory + "/" + file, bucket, file)
print("File {} uploaded to S3".format(file))
try:
ftp.rename(file, ftp_archive_folder + "/" + file)
except:
print("Can not move file {} to archive folder".format(file))
except:
print("Error uploading file {} !".format(file))
except:
print("Error downloading file {}!".format(file))