File tree Expand file tree Collapse file tree 2 files changed +42
-0
lines changed
commons/data_access_layer
tests/commons/data_access_layer Expand file tree Collapse file tree 2 files changed +42
-0
lines changed Original file line number Diff line number Diff line change 1+ import os
2+ from azure .storage .blob .blockblobservice import BlockBlobService
3+
4+ ACCOUNT_KEY = os .environ .get ('AZURE_STORAGE_ACCOUNT_KEY' )
5+
6+ class FileStream :
7+ def __init__ (self , account_name :str , container_name :str ):
8+ """
9+ Initialize the FileStream object. which is used to get the file stream from Azure Blob Storage.
10+ `account_name`: The name of the Azure Storage account.
11+ `container_name`: The name of the Azure Storage container.
12+ """
13+ self .account_name = account_name
14+ self .container_name = container_name
15+ self .blob_service = BlockBlobService (account_name = self .account_name , account_key = ACCOUNT_KEY )
16+
17+ def get_file_stream (self , filename :str ):
18+ import tempfile
19+ try :
20+ local_file = tempfile .NamedTemporaryFile ()
21+ self .blob_service .get_blob_to_stream (self .container_name , filename , stream = local_file )
22+
23+ local_file .seek (0 )
24+ return local_file
25+ except Exception as e :
26+ print (e )
27+ return None
Original file line number Diff line number Diff line change 1+ import json
2+
3+ from commons .data_access_layer .file_stream import FileStream
4+
5+ fs = FileStream ("storagefiles2" ,"ioetfiles" )
6+
7+ def test_get_file_stream_return_file_when_enter_file_name ():
8+ result = fs .get_file_stream ("activity.json" )
9+
10+ assert len (json .load (result )) == 15
11+
12+ def test_get_file_stream_return_None_when_not_enter_file_name_or_incorrect_name ():
13+ result = fs .get_file_stream ("" )
14+
15+ assert result == None
You can’t perform that action at this time.
0 commit comments