31
31
get_storage_account_name_key_by_name ,
32
32
)
33
33
34
+ CONTAINER_SAS_DEFAULT_DURATION = datetime .timedelta (days = 30 )
35
+
34
36
35
37
def get_url (account_name : str ) -> str :
36
38
return f"https://{ account_name } .blob.core.windows.net/"
@@ -109,12 +111,15 @@ def get_container_metadata(
109
111
return cast (Dict [str , str ], result )
110
112
111
113
112
- def add_container_sas_url (container_url : str ) -> str :
114
+ def add_container_sas_url (
115
+ container_url : str , duration : datetime .timedelta = CONTAINER_SAS_DEFAULT_DURATION
116
+ ) -> str :
113
117
parsed = urllib .parse .urlparse (container_url )
114
118
query = urllib .parse .parse_qs (parsed .query )
115
119
if "sig" in query :
116
120
return container_url
117
121
else :
122
+ start , expiry = sas_time_window (duration )
118
123
account_name = parsed .netloc .split ("." )[0 ]
119
124
account_key = get_storage_account_name_key_by_name (account_name )
120
125
sas_token = generate_container_sas (
@@ -124,7 +129,8 @@ def add_container_sas_url(container_url: str) -> str:
124
129
permission = ContainerSasPermissions (
125
130
read = True , write = True , delete = True , list = True
126
131
),
127
- expiry = datetime .datetime .utcnow () + datetime .timedelta (hours = 1 ),
132
+ expiry = expiry ,
133
+ start = start ,
128
134
)
129
135
return f"{ container_url } ?{ sas_token } "
130
136
@@ -189,7 +195,7 @@ def delete_container(container: Container, storage_type: StorageType) -> bool:
189
195
190
196
191
197
def sas_time_window (
192
- * , days : int , hours : int , minutes : int
198
+ duration : datetime . timedelta ,
193
199
) -> Tuple [datetime .datetime , datetime .datetime ]:
194
200
# SAS URLs are valid 6 hours earlier, primarily to work around dev
195
201
# workstations having out-of-sync time. Additionally, SAS URLs are stopped
@@ -201,11 +207,7 @@ def sas_time_window(
201
207
202
208
now = datetime .datetime .utcnow ()
203
209
start = now - SAS_START_TIME_DELTA
204
- expiry = (
205
- now
206
- + datetime .timedelta (days = days , hours = hours , minutes = minutes )
207
- + SAS_END_TIME_DELTA
208
- )
210
+ expiry = now + duration + SAS_END_TIME_DELTA
209
211
return (start , expiry )
210
212
211
213
@@ -218,15 +220,13 @@ def get_container_sas_url_service(
218
220
list_ : bool = False ,
219
221
delete_previous_version : bool = False ,
220
222
tag : bool = False ,
221
- days : int = 30 ,
222
- hours : int = 0 ,
223
- minutes : int = 0 ,
223
+ duration : datetime .timedelta = CONTAINER_SAS_DEFAULT_DURATION ,
224
224
) -> str :
225
225
account_name = client .account_name
226
226
container_name = client .container_name
227
227
account_key = get_storage_account_name_key_by_name (account_name )
228
228
229
- start , expiry = sas_time_window (days = days , hours = hours , minutes = minutes )
229
+ start , expiry = sas_time_window (duration )
230
230
231
231
sas = generate_container_sas (
232
232
account_name ,
@@ -295,16 +295,14 @@ def get_file_sas_url(
295
295
delete : bool = False ,
296
296
delete_previous_version : bool = False ,
297
297
tag : bool = False ,
298
- days : int = 30 ,
299
- hours : int = 0 ,
300
- minutes : int = 0 ,
298
+ duration : datetime .timedelta = CONTAINER_SAS_DEFAULT_DURATION ,
301
299
) -> str :
302
300
client = find_container (container , storage_type )
303
301
if not client :
304
302
raise Exception ("unable to find container: %s - %s" % (container , storage_type ))
305
303
306
304
account_key = get_storage_account_name_key_by_name (client .account_name )
307
- start , expiry = sas_time_window (days = days , hours = hours , minutes = minutes )
305
+ start , expiry = sas_time_window (duration )
308
306
309
307
permission = BlobSasPermissions (
310
308
read = read ,
0 commit comments