Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added blob exists method #13221

Merged
merged 12 commits into from
Sep 4, 2020
26 changes: 26 additions & 0 deletions sdk/storage/azure-storage-blob/azure/storage/blob/_blob_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@

import six
from azure.core.tracing.decorator import distributed_trace
from azure.core.exceptions import ResourceNotFoundError

from ._shared import encode_base64
from ._shared.base_client import StorageAccountHostsMixin, parse_connection_str, parse_query
Expand Down Expand Up @@ -929,6 +930,31 @@ def undelete_blob(self, **kwargs):
except StorageErrorException as error:
process_storage_error(error)

@distributed_trace()
def exists(self, **kwargs):
# type: (**Any) -> bool
"""
Returns True if a blob exists with the defined parameters, and returns
False otherwise.

:param str version_id:
The version id parameter is an opaque DateTime
value that, when present, specifies the version of the blob to check if it exists.
:param int timeout:
The timeout parameter is expressed in seconds.
:returns: boolean
"""
try:
self._client.blob.get_properties(
snapshot=self.snapshot,
**kwargs)
return True
except StorageErrorException as error:
try:
process_storage_error(error)
except ResourceNotFoundError:
return False

@distributed_trace
def get_blob_properties(self, **kwargs):
# type: (**Any) -> BlobProperties
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
)

from azure.core.tracing.decorator_async import distributed_trace_async
from azure.core.exceptions import ResourceNotFoundError

from .._shared.base_client_async import AsyncStorageAccountHostsMixin
from .._shared.policies_async import ExponentialRetry
Expand All @@ -30,6 +31,7 @@
from ._lease_async import BlobLeaseClient
from ._download_async import StorageStreamDownloader


if TYPE_CHECKING:
from datetime import datetime
from .._models import ( # pylint: disable=unused-import
Expand Down Expand Up @@ -459,6 +461,31 @@ async def undelete_blob(self, **kwargs):
except StorageErrorException as error:
process_storage_error(error)

@distributed_trace_async
async def exists(self, **kwargs):
# type: (**Any) -> bool
"""
Returns True if a blob exists with the defined parameters, and returns
False otherwise.

:param str version_id:
The version id parameter is an opaque DateTime
value that, when present, specifies the version of the blob to check if it exists.
:param int timeout:
The timeout parameter is expressed in seconds.
:returns: boolean
"""
try:
await self._client.blob.get_properties(
snapshot=self.snapshot,
**kwargs)
return True
except StorageErrorException as error:
try:
process_storage_error(error)
except ResourceNotFoundError:
return False

@distributed_trace_async
async def get_blob_properties(self, **kwargs):
# type: (Any) -> BlobProperties
Expand Down
Loading