-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmicroAzure.py
More file actions
76 lines (66 loc) · 2.84 KB
/
microAzure.py
File metadata and controls
76 lines (66 loc) · 2.84 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
from azure.storage.blob import BlockBlobService, PublicAccess
from sys import exit
import os, uuid, sys
import glob
import logging
import config
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):
accountName = config.azureBlobConnect['accountName']
accountKey = config.azureBlobConnect['accountKey']
connectionStr = config.azureBlobConnect['connectionStr']
containerName = config.azureBlobConnect['containerName']
self.azureConnection(accountName,accountKey,connectionStr,containerName)
logging.info("================>Process Ended<================")
def azureConnection(self,accountName,accountKey,connectionStr,containerName):
try:
logging.info("================>ProcessStarted<================")
print(containerName)
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.listBlob(containerName) #to list the blobs in the container
self.fileUploadBlob(containerName) # to upload the files
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 = []
print("Upload")
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):
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.Send()
logging.info("Successfuly Mail Have been sent")
obj = AzureBlobFileUpload()
obj.processStart()