Skip to content

Commit 5d3d1b0

Browse files
committed
Closes UNB-2217 Create storage interface for Azure [api library].
1 parent 3a614a9 commit 5d3d1b0

File tree

2 files changed

+36
-3
lines changed

2 files changed

+36
-3
lines changed

unboxapi/__init__.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,10 +25,11 @@ class DeploymentType(Enum):
2525
ONPREM = 1
2626
AWS = 2
2727
GCP = 3
28+
AZURE = 4
2829

2930

3031
# NOTE: Don't modify this unless you are deploying on-prem.
31-
DEPLOYMENT = DeploymentType.AWS
32+
DEPLOYMENT = DeploymentType.AZURE
3233

3334

3435
class UnboxClient(object):
@@ -55,6 +56,8 @@ def __init__(self, api_key: str):
5556
self.upload = self.api.upload_blob_s3
5657
elif DEPLOYMENT == DeploymentType.GCP:
5758
self.upload = self.api.upload_blob_gcs
59+
elif DEPLOYMENT == DeploymentType.AZURE:
60+
self.upload = self.api.upload_blob_azure
5861
else:
5962
self.upload = self.api.transfer_blob
6063

unboxapi/api.py

Lines changed: 32 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,8 @@
1010
from .exceptions import ExceptionMap, UnboxException
1111
from .version import __version__
1212

13-
UNBOX_ENDPOINT = "https://api.unbox.ai/api"
14-
# UNBOX_ENDPOINT = "http://localhost:8080/api"
13+
# UNBOX_ENDPOINT = "https://api.unbox.ai/api"
14+
UNBOX_ENDPOINT = "http://localhost:8080/api"
1515
UNBOX_STORAGE_PATH = os.path.expanduser("~/unbox/unbox-onpremise/userStorage")
1616

1717

@@ -196,6 +196,36 @@ def upload_blob_gcs(
196196
else:
197197
self._raise_on_respose(res)
198198

199+
def upload_blob_azure(
200+
self, endpoint: str, file_path: str, object_name: str = None, body=None
201+
):
202+
"""Generic method to upload data to Azure Blob Storage and create the appropriate resource
203+
in the backend.
204+
"""
205+
params = {"storageInterface": "azure", "objectName": object_name}
206+
presigned_json = self.get_request(endpoint, params=params)
207+
with open(file_path, "rb") as f:
208+
with tqdm(
209+
total=os.stat(file_path).st_size,
210+
unit="B",
211+
unit_scale=True,
212+
unit_divisor=1024,
213+
) as t:
214+
wrapped_file = CallbackIOWrapper(t.update, f, "read")
215+
res = requests.put(
216+
presigned_json["url"],
217+
data=wrapped_file,
218+
headers={
219+
"Content-Type": "application/x-gzip",
220+
"x-ms-blob-type": "BlockBlob",
221+
},
222+
)
223+
if res.ok:
224+
body["storagePath"] = presigned_json["storagePath"]
225+
return self.post_request(f"{endpoint}/{presigned_json['id']}", body=body)
226+
else:
227+
self._raise_on_respose(res)
228+
199229
def transfer_blob(self, endpoint: str, file_path: str, object_name: str, body=None):
200230
"""Generic method to transfer data to the unbox folder and create the appropriate
201231
resource in the backend when using a local deployment.

0 commit comments

Comments
 (0)