Skip to content

Commit 904b9c2

Browse files
authored
fix: TT-384 add enviroment var into MSConfig
1 parent cd5d7a9 commit 904b9c2

File tree

3 files changed

+16
-8
lines changed

3 files changed

+16
-8
lines changed

commons/data_access_layer/file_stream.py

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,15 @@
1-
import os
21
import tempfile
32

4-
from azure.storage.blob import BlockBlobService, ContainerPermissions
5-
from datetime import datetime, timedelta
3+
from azure.storage.blob import BlockBlobService
4+
from utils.azure_users import AzureConnection
65

7-
ACCOUNT_KEY = os.environ.get('AZURE_STORAGE_ACCOUNT_KEY')
86

97
class FileStream:
8+
ACCOUNT_KEY = AzureConnection().get_blob_storage_key()
9+
account_name:str
10+
container_name:str
11+
blob_service:BlockBlobService
12+
1013
def __init__(self, account_name:str, container_name:str):
1114
"""
1215
Initialize the FileStream object. which is used to get the file stream from Azure Blob Storage.
@@ -15,16 +18,16 @@ def __init__(self, account_name:str, container_name:str):
1518
"""
1619
self.account_name = account_name
1720
self.container_name = container_name
18-
self.blob_service = BlockBlobService(account_name=self.account_name, account_key=ACCOUNT_KEY)
1921

2022
def get_file_stream(self, filename:str):
2123
try:
2224
local_file = tempfile.NamedTemporaryFile()
25+
self.blob_service = BlockBlobService(account_name=self.account_name, account_key=self.ACCOUNT_KEY)
2326
self.blob_service.get_blob_to_stream(
2427
container_name=self.container_name,
2528
blob_name= filename,
2629
stream=local_file,
27-
max_connections=200
30+
max_connections=300
2831
)
2932

3033
local_file.seek(0)

time_tracker_api/activities/activities_model.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,7 @@ def find_all_from_blob_storage(
127127

128128
fs = FileStream("storageaccounteystr82c5","tt-common-files")
129129
result = fs.get_file_stream(file_name)
130-
return list(map(function_mapper, json.load(result))) if result is not None else [{"result": "error", "message": "file not found"}]
130+
return list(map(function_mapper, json.load(result))) if result is not None else []
131131

132132
class ActivityCosmosDBDao(APICosmosDBDao, ActivityDao):
133133
def __init__(self, repository):

utils/azure_users.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,8 @@ class MSConfig:
1313
'MS_SECRET',
1414
'MS_SCOPE',
1515
'MS_ENDPOINT',
16-
'USERID'
16+
'USERID',
17+
'AZURE_STORAGE_ACCOUNT_KEY'
1718
]
1819

1920
check_variables_are_defined(ms_variables)
@@ -24,6 +25,7 @@ class MSConfig:
2425
SCOPE = os.environ.get('MS_SCOPE')
2526
ENDPOINT = os.environ.get('MS_ENDPOINT')
2627
USERID = os.environ.get('USERID')
28+
AZURE_STORAGE_ACCOUNT_KEY = os.environ.get('AZURE_STORAGE_ACCOUNT_KEY')
2729

2830

2931
class BearerAuth(requests.auth.AuthBase):
@@ -68,6 +70,9 @@ def __init__(self, config=MSConfig):
6870
self.access_token = self.get_token()
6971
self.groups_and_users = None
7072

73+
def get_blob_storage_key(self) -> str:
74+
return self.config.AZURE_STORAGE_ACCOUNT_KEY
75+
7176
def get_msal_client(self):
7277
client = msal.ConfidentialClientApplication(
7378
self.config.CLIENT_ID,

0 commit comments

Comments
 (0)