Skip to content

Instantly share code, notes, and snippets.

@aliostad
Last active August 22, 2016 11:09
Show Gist options
  • Save aliostad/448eec367b521885092bf37b8a6e1c25 to your computer and use it in GitHub Desktop.
Save aliostad/448eec367b521885092bf37b8a6e1c25 to your computer and use it in GitHub Desktop.
A python script to download Akamai log files from FTP drop folder to an Azure blob storage
from ftplib import FTP
import sys
import os
from azure.storage.blob import BlobService
import time
_tmp_list = []
def _accept_file(x):
_tmp_list.append(x.split(' ')[-1])
def list_files(ftp, folder):
ftp.cwd(folder)
ftp.retrlines('LIST', _accept_file)
def download_file(ftp, filename):
ftp.retrbinary('RETR ' + filename, open(filename, 'wb').write)
def build_iis_filename(filename):
return filename
if __name__ == '__main__':
if len(sys.argv) != 9:
print "Usage: \npython ftpakamai.py <host> <username> <password> <folder> <account name> <key> <container> <path>"
else:
arg = sys.argv
bs = BlobService(account_name=arg[5], account_key=arg[6])
ftp = FTP(arg[1])
ftp.login(user=arg[2], passwd=arg[3])
list_files(ftp, arg[4])
for file in _tmp_list:
i = 0
exists = False
while i<3 and (not exists):
try:
containerName = arg[7]
newFileName = file.replace('.gz', '')
destinationFileName = build_iis_filename(newFileName)
fullFilName = arg[8] + newFileName
try:
props = bs.get_blob_properties(containerName, fullFilName)
print 'File exist: {}'.format(newFileName)
break
except Exception as e:
pass
download_file(ftp, file)
print 'Downloaded ' + file
os.system('gunzip -f ' + file )
print 'Unzipped ' + file
bs.put_block_blob_from_path(containerName, fullFilName, newFileName)
print 'Uploaded ' + newFileName
os.system('rm ' + newFileName)
break
except Exception as e:
print e
i += 1
time.sleep(5)
if exists:
break
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment