-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexemicroAzure.py
More file actions
123 lines (109 loc) · 4.27 KB
/
exemicroAzure.py
File metadata and controls
123 lines (109 loc) · 4.27 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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
from azure.storage.blob import BlockBlobService, PublicAccess
from sys import exit
import os, uuid, sys
import glob
import logging
import config
import getpass
import win32com.client
class AzureBlobFileUpload():
errorLogFormat = "%(asctime)s:::%(filename)s:::%(message)s"
logFormat = "%(asctime)s:::%(filename)s:::%(message)s"
logging.basicConfig(filename=config.BlobLogFileName,level=logging.INFO, format = logFormat)
logging.basicConfig(filename=config.BlobLogFileName,level=logging.error, format = errorLogFormat)
def processStart(self):
logging.info("================>ProcessStarted<================")
global nestList,localCredential
nestList = []
localCredential = R"C:\Users\\" + getpass.getuser() + "\AppData\Local\CredAzure.txt"
azureCred = self.configChange()
# azureCred = self.fileCheck()
accountName,accountKey,containerName,connectionStr = azureCred
self.azureConnection(accountName,accountKey,connectionStr,containerName)
logging.info("================>Process Ended<================")
def azureConnection(self,accountName,accountKey,connectionStr,containerName):
try:
global blob_service_client
blob_service_client = BlockBlobService(account_name=accountName, account_key=accountKey)
# blob_service_client.set_container_acl(containerName, public_access=PublicAccess.Container)
# self.fileUploadBlob(containerName) # to upload the files
# self.listBlob(containerName) #to list the blobs in the container
except Exception as e:
print(e)
def listBlob(self,containerName):
logging.info("List the blob")
try:
generator = blob_service_client.list_blobs(containerName)
for blob in generator:
print("\t Blob name: " + blob.name)
except Exception as e:
logging.error(e)
def fileUploadBlob(self,containerName):
blobFiles = []
definedFileFormat = "**/*.csv"
csvLocalFolderPath = config.csvLocalPath
blobFiles = [f for f in glob.glob(csvLocalFolderPath + definedFileFormat, recursive=False)]
logging.info("Csv files have been filtered and Selected from the local Path")
for blob in blobFiles:
try:
fullBlobPath,fileName = os.path.split(blob)
blob_service_client.create_blob_from_path(containerName, fileName, blob)
logging.info("{}=> {}".format(fileName,fullBlobPath))
except Exception as e:
logging.error(e)
logging.info("All the Files have been uploaded to Azure blob storage")
self.mailForward()
def mailForward(self):
logging.info("Windows outlook mail have been connected")
mailTrig = win32com.client.Dispatch("Outlook.Application")
Msg = mailTrig.CreateItem(0)
Msg.Importance = 0
Msg.Subject = config.mailConfig['mailSubject']
Msg.HTMLBody = config.mailConfig['mailBody']
Msg.To = config.mailConfig['mailTo']
Msg.CC = config.mailConfig['mailCC']
Msg.Display() # to make the use to enter the mail subject, body and mail recipient
# Msg.Send()
logging.info("Successfuly Mail Have been sent")
def fileCheck(self):
if os.path.exists(localCredential):
print("file exists")
pass
else:
accName = input("Enter the Acc name:")
accKey = input("Enter the Acc key:")
containerName = input("Enter the container name:")
connectString = input("Enter the connectString:")
fil = open(localCredential,"w+")
fil.write(accName + "\n")
fil.write(accKey + "\n")
fil.write(containerName + "\n")
fil.write(connectString + "\n")
fil.close()
with open(localCredential,"r") as doc:
credData = [line.strip().split() for line in doc.readlines()]
azureCred = self.removeinList(credData)
return azureCred
def removeinList(self,credData):
for i in credData:
if type(i) == list:
self.removeinList(i)
else:
nestList.append(i)
return nestList
def configChange(self):
userAct = input("Do You Want to Proceed With the Same Container or Quit? Y! or N! or Q!")
if (userAct.lower() == "y"):
logging.info("Config not yet changed")
data = self.fileCheck()
elif (userAct.lower() == "n"):
logging.info("Congif Deleted and Updated with new config")
os.remove(localCredential)
data = self.fileCheck()
elif (userAct.lower() == "q"):
exit()
else:
data = self.fileCheck()
return data
obj = AzureBlobFileUpload()
obj.processStart()