Skip to content

Added blob exists method #13221

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

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 @@ -918,6 +919,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:
blob_props = self._client.blob.get_properties(
snapshot=self.snapshot,
cls=deserialize_blob_properties,
**kwargs)
if blob_props and blob_props.is_current_version or blob_props and self.snapshot:
return True
return False
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 Down Expand Up @@ -460,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:
blob_props = await self._client.blob.get_properties(
**kwargs,
snapshot=self.snapshot,
cls=deserialize_blob_properties)
if blob_props and blob_props.is_current_version or blob_props and self.snapshot:
return True
return False
except ResourceNotFoundError:
return False

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