Glider
Loading...
Searching...
No Matches
UploadFilesS3.py
Go to the documentation of this file.
1import os, sys, traceback
2import gzip
3import shutil
4import zipfile
5import boto3
6import uuid
7import subprocess
8# from tkinter import messagebox
9from datetime import date, datetime
10from dotenv import load_dotenv
11
12file_path = os.path.dirname(__file__)
13dotenv_path = os.path.join(file_path, ".env")
14load_dotenv(dotenv_path)
15
16
17ACCESS_ID, ACCESS_KEY = os.environ.get("AWS_KEY_ID"), os.environ.get("AWS_KEY_SECRET")
18
19
20s3_client = boto3.client("s3", aws_access_key_id=ACCESS_ID, aws_secret_access_key=ACCESS_KEY)
21
22
23date_for_path = date.today()
24date_for_path = datetime.strftime(date_for_path, "%Y-%m-%d")
25# final_files = None
26mode = None
27global bucket, s3_path
28
29def folder_or_file(local_path, files_list):
30 """
31 Moves all files (in fub folders) to root path
32
33 Args:
34 local_path (str): Root path where is saved files
35 files_list (list): list where each file is allocated to be moved posteriorly
36 Returns : Nothing.
37 """
38 for path, subdirs, files in os.walk(local_path):
39 for name in files:
40 files_list.append(os.path.join(path, name))
41 for file in files_list:
42 try:
43 shutil.move(file, local_path)
44 except:
45 continue
46
47def upload_files_s3(bucket, s3_path, local_path, file, mode):
48 """
49 Uploads all files to s3 path defined previously
50
51 Args:
52 local_path (str): Root path where is saved files
53 files (str): File to upload
54 Returns : Nothing.
55 """
56 if "manual" in mode:
57 full_path = local_path+file
58 s3_client.upload_file(full_path, bucket, s3_path+file)
59 return file
60 else:
61 random_uuid = uuid.uuid4()
62 random_text = str(random_uuid)[:8]
63 file_id = random_text+"_"+file
64 full_path = local_path+file
65 s3_client.upload_file(full_path, bucket, s3_path+file_id)
66 return file_id
67
68def transform_xls_file(local_path, file):
69 """
70 Converts xls file (it contains fx rates for apple) to csv file
71
72 Args:
73 local_path (str): Root path where is saved files
74 files (str): File to convert
75 Returns : Nothing.
76 """
77 output_file = file.replace(" ", "_")
78 output_file = os.path.join(local_path, output_file)
79 ext_file = os.path.splitext(output_file)[1]
80 output_file = output_file.replace(ext_file, ".csv")
81 input_file = os.path.join(local_path, file)
82 if os.name == 'nt':
83 #ssc_path = file_path+r"\gnumeric\1.12.17\bin\ssconvert.exe" # testing
84 ssc_path = file_path+r"\gnumeric\bin\ssconvert.exe" # app
85 command = [ssc_path, input_file, output_file]
86 subprocess.run(command, shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE, stdin=subprocess.PIPE)
87 elif os.name == 'posix':
88 ssc_path = "gnumeric/bin/ssconvert" #for local testing
89 # ssc_path = os.path.join(file_path, "gnumeric/bin/ssconvert") #for app
90 # subprocess.call([ssc_path, input_file, output_file])
91 subprocess.call(["ssconvert", input_file, output_file])
92 # file_o = file.replace(" ", "_")
93 # file_o = file_o.replace(".xlsx", ".csv")
94 # subprocess.call(["ssconvert",file,local_path+file_o],cwd=local_path)
95
96def extract_file(bucket, s3_path, local_path, file, compression, final_files):
97 # global final_files
98 """
99 Extract files from a compressed file, delete blank spaces
100
101 Args:
102 local_path (str): Root path where is saved files
103 files (str): File to decompress
104 compression (str): Type of compression (gz, zip)
105 Returns : Nothing.
106 """
107 if compression == ".zip":
108 file_replaced = file.replace(" ", "_")
109 z = zipfile.ZipFile(local_path+file)
110 for f in z.filelist:
111 f.filename = f.filename.replace(" ", "_")
112 z.extract(f, local_path)
113 file_upload = file_replaced.replace(".zip", "")
114 print(file_upload)
115 file_upload_id = upload_files_s3(bucket, s3_path, local_path, file_upload, mode)
116 final_files.append(file_upload_id)
117 elif compression == ".gz":
118 file_o = file.replace(".gz", "")
119 file_o = file_o.replace(" ", "_")
120 print(file_o)
121 with gzip.open(local_path+file, 'rb') as f_in:
122 with open(local_path+file_o, 'wb') as f_out:
123 shutil.copyfileobj(f_in, f_out)
124 file_upload_id = upload_files_s3(bucket, s3_path, local_path, file_o, mode)
125 final_files.append(file_upload_id)
126
127def clean_files(event, local_path):
128 global mode
129 """
130 Makes blank spaces deletion, extraction, conversion files and upload procedure for each file into the defined path
131
132 Args:
133 event (dict): Contains the client info to save files in s3
134 local_path (str): Root path where is saved files
135 Returns : Nothing.
136 """
137 mode = event["module"]
138 # Bucket to upload files (it's fixed)
139 bucket = event["bucket"]
140 # path to upload files, each moth it's a different path
141 s3_path = event["path"]
142 client_id = event["client_id"]
143 if s3_path.endswith("/"):
144 pass
145 else:
146 s3_path = s3_path+"/"
147 # s3_path = "{}/{}/".format(client_id, date_for_path)
148 #Create path if it doesn't exist
149 response_in = s3_client.list_objects(Bucket = bucket, Prefix=s3_path)
150 if "Contents" not in response_in:
151 s3_client.put_object(Bucket=bucket, Key=(s3_path))
152 csv_files = []
153 folder_or_file(local_path, csv_files)
154 files_to_upload = os.listdir(local_path)
155 files_to_upload.sort()
156 for file in files_to_upload:
157 try:
158 file_replaced = file.replace(" ", "_")
159 os.rename(local_path+file, local_path+file_replaced)
160 extensions = [".csv", ".xls", ".xlsx", ".txt", ".tsv"]
161 ext = os.path.splitext(file)[1]
162 if ext == ".zip" or ext == ".gz":
163 extract_file(bucket, s3_path, local_path, file_replaced, ext, event["final_files"])
164 pass
165 if ext in extensions:
166 if "Exchange" in file_replaced:
167 transform_xls_file(local_path,file_replaced)
168 continue
169 if (".csv" in file_replaced) and ("ZQGRO_" in file_replaced) and ("_Summary_Statement" in file_replaced):
170 transform_xls_file(local_path, file_replaced)
171 # subprocess.call(["ssconvert",file_replaced,local_path+file_replaced],cwd=local_path)
172 elif ("Napster" in file_replaced) or ("revshare" in file_replaced) or ("Audiomack" in file_replaced) or ("WW_MN" in file_replaced) or ("KepachMusictas" in file_replaced):
173 transform_xls_file(local_path, file_replaced)
174 # subprocess.call(["ssconvert",file_replaced,local_path+file_replaced],cwd=local_path)
175 elif ext == ".xls" or ext == ".xlsx":
176 file_replaced_1 = file_replaced.replace(".xlsx", ".csv")
177 transform_xls_file(local_path, file_replaced)
178 # subprocess.call(["ssconvert",file_replaced,local_path+file_replaced_1],cwd=local_path) # path to your file must contain the xlsx and csv files
179 file_replaced = file_replaced_1
180 file_upload_id = upload_files_s3(bucket, s3_path, local_path, file_replaced, mode)
181 print(file_upload_id)
182 event["final_files"].append(file_upload_id)
183 except:
184 print("{} could not be uploaded".format(file_replaced))
185 print(sys.exc_info()[2])
186 print(traceback.format_exc())
187 continue
188 return bucket+"/"+s3_path, event["final_files"]
189
extract_file(bucket, s3_path, local_path, file, compression, final_files)
folder_or_file(local_path, files_list)
upload_files_s3(bucket, s3_path, local_path, file, mode)