-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathget_models_azure.py
34 lines (28 loc) · 1.55 KB
/
get_models_azure.py
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
# modify the following function to download all pickle files from azure blob storage and save in a models folder
# create a function to read pkl files from blob storage
import pickle
from azure.storage.blob import BlobServiceClient
import os
import logging
# Configura el logger para mostrar mensajes en la consola
logging.basicConfig(level=logging.INFO)
logger = logging.getLogger(__name__)
def read_pkl_blob_azure(account_name, account_key, container_name):
#create a client to interact with blob storage
connect_str = 'DefaultEndpointsProtocol=https;AccountName=' + account_name + ';AccountKey=' + account_key + ';EndpointSuffix=core.windows.net'
blob_service_client = BlobServiceClient.from_connection_string(connect_str)
#use the client to connect to the container
container_client = blob_service_client.get_container_client(container_name)
# create the models folder
os.makedirs('models', exist_ok=True)
# iterate over the blobs in the container
for blob_i in container_client.list_blobs():
# download the file pkl file and save in a models folder
if blob_i.name.endswith('.pkl'):
with open(os.path.join('models', blob_i.name), 'wb') as file:
file.write(container_client.get_blob_client(blob_i.name).download_blob().readall())
# define the azure credentials
#enter credentials
account_name = os.environ.get('SECRET_NAME_BS_AZURE')
account_key = os.environ.get('SECRET_KEY_BS_AZURE')
read_pkl_blob_azure(account_name, account_key, 'models')