Skip to content

Commit abb39d1

Browse files
[Storage] exists() for BlobClient and BlobContainerClient (#3010)
1 parent 6e9c44f commit abb39d1

File tree

8 files changed

+381
-8
lines changed

8 files changed

+381
-8
lines changed
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"AssetsRepo": "Azure/azure-sdk-assets",
33
"AssetsRepoPrefixPath": "rust",
4-
"Tag": "rust/azure_storage_blob_f9b39b45b4",
4+
"Tag": "rust/azure_storage_blob_fc6c153d44",
55
"TagPrefix": "rust/azure_storage_blob"
66
}

sdk/storage/azure_storage_blob/src/clients/blob_client.rs

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,16 +17,17 @@ use crate::{
1717
BlobClientReleaseLeaseOptions, BlobClientRenewLeaseOptions, BlobClientSetMetadataOptions,
1818
BlobClientSetPropertiesOptions, BlobClientSetTagsOptions, BlobClientSetTierOptions,
1919
BlobTags, BlockBlobClientCommitBlockListOptions, BlockBlobClientUploadOptions, BlockList,
20-
BlockListType, BlockLookupList,
20+
BlockListType, BlockLookupList, StorageErrorCode,
2121
},
2222
pipeline::StorageHeadersPolicy,
2323
AppendBlobClient, BlobClientOptions, BlockBlobClient, PageBlobClient,
2424
};
2525
use azure_core::{
2626
credentials::TokenCredential,
27+
error::ErrorKind,
2728
http::{
2829
policies::{BearerTokenCredentialPolicy, Policy},
29-
JsonFormat, NoFormat, RequestContent, Response, Url, XmlFormat,
30+
JsonFormat, NoFormat, RequestContent, Response, StatusCode, Url, XmlFormat,
3031
},
3132
Bytes, Result,
3233
};
@@ -355,4 +356,24 @@ impl BlobClient {
355356
) -> Result<Response<BlobClientGetAccountInfoResult, NoFormat>> {
356357
self.client.get_account_info(options).await
357358
}
359+
360+
/// Returns `true` if the blob exists, `false` if the blob does not exist, and propagates all other errors.
361+
pub async fn exists(&self) -> Result<bool> {
362+
match self.client.get_properties(None).await {
363+
Ok(_) => Ok(true),
364+
Err(e) if e.http_status() == Some(StatusCode::NotFound) => match e.kind() {
365+
ErrorKind::HttpResponse {
366+
error_code: Some(error_code),
367+
..
368+
} if error_code == StorageErrorCode::BlobNotFound.as_ref()
369+
|| error_code == StorageErrorCode::ContainerNotFound.as_ref() =>
370+
{
371+
Ok(false)
372+
}
373+
// Propagate all other error types.
374+
_ => Err(e),
375+
},
376+
Err(e) => Err(e),
377+
}
378+
}
358379
}

sdk/storage/azure_storage_blob/src/clients/blob_container_client.rs

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,16 +16,17 @@ use crate::{
1616
BlobContainerClientGetAccountInfoOptions, BlobContainerClientGetPropertiesOptions,
1717
BlobContainerClientListBlobFlatSegmentOptions, BlobContainerClientReleaseLeaseOptions,
1818
BlobContainerClientRenewLeaseOptions, BlobContainerClientSetMetadataOptions,
19-
FilterBlobSegment, ListBlobsFlatSegmentResponse,
19+
FilterBlobSegment, ListBlobsFlatSegmentResponse, StorageErrorCode,
2020
},
2121
pipeline::StorageHeadersPolicy,
2222
BlobClient, BlobContainerClientOptions,
2323
};
2424
use azure_core::{
2525
credentials::TokenCredential,
26+
error::ErrorKind,
2627
http::{
2728
policies::{BearerTokenCredentialPolicy, Policy},
28-
NoFormat, PageIterator, Pager, Response, Url, XmlFormat,
29+
NoFormat, PageIterator, Pager, Response, StatusCode, Url, XmlFormat,
2930
},
3031
Result,
3132
};
@@ -275,4 +276,20 @@ impl BlobContainerClient {
275276
) -> Result<Response<BlobContainerClientGetAccountInfoResult, NoFormat>> {
276277
self.client.get_account_info(options).await
277278
}
279+
280+
/// Returns `true` if the container exists, `false` if the container does not exist, and propagates all other errors.
281+
pub async fn exists(&self) -> Result<bool> {
282+
match self.client.get_properties(None).await {
283+
Ok(_) => Ok(true),
284+
Err(e) if e.http_status() == Some(StatusCode::NotFound) => match e.kind() {
285+
ErrorKind::HttpResponse {
286+
error_code: Some(error_code),
287+
..
288+
} if error_code == StorageErrorCode::ContainerNotFound.as_ref() => Ok(false),
289+
// Propagate all other error types.
290+
_ => Err(e),
291+
},
292+
Err(e) => Err(e),
293+
}
294+
}
278295
}

0 commit comments

Comments
 (0)