11import csv
22import json
3- from azure .storage .blob import BlockBlobService
3+ from azure .storage .blob import BlobServiceClient
44import datetime
55import os
66import argparse
77
88#### Recommendation: Store these as environment variables or download it from Key Vault
99
10- storage_acc_name = '<storage-account-name> '
10+ storage_acc_url = 'https://<AZURE_STORAGE_ACCOUNT_NAME>.blob.core.windows.net '
1111storage_acc_key = '<storage-account-key>'
12- block_blob_service = BlockBlobService ( account_name = storage_acc_name , account_key = storage_acc_key )
12+ block_blob_service = BlobServiceClient ( account_url = storage_acc_url , credential = storage_acc_key )
1313####
1414
1515def getfilename (name ):
@@ -24,10 +24,11 @@ def processcsvfile(fname,seperator,outdir,outfname):
2424 print ("loaded file " + fname )
2525 all_vals = []
2626 for rows in allrows :
27+ line = ""
2728 if isHeaderLoaded is False :
2829 # Getting the first line as header
2930 header = rows
30- isHeaderLoaded = true
31+ isHeaderLoaded = True
3132 else :
3233 if len (header ) > 0 :
3334 i = 0
@@ -41,7 +42,6 @@ def processcsvfile(fname,seperator,outdir,outfname):
4142 i = i + 1
4243 line = line + "}"
4344 all_vals .append (json .loads (json .dumps (line )))
44- line = ""
4545 if not os .path .exists (outdir ):
4646 os .makedirs (outdir )
4747 json_fpath = outdir + "/" + outfname + '.json'
@@ -60,22 +60,32 @@ def processcsvfile(fname,seperator,outdir,outfname):
6060 container = args .container
6161 pattern = args .pattern
6262
63+ container_client = block_blob_service .get_container_client (container )
64+
6365 print ("Processing files from container : " + str (container ))
6466 if pattern is not None :
65- blob_list = block_blob_service .list_blobs (container_name = container , prefix = pattern )
67+ blob_list = container_client .list_blobs (name_starts_with = pattern )
6668 else :
67- blob_list = block_blob_service .list_blobs (container_name = container )
69+ blob_list = container_client .list_blobs ()
6870
6971 for blob in blob_list :
7072 print ("Processing blob : " + blob .name )
71- blob_name = getfilename (blob .name )
73+ blob_name = getfilename (blob .name ). split ( '.' )[ 0 ]
7274 downloadedblob = "downloaded_" + blob_name
73- block_blob_service .get_blob_to_path (container_name = container ,blob_name = blob .name , file_path = downloadedblob , open_mode = 'w' )
75+
76+ #Download the blob locally
77+ blob_client = container_client .get_blob_client (blob .name )
78+ with open (downloadedblob , "wb" ) as my_blob :
79+ downloaded_blob_stream = blob_client .download_blob ()
80+ my_blob .write (downloaded_blob_stream .readall ())
81+
7482 if pattern is not None :
7583 json_outpath = processcsvfile (fname = downloadedblob ,seperator = "|" ,outfname = blob_name ,outdir = 'jsonfiles/' + container + "/" + pattern )
76- print ("uploading blob" + json_outpath )
77- block_blob_service .create_blob_from_path (container_name = container ,blob_name = str (pattern + 'json/' + blob_name + ".json" ),file_path = json_outpath )
84+ print ("uploading blob " + json_outpath )
85+ with open (json_outpath , "rb" ) as data :
86+ container_client .upload_blob (name = str (pattern + 'json/' + blob_name + ".json" ), data = data )
7887 else :
7988 json_outpath = processcsvfile (fname = downloadedblob ,seperator = "|" ,outfname = blob_name ,outdir = 'jsonfiles/' + container + "/" )
80- print ("uploading blob" + json_outpath )
81- block_blob_service .create_blob_from_path (container_name = container ,blob_name = str ('json/' + blob_name + ".json" ),file_path = json_outpath )
89+ print ("uploading blob " + json_outpath )
90+ with open (json_outpath , "rb" ) as data :
91+ container_client .upload_blob (name = str ('json/' + blob_name + ".json" ), data = data )
0 commit comments