-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathstorage_service.py
More file actions
50 lines (41 loc) · 1.88 KB
/
storage_service.py
File metadata and controls
50 lines (41 loc) · 1.88 KB
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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# Class for Storage service.
from python_ms_core import Core
from src.config import Settings
class StorageService:
"""
Class for handling storage operations.
Use this class for uploading and downloading files to and from a remote storage location.
"""
def __init__(self, core: Core) -> None:
self.storage_client = core.get_storage_client()
# TODO : Change storage location
self.config = Settings()
self.storage_container = self.storage_client.get_container(self.config.storage_container_name)
def upload_local_file(self, local_path: str, remote_path: str) -> str:
"""
Uploads a local file to a remote storage location.
Args:
local_path (str): The path to the local file to be uploaded.
remote_path (str): The path where the file should be stored remotely.
Returns:
str: The URL of the uploaded file in the remote storage.
"""
azure_file = self.storage_container.create_file(remote_path)
with open(local_path, 'rb') as file_stream:
azure_file.upload(file_stream.read())
remote_url = azure_file.get_remote_url()
return remote_url
def download_remote_file(self, remote_path: str, local_path: str) -> str:
"""
Downloads a file from a remote storage location to a local path.
Args:
remote_path (str): The path to the remote file to be downloaded.
local_path (str): The local path where the file should be saved.
Returns:
str: The local path where the file has been saved.
"""
# Change this to download from client
file_entity = self.storage_client.get_file_from_url(self.config.storage_container_name, remote_path)
with open(local_path, 'wb') as file_stream:
file_stream.write(file_entity.get_stream())
return ''