Skip to content
22 changes: 22 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 @@ -918,6 +918,28 @@ def undelete_blob(self, **kwargs):
except StorageErrorException as error:
process_storage_error(error)

@distributed_trace()
def exists(self, timeout=None, version_id=None):
# 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(
timeout=timeout,
version_id=version_id)
return True
except StorageErrorException:
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
@@ -0,0 +1,237 @@
interactions:
- request:
body: null
headers:
Accept:
- '*/*'
Accept-Encoding:
- gzip, deflate
Connection:
- keep-alive
Content-Length:
- '0'
User-Agent:
- azsdk-python-storage-blob/12.4.0 Python/3.8.5 (Windows-10-10.0.18362-SP0)
x-ms-date:
- Wed, 19 Aug 2020 20:52:58 GMT
x-ms-version:
- '2019-12-12'
method: PUT
uri: https://storagename.blob.core.windows.net/utcontainerccc51342?restype=container
response:
body:
string: ''
headers:
content-length:
- '0'
date:
- Wed, 19 Aug 2020 20:52:58 GMT
etag:
- '"0x8D84481DAA59725"'
last-modified:
- Wed, 19 Aug 2020 20:52:58 GMT
server:
- Windows-Azure-Blob/1.0 Microsoft-HTTPAPI/2.0
x-ms-version:
- '2019-12-12'
status:
code: 201
message: Created
- request:
body: hello world
headers:
Accept:
- '*/*'
Accept-Encoding:
- gzip, deflate
Connection:
- keep-alive
Content-Length:
- '11'
Content-Type:
- application/octet-stream
If-None-Match:
- '*'
User-Agent:
- azsdk-python-storage-blob/12.4.0 Python/3.8.5 (Windows-10-10.0.18362-SP0)
x-ms-blob-type:
- BlockBlob
x-ms-date:
- Wed, 19 Aug 2020 20:52:58 GMT
x-ms-version:
- '2019-12-12'
method: PUT
uri: https://storagename.blob.core.windows.net/utcontainerccc51342/blob1
response:
body:
string: ''
headers:
content-length:
- '0'
content-md5:
- XrY7u+Ae7tCTyyK7j1rNww==
date:
- Wed, 19 Aug 2020 20:52:58 GMT
etag:
- '"0x8D84481DABA7742"'
last-modified:
- Wed, 19 Aug 2020 20:52:58 GMT
server:
- Windows-Azure-Blob/1.0 Microsoft-HTTPAPI/2.0
x-ms-content-crc64:
- vo7q9sPVKY0=
x-ms-request-server-encrypted:
- 'true'
x-ms-version:
- '2019-12-12'
x-ms-version-id:
- '2020-08-19T20:52:58.4048450Z'
status:
code: 201
message: Created
- request:
body: null
headers:
Accept:
- '*/*'
Accept-Encoding:
- gzip, deflate
Connection:
- keep-alive
User-Agent:
- azsdk-python-storage-blob/12.4.0 Python/3.8.5 (Windows-10-10.0.18362-SP0)
x-ms-date:
- Wed, 19 Aug 2020 20:52:58 GMT
x-ms-version:
- '2019-12-12'
method: HEAD
uri: https://storagename.blob.core.windows.net/utcontainerccc51342/blob1
response:
body:
string: ''
headers:
accept-ranges:
- bytes
content-length:
- '11'
content-md5:
- XrY7u+Ae7tCTyyK7j1rNww==
content-type:
- application/octet-stream
date:
- Wed, 19 Aug 2020 20:52:58 GMT
etag:
- '"0x8D84481DABA7742"'
last-modified:
- Wed, 19 Aug 2020 20:52:58 GMT
server:
- Windows-Azure-Blob/1.0 Microsoft-HTTPAPI/2.0
x-ms-access-tier:
- Hot
x-ms-access-tier-inferred:
- 'true'
x-ms-blob-type:
- BlockBlob
x-ms-creation-time:
- Wed, 19 Aug 2020 20:52:58 GMT
x-ms-is-current-version:
- 'true'
x-ms-lease-state:
- available
x-ms-lease-status:
- unlocked
x-ms-server-encrypted:
- 'true'
x-ms-version:
- '2019-12-12'
x-ms-version-id:
- '2020-08-19T20:52:58.4048450Z'
status:
code: 200
message: OK
- request:
body: null
headers:
Accept:
- '*/*'
Accept-Encoding:
- gzip, deflate
Connection:
- keep-alive
User-Agent:
- azsdk-python-storage-blob/12.4.0 Python/3.8.5 (Windows-10-10.0.18362-SP0)
x-ms-date:
- Wed, 19 Aug 2020 20:52:58 GMT
x-ms-version:
- '2019-12-12'
method: HEAD
uri: https://storagename.blob.core.windows.net/utcontainerccc51342/blob1?versionid=2020-08-19T20%3A52%3A58.4048450Z
response:
body:
string: ''
headers:
accept-ranges:
- bytes
content-length:
- '11'
content-md5:
- XrY7u+Ae7tCTyyK7j1rNww==
content-type:
- application/octet-stream
date:
- Wed, 19 Aug 2020 20:52:58 GMT
etag:
- '"0x8D84481DABA7742"'
last-modified:
- Wed, 19 Aug 2020 20:52:58 GMT
server:
- Windows-Azure-Blob/1.0 Microsoft-HTTPAPI/2.0
x-ms-access-tier:
- Hot
x-ms-access-tier-inferred:
- 'true'
x-ms-blob-type:
- BlockBlob
x-ms-creation-time:
- Wed, 19 Aug 2020 20:52:58 GMT
x-ms-is-current-version:
- 'true'
x-ms-server-encrypted:
- 'true'
x-ms-version:
- '2019-12-12'
x-ms-version-id:
- '2020-08-19T20:52:58.4048450Z'
status:
code: 200
message: OK
- request:
body: null
headers:
Accept:
- '*/*'
Accept-Encoding:
- gzip, deflate
Connection:
- keep-alive
User-Agent:
- azsdk-python-storage-blob/12.4.0 Python/3.8.5 (Windows-10-10.0.18362-SP0)
x-ms-date:
- Wed, 19 Aug 2020 20:52:59 GMT
x-ms-version:
- '2019-12-12'
method: HEAD
uri: https://storagename.blob.core.windows.net/utcontainerccc51342/blob1?versionid=bad_version_id
response:
body:
string: ''
headers:
date:
- Wed, 19 Aug 2020 20:52:58 GMT
server:
- Windows-Azure-Blob/1.0 Microsoft-HTTPAPI/2.0
status:
code: 400
message: Value for one of the query parameters specified in the request URI
is invalid.
version: 1
Original file line number Diff line number Diff line change
Expand Up @@ -715,6 +715,20 @@ def test_get_blob_properties_with_if_modified(self, resource_group, location, st
self.assertEqual(properties.size, 11)
self.assertEqual(properties.lease.status, 'unlocked')

@GlobalStorageAccountPreparer()
def test_if_blob_exists(self, resource_group, location, storage_account, storage_account_key):
bsc = BlobServiceClient(self.account_url(storage_account, "blob"), storage_account_key, connection_data_block_size=4 * 1024)
self._setup()
self._create_container_and_block_blob(
self.container_name, 'blob1', b'hello world', bsc)
test_datetime = (datetime.utcnow() -
timedelta(minutes=15))
# Act
blob = bsc.get_blob_client(self.container_name, 'blob1')
blob_version_id = blob.get_blob_properties().get("version_id")
self.assertEqual(blob.exists(version_id=blob_version_id), True)
self.assertEqual(blob.exists(version_id="bad_version_id"), False)

@GlobalStorageAccountPreparer()
def test_get_blob_properties_with_if_modified_fail(self, resource_group, location, storage_account, storage_account_key):
bsc = BlobServiceClient(self.account_url(storage_account, "blob"), storage_account_key, connection_data_block_size=4 * 1024)
Expand Down