azure_storage_helpers.py 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. import azure.cosmos.cosmos_client as cosmos_client
  2. from azure.storage.blob import BlockBlobService, PublicAccess
  3. import csv
  4. import pickle
  5. # Initializes CosmosDB Client
  6. def InitializeCosmosClient(config):
  7. client = cosmos_client.CosmosClient(url_connection=config['ENDPOINT'], auth={'masterKey': config['PRIMARYKEY']})
  8. return client
  9. # Reads all documents in the given collection
  10. def ReadDocuments(client,config):
  11. collection_link = 'dbs/'+ config['DATABASE'] + '/colls/' + config['COLLECTION']
  12. documentlist = list(client.ReadItems(collection_link))
  13. print('Found {0} documents'.format(documentlist.__len__()))
  14. documents = []
  15. for doc in documentlist:
  16. docId = doc.get('id')
  17. pk = doc.get(config['PARTITIONKEY'])
  18. documents.append(ReadDocument(docId,pk,client,collection_link))
  19. return documents
  20. # Reads the contents of a document
  21. def ReadDocument(doc_id,pk,client,collection_link):
  22. doc_link = collection_link + '/docs/' + doc_id
  23. if pk != '':
  24. response = client.ReadItem(doc_link,{'partitionKey':pk})
  25. else:
  26. response = client.ReadItem(doc_link)
  27. print('Read document {0}'.format(response.get('name')))
  28. return response
  29. # Initializes Blob Storage Service
  30. def InitializeBlobService(config):
  31. service = BlockBlobService(account_name=config['ACCOUNTNAME'], account_key=config['KEY'])
  32. return service
  33. # Uploads file to Blob Storage
  34. def uploadFile(config,service,filename,f):
  35. try:
  36. service.create_blob_from_path(config['CONTAINER'], filename, f)
  37. print("File uploaded to blob storage.")
  38. except Exception as e:
  39. print(e)
  40. # Creates CSV file
  41. def createCSV(data,filename):
  42. with open(filename, 'w', newline='', encoding='utf-8') as csvFile:
  43. writer = csv.writer(csvFile,delimiter='|')
  44. writer.writerows(data)
  45. csvFile.close()
  46. print("CSV file created.")
  47. # Gets file from Blob Storage and stores it locally
  48. def getFile(config,service,filename,path):
  49. try:
  50. service.get_blob_to_path(config['CONTAINER'], filename, path)
  51. print("File retrieved from blob storage.")
  52. except Exception as e:
  53. print(e)
  54. # Creates a pickle file, saves it locally, and uploads it to Blob Storage
  55. def uploadPickle(config,service,data,filename,path):
  56. with open(path, 'wb') as f:
  57. pickle.dump(data, f)
  58. print("Pickle file created.")
  59. uploadFile(config,service,filename,path)
  60. # Loads model from local file
  61. def getModel(path):
  62. model = pickle.load(open(path, 'rb'))
  63. print("Model loaded.")
  64. return model