20s3_client = boto3.client(
"s3", aws_access_key_id=ACCESS_ID, aws_secret_access_key=ACCESS_KEY)
49 Uploads all files to s3 path defined previously
52 local_path (str): Root path where is saved files
53 files (str): File to upload
57 full_path = local_path+file
58 s3_client.upload_file(full_path, bucket, s3_path+file)
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)
70 Converts xls file (it contains fx rates for apple) to csv file
73 local_path (str): Root path where is saved files
74 files (str): File to convert
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)
84 ssc_path = file_path+
r"\gnumeric\bin\ssconvert.exe"
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"
91 subprocess.call([
"ssconvert", input_file, output_file])
96def extract_file(bucket, s3_path, local_path, file, compression, final_files):
99 Extract files from a compressed file, delete blank spaces
102 local_path (str): Root path where is saved files
103 files (str): File to decompress
104 compression (str): Type of compression (gz, zip)
107 if compression ==
".zip":
108 file_replaced = file.replace(
" ",
"_")
109 z = zipfile.ZipFile(local_path+file)
111 f.filename = f.filename.replace(
" ",
"_")
112 z.extract(f, local_path)
113 file_upload = file_replaced.replace(
".zip",
"")
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(
" ",
"_")
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)
130 Makes blank spaces deletion, extraction, conversion files and upload procedure for each file into the defined path
133 event (dict): Contains the client info to save files in s3
134 local_path (str): Root path where is saved files
137 mode = event[
"module"]
139 bucket = event[
"bucket"]
141 s3_path = event[
"path"]
142 client_id = event[
"client_id"]
143 if s3_path.endswith(
"/"):
146 s3_path = s3_path+
"/"
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))
154 files_to_upload = os.listdir(local_path)
155 files_to_upload.sort()
156 for file
in files_to_upload:
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"])
165 if ext
in extensions:
166 if "Exchange" in file_replaced:
169 if (
".csv" in file_replaced)
and (
"ZQGRO_" in file_replaced)
and (
"_Summary_Statement" in file_replaced):
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):
175 elif ext ==
".xls" or ext ==
".xlsx":
176 file_replaced_1 = file_replaced.replace(
".xlsx",
".csv")
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)
184 print(
"{} could not be uploaded".format(file_replaced))
185 print(sys.exc_info()[2])
186 print(traceback.format_exc())
188 return bucket+
"/"+s3_path, event[
"final_files"]