Repository navigation
Expand file tree
/
Copy pathconfig_uploader.py
More file actions
101 lines (76 loc) · 4.28 KB
/
Copy pathconfig_uploader.py
File metadata and controls
101 lines (76 loc) · 4.28 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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
import os
import logging
from azure.storage.blob import BlobServiceClient, BlobClient, ContainerClient
from azure.core.exceptions import ResourceNotFoundError, ServiceRequestError
logger = logging.getLogger(__name__)
connection_string = "<Storage-account-connection-string>"
container = "leotest"
jobid = "test-job-id"
config_file = "/home/leotest/leotest-testbed-v2/leotest/experiment-config.yaml"
def upload_file(connection_string, container, local_path, remote_path, overwrite=True):
"""upload file to Azure Blob Storage"""
blob_service_client = BlobServiceClient.from_connection_string(connection_string)
path_remove = os.path.join(
os.path.normpath(os.path.join(local_path, os.pardir)),
'')
# print('path_remove=', path_remove)
file_path_azure = os.path.join(remote_path,
local_path.replace(path_remove, ""))
blob_client = blob_service_client.get_blob_client(
container=container,
blob=file_path_azure)
logging.info('uploading %s to %s' % (local_path, remote_path))
with open(local_path, "rb") as data:
blob_client.upload_blob(data, overwrite=overwrite)
def download_file(connection_string, container, remote_path, local_path):
"""download file from Azure Blob Storage"""
blob_service_client = BlobServiceClient.from_connection_string(connection_string)
container_client = blob_service_client.get_container_client(container)
with open(local_path, "wb") as download_file:
download_file.write(container_client.download_blob(remote_path).readall())
def upload_folder(connection_string, container, local_path, remote_path, overwrite=True):
"""upload artifact folder to Azure Blob Storage"""
blob_service_client = BlobServiceClient.from_connection_string(
connection_string)
path_remove = os.path.join(
os.path.normpath(os.path.join(local_path, os.pardir)),
'')
for r,d,f in os.walk(local_path):
if f:
for file in f:
# print('r=' + r + ' d=' + str(d) + ' f=' + str(f) + ' file=' + str(file))
file_path_azure = os.path.join(
remote_path,
os.path.join(r,file)
.replace(path_remove, ""))
file_path_local = os.path.join(r, file)
blob_client = blob_service_client.get_blob_client(
container=container,
blob=file_path_azure)
# Upload the file
with open(file_path_local, "rb") as data:
blob_client.upload_blob(data, overwrite=overwrite)
upload_file(connection_string, container, config_file, "jobs/")
download_file(connection_string, container, 'jobs/experiment-config.yaml', "downloaded_config.yaml")
# count = 0
# while count < self.max_tries:
# try:
# for r,d,f in os.walk(local_path):
# if f:
# for file in f:
# # print('r=' + r + ' d=' + str(d) + ' f=' + str(f) + ' file=' + str(file))
# file_path_azure = os.path.join(
# remote_path,
# os.path.join(r,file)
# .replace(path_remove, ""))
# file_path_local = os.path.join(r, file)
# blob_client = blob_service_client.get_blob_client(
# container=self.container,
# blob=file_path_azure)
# # Upload the file
# with open(file_path_local, "rb") as data:
# blob_client.upload_blob(data, overwrite=overwrite)
# break
# except ServiceRequestError:
# count +=1
# logging.warn('error connecting to blob storage, attempt: %d' % (count))