Skip to content

Commit 5ff74f8

Browse files
authored
Merge pull request #33 from TaskarCenterAtUW/feature-clone-delete
Added methods
2 parents 92e77b8 + 99f4eb4 commit 5ff74f8

8 files changed

Lines changed: 60 additions & 15 deletions

File tree

freeze_version.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111

1212
build_date = date.today().strftime('%Y-%m-%d')
1313

14-
version = '0.0.19'
14+
version = '0.0.20'
1515

1616
with open(version_file_path, 'w+') as version_file:
1717
version_file.write("version = '{}'\n".format(version))

src/python_ms_core/core/storage/abstract/file_entity.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,3 +26,7 @@ def upload(self, upload_stream):
2626
@abstractmethod
2727
def get_remote_url(self):
2828
pass
29+
30+
@abstractmethod
31+
def delete_file(self):
32+
pass

src/python_ms_core/core/storage/abstract/storage_client.py

Lines changed: 3 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -56,16 +56,6 @@ def get_sas_url(self, container_name: str, file_path: str, expiry_hours: int) ->
5656
The SAS URL as a string.
5757
"""
5858
pass
59-
# class StorageClient(ABC):
60-
# #
61-
# @abstractmethod
62-
# def get_container(self, name: str): pass
63-
64-
# @abstractmethod
65-
# def get_file(self, container_name: str, file_name: str): pass
66-
67-
# @abstractmethod
68-
# def get_file_from_url(self, container_name: str, full_url: str): pass
69-
70-
# @abstractmethod
71-
# def get_sas_url(self, container_name:str,file_path: str, expiry_hours:int) -> str: pass
59+
@abstractmethod
60+
def clone_file(self, file_url:str, destination_container_name:str, destination_file_path:str):
61+
pass

src/python_ms_core/core/storage/providers/azure/azure_file_entity.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,3 +27,7 @@ def upload(self, upload_stream):
2727
@ExceptionHandler.decorated
2828
def get_remote_url(self):
2929
return self._get_remote_url
30+
31+
@ExceptionHandler.decorated
32+
def delete_file(self):
33+
self.blob_client.delete_blob()

src/python_ms_core/core/storage/providers/azure/azure_storage_client.py

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,3 +97,35 @@ def get_sas_url(self, container_name: str, file_path: str, expiry_hours:int = 1
9797
)
9898
sas_url = f"https://{self._blob_service_client.account_name}.blob.core.windows.net/{container_name}/{file_path}?{sas_token}"
9999
return sas_url
100+
101+
def get_container_info(self, file_url:str):
102+
"""
103+
Retrieves the container name and file path from a file URL.
104+
105+
Args:
106+
file_url (str): The URL of the file.
107+
108+
Returns:
109+
tuple: A tuple containing the container name and file path.
110+
"""
111+
container_name = file_url.split('/')[3]
112+
file_path = '/'.join(file_url.split('/')[4:])
113+
return container_name, file_path
114+
115+
116+
def clone_file(self, file_url:str, destination_container_name:str, destination_file_path:str):
117+
"""
118+
Clones a file from one container to another.
119+
120+
Args:
121+
file_url (str): The URL of the file to clone.
122+
destination_container_name (str): The name of the destination container.
123+
destination_file_path (str): The path of the destination file.
124+
"""
125+
source_container_name, source_file_path = self.get_container_info(file_url)
126+
source_container = self._blob_service_client.get_container_client(source_container_name)
127+
source_blob = source_container.get_blob_client(source_file_path)
128+
destination_container = self._blob_service_client.get_container_client(destination_container_name)
129+
destination_blob = destination_container.get_blob_client(destination_file_path)
130+
destination_blob.start_copy_from_url(source_blob.url)
131+
return destination_blob

src/python_ms_core/core/storage/providers/local/local_file_entity.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,3 +34,8 @@ def upload(self, upload_stream):
3434
@ExceptionHandler.decorated
3535
def get_remote_url(self):
3636
return self._get_remote_url
37+
38+
@ExceptionHandler.decorated
39+
def delete_file(self):
40+
delete_relative_path = f'{self.config.connection_string}{self.download_path}{self.path}'
41+
pass

src/python_ms_core/core/storage/providers/local/local_storage_client.py

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,4 +36,11 @@ def get_file_from_url(self, container_name: str, full_url: str):
3636

3737
@ExceptionHandler.decorated
3838
def get_sas_url(self, container_name: str, file_path: str, expiry_hours: int) -> str:
39-
return self.get_file_from_url(container_name, file_path).get_remote_url()
39+
return self.get_file_from_url(container_name, file_path).get_remote_url()
40+
41+
@ExceptionHandler.decorated
42+
def clone_file(self, file_url: str, destination_container_name: str, destination_file_path: str):
43+
file = self.get_file_from_url('', file_url)
44+
file.name = destination_file_path
45+
file.upload(file.get_stream())
46+
return file

tests/unit_tests/test_storage/abstract/test_file_entity.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,9 @@ def upload(self, upload_stream):
2121
def get_remote_url(self):
2222
# Mock implementation for getting the remote URL
2323
return 'http://example.com/file.txt'
24+
def delete_file(self):
25+
# Mock implementation for deleting the file
26+
pass
2427

2528

2629
class TestFileEntity(unittest.TestCase):

0 commit comments

Comments
 (0)