/
container_client.py
593 lines (551 loc) · 26.4 KB
/
container_client.py
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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
# -------------------------------------------------------------------------
# Copyright (c) Microsoft Corporation. All rights reserved.
# Licensed under the MIT License. See License.txt in the project root for
# license information.
# --------------------------------------------------------------------------
import functools
from typing import ( # pylint: disable=unused-import
Union, Optional, Any, Iterable, AnyStr, Dict, List, Tuple,
TYPE_CHECKING
)
from azure.storage.blob._shared_access_signature import BlobSharedAccessSignature
try:
from urllib.parse import urlparse, quote, unquote
except ImportError:
from urlparse import urlparse
from urllib2 import quote, unquote
from azure.core import Configuration
from .common import BlobType
from .lease import Lease
from .blob_client import BlobClient
from .models import ContainerProperties, BlobProperties, BlobPropertiesPaged, AccessPolicy
from ._utils import (
create_client,
create_configuration,
create_pipeline,
basic_error_map,
get_access_conditions,
get_modification_conditions,
return_response_headers,
add_metadata_headers,
process_storage_error,
encode_base64,
parse_connection_str,
serialize_iso,
basic_error_map,
return_response_and_deserialized)
from ._deserialize import (
deserialize_container_properties,
deserialize_metadata
)
from ._generated.models import (
ListBlobsIncludeItem,
BlobHTTPHeaders,
StorageErrorException,
SignedIdentifier)
if TYPE_CHECKING:
from azure.core.pipeline.transport import HttpTransport
from azure.core.pipeline.policies import HTTPPolicy
from .common import PublicAccess
from .models import ContainerPermissions
from datetime import datetime
class ContainerClient(object):
def __init__(
self, url, # type: str
container=None, # type: Union[ContainerProperties, str]
credentials=None, # type: Optional[HTTPPolicy]
configuration=None, # type: Optional[Configuration]
**kwargs # type: Any
):
# type: (...) -> None
"""Creates a new ContainerClient. This client represents interaction with a specific
container, although that container may not yet exist.
:param str url: The full URI to the container. This can also be a URL to the storage
account, in which case the blob container must also be specified.
:param container: The container for the blob. If specified, this value will override
a container value specified in the blob URL.
:type container: str or ~azure.storage.blob.models.ContainerProperties
:param ~azure.storage.blob.authentication.SharedKeyCredentials credentials: Optional shared
key credentials. This is not necessary if the URL contains a SAS token, or if the blob is
publicly available.
:param configuration: A optional pipeline configuration.
This can be retrieved with :func:`ContainerClient.create_configuration()`
"""
parsed_url = urlparse(url.rstrip('/'))
if not parsed_url.path and not container:
raise ValueError("Please specify a container name.")
path_container = ""
if parsed_url.path:
path_container = parsed_url.path.partition('/')[0]
try:
self.name = container.name
except AttributeError:
self.name = container or unquote(path_container)
self.scheme = parsed_url.scheme
self.credentials = credentials
self.account = parsed_url.hostname.split(".blob.core.")[0]
self.url = url if parsed_url.path else "{}://{}/{}".format(
self.scheme,
parsed_url.hostname,
quote(self.name)
)
self.require_encryption = kwargs.get('require_encryption', False)
self.key_encryption_key = kwargs.get('key_encryption_key')
self.key_resolver_function = kwargs.get('key_resolver_function')
self._config, self._pipeline = create_pipeline(configuration, credentials, **kwargs)
self._client = create_client(self.url, self._pipeline)
@staticmethod
def create_configuration(**kwargs):
# type: (**Any) -> Configuration
return create_configuration(**kwargs)
@classmethod
def from_connection_string(
cls, conn_str, # type: str
container, # type: Union[str, ContainerProperties]
credentials=None, # type: Optional[HTTPPolicy]
configuration=None, # type: Optional[Configuration]
**kwargs # type: Any
):
"""
Create ContainerClient from a Connection String.
"""
account_url, creds = parse_connection_str(conn_str, credentials)
return cls(
account_url, container=container,
credentials=creds, configuration=configuration, **kwargs)
def make_url(self, protocol=None, sas_token=None):
# type: (Optional[str], Optional[str]) -> str
parsed_url = urlparse(self.url)
new_scheme = protocol or parsed_url.scheme
query = []
if sas_token:
query.append(sas_token)
new_url = "{}://{}{}".format(
new_scheme,
parsed_url.netloc,
parsed_url.path)
if query:
new_url += "?{}".format('&'.join(query))
return new_url
def generate_shared_access_signature(
self, permission=None, # type: Optional[Union[ContainerPermissions, str]]
expiry=None, # type: Optional[Union[datetime, str]]
start=None, # type: Optional[Union[datetime, str]]
policy_id=None, # type: Optional[str]
ip=None, # type: Optional[str]
protocol=None, # type: Optional[str]
cache_control=None, # type: Optional[str]
content_disposition=None, # type: Optional[str]
content_encoding=None, # type: Optional[str]
content_language=None, # type: Optional[str]
content_type=None # type: Optional[str]
):
# type: (...) -> str
if not hasattr(self.credentials, 'account_key') and not self.credentials.account_key:
raise ValueError("No account SAS key available.")
sas = BlobSharedAccessSignature(self.account, self.credentials.account_key)
return sas.generate_container(
self.name,
permission=permission,
expiry=expiry,
start=start,
policy_id=policy_id,
ip=ip,
protocol=protocol,
cache_control=cache_control,
content_disposition=content_disposition,
content_encoding=content_encoding,
content_language=content_language,
content_type=content_type,
)
def create_container(self, metadata=None, public_access=None, timeout=None, **kwargs):
# type: (Optional[Dict[str, str]], Optional[Union[PublicAccess, str]], Optional[int]) -> None
"""
Creates a new container under the specified account. If the container
with the same name already exists, the operation fails.
:param metadata:
A dict with name_value pairs to associate with the
container as metadata. Example:{'Category':'test'}
:type metadata: dict(str, str)
:param ~azure.storage.blob.models.PublicAccess public_access:
Possible values include: container, blob.
:param int timeout:
The timeout parameter is expressed in seconds.
:rtype: None
"""
headers = kwargs.pop('headers', {})
headers.update(add_metadata_headers(metadata))
try:
return self._client.container.create(
timeout=timeout,
access=public_access,
cls=return_response_headers,
headers=headers,
error_map=basic_error_map(),
**kwargs)
except StorageErrorException as error:
process_storage_error(error)
def delete_container(
self, lease=None, # type: Optional[Union[Lease, str]]
if_modified_since=None, # type: Optional[datetime]
if_unmodified_since=None, # type: Optional[datetime]
if_match=None, # type: Optional[str]
if_none_match=None, # type: Optional[str]
timeout=None, # type: Optional[int]
**kwargs
):
# type: (...) -> None
"""
Marks the specified container for deletion. The container and any blobs
contained within it are later deleted during garbage collection.
:param ~azure.storage.blob.lease.Lease lease:
If specified, delete_container only succeeds if the
container's lease is active and matches this ID.
Required if the container has an active lease.
:param datetime if_modified_since:
A DateTime value. Azure expects the date value passed in to be UTC.
If timezone is included, any non-UTC datetimes will be converted to UTC.
If a date is passed in without timezone info, it is assumed to be UTC.
Specify this header to perform the operation only
if the resource has been modified since the specified time.
:param datetime if_unmodified_since:
A DateTime value. Azure expects the date value passed in to be UTC.
If timezone is included, any non-UTC datetimes will be converted to UTC.
If a date is passed in without timezone info, it is assumed to be UTC.
Specify this header to perform the operation only if
the resource has not been modified since the specified date/time.
:param int timeout:
The timeout parameter is expressed in seconds.
:rtype: None
"""
access_conditions = get_access_conditions(lease)
mod_conditions = get_modification_conditions(
if_modified_since, if_unmodified_since, if_match, if_none_match)
try:
self._client.container.delete(
timeout=timeout,
lease_access_conditions=access_conditions,
modified_access_conditions=mod_conditions,
error_map=basic_error_map(),
**kwargs)
except StorageErrorException as error:
process_storage_error(error)
def acquire_lease(
self, lease_duration=-1, # type: int
lease_id=None, # type: Optional[str]
if_modified_since=None, # type: Optional[datetime]
if_unmodified_since=None, # type: Optional[datetime]
if_match=None, # type: Optional[str]
if_none_match=None, # type: Optional[str]
timeout=None, # type: Optional[int]
**kwargs):
# type: (...) -> Lease
"""
Requests a new lease. If the container does not have an active lease,
the Blob service creates a lease on the container and returns a new
lease ID.
:param int lease_duration:
Specifies the duration of the lease, in seconds, or negative one
(-1) for a lease that never expires. A non-infinite lease can be
between 15 and 60 seconds. A lease duration cannot be changed
using renew or change. Default is -1 (infinite lease).
:param str lease_id:
Proposed lease ID, in a GUID string format. The Blob service returns
400 (Invalid request) if the proposed lease ID is not in the correct format.
:param datetime if_modified_since:
A DateTime value. Azure expects the date value passed in to be UTC.
If timezone is included, any non-UTC datetimes will be converted to UTC.
If a date is passed in without timezone info, it is assumed to be UTC.
Specify this header to perform the operation only
if the resource has been modified since the specified time.
:param datetime if_unmodified_since:
A DateTime value. Azure expects the date value passed in to be UTC.
If timezone is included, any non-UTC datetimes will be converted to UTC.
If a date is passed in without timezone info, it is assumed to be UTC.
Specify this header to perform the operation only if
the resource has not been modified since the specified date/time.
:param int timeout:
The timeout parameter is expressed in seconds.
:returns: A Lease object, that can be run in a context manager.
:rtype: ~azure.storage.blob.lease.Lease
"""
mod_conditions = get_modification_conditions(
if_modified_since, if_unmodified_since, if_match, if_none_match)
try:
response = self._client.container.acquire_lease(
timeout=timeout,
duration=lease_duration,
proposed_lease_id=lease_id,
modified_access_conditions=mod_conditions,
cls=return_response_headers,
error_map=basic_error_map(),
**kwargs)
except StorageErrorException as error:
process_storage_error(error)
return Lease(self._client.container, **response)
def break_lease(
self, lease_break_period=None, # type: Optional[int]
if_modified_since=None, # type: Optional[datetime]
if_unmodified_since=None, # type: Optional[datetime]
timeout=None, # type: Optional[int]
**kwargs):
# type: (...) -> int
"""
Break the lease, if the container has an active lease. Once a lease is
broken, it cannot be renewed. Any authorized request can break the lease;
the request is not required to specify a matching lease ID. When a lease
is broken, the lease break period is allowed to elapse, during which time
no lease operation except break and release can be performed on the container.
When a lease is successfully broken, the response indicates the interval
in seconds until a new lease can be acquired.
:param int lease_break_period:
This is the proposed duration of seconds that the lease
should continue before it is broken, between 0 and 60 seconds. This
break period is only used if it is shorter than the time remaining
on the lease. If longer, the time remaining on the lease is used.
A new lease will not be available before the break period has
expired, but the lease may be held for longer than the break
period. If this header does not appear with a break
operation, a fixed-duration lease breaks after the remaining lease
period elapses, and an infinite lease breaks immediately.
:param datetime if_modified_since:
A DateTime value. Azure expects the date value passed in to be UTC.
If timezone is included, any non-UTC datetimes will be converted to UTC.
If a date is passed in without timezone info, it is assumed to be UTC.
Specify this header to perform the operation only
if the resource has been modified since the specified time.
:param datetime if_unmodified_since:
A DateTime value. Azure expects the date value passed in to be UTC.
If timezone is included, any non-UTC datetimes will be converted to UTC.
If a date is passed in without timezone info, it is assumed to be UTC.
Specify this header to perform the operation only if
the resource has not been modified since the specified date/time.
:param int timeout:
The timeout parameter is expressed in seconds.
:return: Approximate time remaining in the lease period, in seconds.
:rtype: int
"""
mod_conditions = get_modification_conditions(
if_modified_since, if_unmodified_since)
try:
response = self._client.container.break_lease(
timeout=timeout,
break_period=lease_break_period,
modified_access_conditions=mod_conditions,
cls=return_response_headers,
error_map=basic_error_map(),
**kwargs)
except StorageErrorException as error:
process_storage_error(error)
return response.get('x-ms-lease-time')
def get_account_information(self, timeout=None, **kwargs):
# type: (Optional[int]) -> Dict[str, str]
"""
:returns: A dict of account information (SKU and account type).
"""
try:
response = self._client.container.get_account_info(
cls=return_response_headers,
timeout=timeout,
error_map=basic_error_map(),
**kwargs)
except StorageErrorException as error:
process_storage_error(error)
return {
'SKU': response.get('x-ms-sku-name'),
'AccountType': response.get('x-ms-account-kind')
}
def get_container_properties(self, lease=None, timeout=None, **kwargs):
# type: (Optional[Union[Lease, str]], Optional[int], **Any) -> ContainerProperties
"""
Returns all user-defined metadata and system properties for the specified
container. The data returned does not include the container's list of blobs.
:param ~azure.storage.blob.lease.Lease lease:
If specified, get_container_properties only succeeds if the
container's lease is active and matches this ID.
:param int timeout:
The timeout parameter is expressed in seconds.
:return: properties for the specified container within a container object.
:rtype: ~azure.storage.blob.models.ContainerProperties
"""
access_conditions = get_access_conditions(lease)
try:
response = self._client.container.get_properties(
timeout=timeout,
lease_access_conditions=access_conditions,
cls=deserialize_container_properties,
error_map=basic_error_map(),
**kwargs)
except StorageErrorException as error:
process_storage_error(error)
response.name = self.name
return response
def set_container_metadata(
self, metadata=None, # type: Optional[Dict[str, str]]
lease=None, # type: Optional[Union[str, Lease]]
if_modified_since=None, # type: Optional[datetime]
timeout=None, # type: Optional[int]
**kwargs
):
# type: (...) -> Dict[str, Union[str, datetime]]
"""
:returns: Container-updated property dict (Etag and last modified).
"""
headers = kwargs.pop('headers', {})
headers.update(add_metadata_headers(metadata))
access_conditions = get_access_conditions(lease)
mod_conditions = get_modification_conditions(if_modified_since)
try:
return self._client.container.set_metadata(
timeout=timeout,
lease_access_conditions=access_conditions,
modified_access_conditions=mod_conditions,
cls=return_response_headers,
headers=headers,
error_map=basic_error_map(),
**kwargs)
except StorageErrorException as error:
process_storage_error(error)
def get_container_acl(self, lease=None, timeout=None, **kwargs):
# type: (Optional[Union[Lease, str]], Optional[int]) -> Dict[str, str]
"""
:returns: Access policy information in a dict.
"""
access_conditions = get_access_conditions(lease)
try:
response = self._client.container.get_access_policy(
timeout=timeout,
lease_access_conditions=access_conditions,
cls=return_response_and_deserialized,
error_map=basic_error_map(),
**kwargs)
except StorageErrorException as error:
process_storage_error(error)
return {
'public_access': response.get('header').get('x-ms-blob-public-access'),
'signed_identifiers': response.get('deserialized', [])
}
def set_container_acl(
self, signed_identifiers=None, # type: Optional[Dict[str, Optional[AccessPolicy]]]
public_access=None, # type: Optional[Union[str, PublicAccess]]
lease=None, # type: Optional[Union[str, Lease]]
if_modified_since=None, # type: Optional[datetime]
if_unmodified_since=None, # type: Optional[datetime]
timeout=None, # type: Optional[int]
**kwargs
):
"""
:returns: Container-updated property dict (Etag and last modified).
"""
if signed_identifiers:
if len(signed_identifiers) > 5:
raise ValueError(
'Too many access policies provided. The server does not support setting '
'more than 5 access policies on a single resource.')
identifiers = []
for key, value in signed_identifiers.items():
identifiers.append(SignedIdentifier(id=key, access_policy=value))
signed_identifiers = identifiers
mod_conditions = get_modification_conditions(
if_modified_since, if_unmodified_since)
access_conditions = get_access_conditions(lease)
try:
response = self._client.container.set_access_policy(
container_acl=signed_identifiers or None,
timeout=timeout,
access=public_access,
lease_access_conditions=access_conditions,
modified_access_conditions=mod_conditions,
cls=return_response_headers,
error_map=basic_error_map(),
**kwargs)
except StorageErrorException as error:
process_storage_error(error)
return {
'ETag': response.get('ETag'),
'Last-Modified': response.get('Last-Modified')
}
def list_blob_properties(self, starts_with=None, include=None, marker=None, timeout=None, **kwargs):
# type: (Optional[str], Optional[Include], Optional[int]) -> Iterable[BlobProperties]
"""
Returns a generator to list the blobs under the specified container.
The generator will lazily follow the continuation tokens returned by
the service.
:param str starts_with:
Filters the results to return only blobs whose names
begin with the specified prefix.
:param ~azure.storage.blob.models.Include include:
Specifies one or more additional datasets to include in the response.
:param str marker:
An opaque continuation token. This value can be retrieved from the
next_marker field of a previous generator object. If specified,
this generator will begin returning results from this point.
:param int timeout:
The timeout parameter is expressed in seconds.
:returns: An iterable (auto-paging) response of BlobProperties.
"""
if include and not isinstance(include, list):
include = [include]
results_per_page = kwargs.pop('results_per_page', None)
command = functools.partial(
self._client.container.list_blob_flat_segment,
prefix=starts_with,
include=include,
timeout=timeout,
error_map=basic_error_map(),
**kwargs)
return BlobPropertiesPaged(command, prefix=starts_with, results_per_page=results_per_page, marker=marker)
# def walk_blob_properties(self, starts_with=None, include=None, delimiter="/", timeout=None, **kwargs):
# # type: (Optional[str], Optional[Include], Optional[int]) -> Iterable[BlobProperties]
# """
# Returns a generator to list the blobs under the specified container.
# The generator will lazily follow the continuation tokens returned by
# the service.
# :param str starts_with:
# Filters the results to return only blobs whose names
# begin with the specified prefix.
# :param ~azure.storage.blob.models.Include include:
# Specifies one or more additional datasets to include in the response.
# :param str marker:
# An opaque continuation token. This value can be retrieved from the
# next_marker field of a previous generator object. If specified,
# this generator will begin returning results from this point.
# :param int timeout:
# The timeout parameter is expressed in seconds.
# :returns: An iterable (auto-paging) response of BlobProperties.
# """
# if include and not isinstance(include, list):
# include = [include]
# results_per_page = kwargs.pop('results_per_page', None)
# marker = kwargs.pop('marker', "")
# command = functools.partial(
# self._client.container.list_blob_hierarchy_segment(
# delimiter,
# prefix=starts_with,
# include=include,
# timeout=timeout,
# **kwargs)
# return BlobPropertiesWalked(command, prefix=starts_with, results_per_page=results_per_page, marker=marker)
def get_blob_client(
self, blob, # type: Union[str, BlobProperties]
blob_type=BlobType.BlockBlob, # type: Union[BlobType, str]
snapshot=None # type: str
):
# type: (...) -> BlobClient
"""
Get a client to interact with the specified blob.
The blob need not already exist.
:param blob: The blob with which to interact. If specified, this value will override
a blob value specified in the blob URL.
:type blob: str or ~azure.storage.blob.models.BlobProperties
:param ~azure.storage.blob.common.BlobType blob_type: The type of Blob. Default
vale is BlobType.BlockBlob
:param str snapshot: The optional blob snapshot on which to operate.
:returns: A BlobClient.
:rtype: ~azure.core.blob.blob_client.BlobClient
"""
return BlobClient(
self.url, container=self.name, blob=blob, blob_type=blob_type, snapshot=snapshot,
credentials=self.credentials, configuration=self._config, _pipeline=self._pipeline,
require_encryption=self.require_encryption, key_encryption_key=self.key_encryption_key,
key_resolver_function=self.key_resolver_function)