Skip to content
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion sdk/storage/azure_storage_blob/assets.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"AssetsRepo": "Azure/azure-sdk-assets",
"AssetsRepoPrefixPath": "rust",
"Tag": "rust/azure_storage_blob_f9b39b45b4",
"Tag": "rust/azure_storage_blob_fc6c153d44",
"TagPrefix": "rust/azure_storage_blob"
}
18 changes: 17 additions & 1 deletion sdk/storage/azure_storage_blob/src/clients/blob_client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,10 @@ use crate::{
};
use azure_core::{
credentials::TokenCredential,
error::ErrorKind,
http::{
policies::{BearerTokenCredentialPolicy, Policy},
JsonFormat, NoFormat, RequestContent, Response, Url, XmlFormat,
JsonFormat, NoFormat, RequestContent, Response, StatusCode, Url, XmlFormat,
},
Bytes, Result,
};
Expand Down Expand Up @@ -355,4 +356,19 @@ impl BlobClient {
) -> Result<Response<BlobClientGetAccountInfoResult, NoFormat>> {
self.client.get_account_info(options).await
}

/// Returns `true` if the blob exists, `false` if the blob does not exist, and propagates all other errors.
pub async fn exists(&self) -> Result<bool> {
match self.client.get_properties(None).await {
Ok(_) => Ok(true),
Err(e) if e.http_status() == Some(StatusCode::NotFound) => match e.kind() {
ErrorKind::HttpResponse {
error_code: Some(error_code),
..
} if error_code == "BlobNotFound" || error_code == "ContainerNotFound" => Ok(false),
_ => Ok(false),
Comment thread
vincenttran-msft marked this conversation as resolved.
Outdated
},
Err(e) => Err(e),
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,10 @@ use crate::{
};
use azure_core::{
credentials::TokenCredential,
error::ErrorKind,
http::{
policies::{BearerTokenCredentialPolicy, Policy},
NoFormat, PageIterator, Pager, Response, Url, XmlFormat,
NoFormat, PageIterator, Pager, Response, StatusCode, Url, XmlFormat,
},
Result,
};
Expand Down Expand Up @@ -275,4 +276,19 @@ impl BlobContainerClient {
) -> Result<Response<BlobContainerClientGetAccountInfoResult, NoFormat>> {
self.client.get_account_info(options).await
}

/// Returns `true` if the container exists, `false` if the container does not exist, and propagates all other errors.
pub async fn exists(&self) -> Result<bool> {
match self.client.get_properties(None).await {
Ok(_) => Ok(true),
Err(e) if e.http_status() == Some(StatusCode::NotFound) => match e.kind() {
ErrorKind::HttpResponse {
error_code: Some(error_code),
..
} if error_code == "ContainerNotFound" => Ok(false),
_ => Ok(false),
},
Err(e) => Err(e),
}
}
}
5 changes: 4 additions & 1 deletion sdk/storage/azure_storage_blob/tests/blob_client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,14 +26,16 @@ async fn test_get_blob_properties(ctx: TestContext) -> Result<(), Box<dyn Error>
let container_client = get_container_client(recording, false).await?;
let blob_client = container_client.blob_client(get_blob_name(recording));

// Invalid Container Scenario
// Container Doesn't Exist Scenario
let response = blob_client.get_properties(None).await;

// Assert
let error = response.unwrap_err().http_status();
assert_eq!(StatusCode::NotFound, error.unwrap());
assert!(!blob_client.exists().await?);

container_client.create_container(None).await?;
assert!(!blob_client.exists().await?);
create_test_blob(&blob_client, None, None).await?;

// No Option Scenario
Expand All @@ -49,6 +51,7 @@ async fn test_get_blob_properties(ctx: TestContext) -> Result<(), Box<dyn Error>
assert_eq!(17, content_length.unwrap());
assert!(etag.is_some());
assert!(creation_time.is_some());
assert!(blob_client.exists().await?);

container_client.delete_container(None).await?;
Ok(())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ async fn test_get_container_properties(ctx: TestContext) -> Result<(), Box<dyn E
assert!(response.is_err());
let error = response.unwrap_err().http_status();
assert_eq!(StatusCode::NotFound, error.unwrap());
assert!(!container_client.exists().await?);

// Container Exists Scenario
container_client.create_container(None).await?;
Expand All @@ -53,6 +54,7 @@ async fn test_get_container_properties(ctx: TestContext) -> Result<(), Box<dyn E
// Assert
assert_eq!(LeaseState::Available, lease_state.unwrap());
assert!(!has_immutability_policy.unwrap());
assert!(container_client.exists().await?);

container_client.delete_container(None).await?;
Ok(())
Expand Down