Skip to content

Commit 576953d

Browse files
Added blob exists method (#13221)
* added feature and unit tests * fixed failing test issue * added async method and more unit tests * ffixed passed parameters * fixed python 27 issue with kwargs * reset commit * Update _blob_client_async.py removed unused import, fixed linter * Update sdk/storage/azure-storage-blob/azure/storage/blob/aio/_blob_client_async.py Co-authored-by: Xiaoxi Fu <[email protected]> * Update sdk/storage/azure-storage-blob/azure/storage/blob/_blob_client.py Co-authored-by: Xiaoxi Fu <[email protected]> * fixed failing tests * fixed linting/import order Co-authored-by: Xiaoxi Fu <[email protected]>
1 parent 23cd9f6 commit 576953d

6 files changed

+1329
-0
lines changed

sdk/storage/azure-storage-blob/azure/storage/blob/_blob_client.py

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818

1919
import six
2020
from azure.core.tracing.decorator import distributed_trace
21+
from azure.core.exceptions import ResourceNotFoundError
2122

2223
from ._shared import encode_base64
2324
from ._shared.base_client import StorageAccountHostsMixin, parse_connection_str, parse_query
@@ -929,6 +930,31 @@ def undelete_blob(self, **kwargs):
929930
except StorageErrorException as error:
930931
process_storage_error(error)
931932

933+
@distributed_trace()
934+
def exists(self, **kwargs):
935+
# type: (**Any) -> bool
936+
"""
937+
Returns True if a blob exists with the defined parameters, and returns
938+
False otherwise.
939+
940+
:param str version_id:
941+
The version id parameter is an opaque DateTime
942+
value that, when present, specifies the version of the blob to check if it exists.
943+
:param int timeout:
944+
The timeout parameter is expressed in seconds.
945+
:returns: boolean
946+
"""
947+
try:
948+
self._client.blob.get_properties(
949+
snapshot=self.snapshot,
950+
**kwargs)
951+
return True
952+
except StorageErrorException as error:
953+
try:
954+
process_storage_error(error)
955+
except ResourceNotFoundError:
956+
return False
957+
932958
@distributed_trace
933959
def get_blob_properties(self, **kwargs):
934960
# type: (**Any) -> BlobProperties

sdk/storage/azure-storage-blob/azure/storage/blob/aio/_blob_client_async.py

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
)
1212

1313
from azure.core.tracing.decorator_async import distributed_trace_async
14+
from azure.core.exceptions import ResourceNotFoundError
1415

1516
from .._shared.base_client_async import AsyncStorageAccountHostsMixin
1617
from .._shared.policies_async import ExponentialRetry
@@ -30,6 +31,7 @@
3031
from ._lease_async import BlobLeaseClient
3132
from ._download_async import StorageStreamDownloader
3233

34+
3335
if TYPE_CHECKING:
3436
from datetime import datetime
3537
from .._models import ( # pylint: disable=unused-import
@@ -459,6 +461,31 @@ async def undelete_blob(self, **kwargs):
459461
except StorageErrorException as error:
460462
process_storage_error(error)
461463

464+
@distributed_trace_async
465+
async def exists(self, **kwargs):
466+
# type: (**Any) -> bool
467+
"""
468+
Returns True if a blob exists with the defined parameters, and returns
469+
False otherwise.
470+
471+
:param str version_id:
472+
The version id parameter is an opaque DateTime
473+
value that, when present, specifies the version of the blob to check if it exists.
474+
:param int timeout:
475+
The timeout parameter is expressed in seconds.
476+
:returns: boolean
477+
"""
478+
try:
479+
await self._client.blob.get_properties(
480+
snapshot=self.snapshot,
481+
**kwargs)
482+
return True
483+
except StorageErrorException as error:
484+
try:
485+
process_storage_error(error)
486+
except ResourceNotFoundError:
487+
return False
488+
462489
@distributed_trace_async
463490
async def get_blob_properties(self, **kwargs):
464491
# type: (Any) -> BlobProperties

0 commit comments

Comments
 (0)