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

[Storage] get_blob_properties for BlobClient #2014

Open
wants to merge 19 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
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
6 changes: 3 additions & 3 deletions sdk/storage/azure_storage_blob/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,10 @@ async-trait.workspace = true
azure_storage_common.workspace = true
azure_core = { workspace = true, features = ["xml"] }
azure_identity = { workspace = true }
serde = { workspace = true }
time = { workspace = true }
serde.workspace = true
time.workspace = true
typespec_client_core = { workspace = true, features = ["derive"] }
uuid = { workspace = true }
uuid.workspace = true

[lints]
workspace = true
Expand Down
15 changes: 7 additions & 8 deletions sdk/storage/azure_storage_blob/src/clients/blob_client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,22 +2,22 @@
// Licensed under the MIT License.

use crate::{
models::BlobProperties, pipeline::StorageHeadersPolicy, BlobBlobClientGetPropertiesOptions,
BlobClientOptions, GeneratedBlobClient,
generated::clients::blob_client::BlobClient as GeneratedBlobClient, models::BlobProperties,
vincenttran-msft marked this conversation as resolved.
Show resolved Hide resolved
pipeline::StorageHeadersPolicy, BlobBlobClientGetPropertiesOptions, BlobClientOptions,
};
use azure_core::{credentials::TokenCredential, BearerTokenCredentialPolicy, Policy, Result};
use azure_core::{credentials::TokenCredential, BearerTokenCredentialPolicy, Policy, Result, Url};
use std::sync::Arc;

pub struct BlobClient {
pub endpoint: String,
pub endpoint: Url,
pub container_name: String,
pub blob_name: String,
client: GeneratedBlobClient,
}

impl BlobClient {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In other languages, I thought you can only get a BlobClient from a ContainerClient or something i.e., these aren't creatable. If so, they must be exported from both the crate root (re-exported) and from the clients module in which all clients - creatable or gettable - are exported.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

pub fn new(
endpoint: String,
endpoint: &str,
container_name: String,
blob_name: String,
credential: Arc<dyn TokenCredential>,
Expand All @@ -40,10 +40,9 @@ impl BlobClient {
.per_try_policies
.push(Arc::new(oauth_token_policy) as Arc<dyn Policy>);

let client = GeneratedBlobClient::new(&endpoint, credential, Some(options))?;

let client = GeneratedBlobClient::new(endpoint, credential, Some(options))?;
Ok(Self {
endpoint,
endpoint: endpoint.parse()?,
container_name,
blob_name,
client,
Expand Down
9 changes: 5 additions & 4 deletions sdk/storage/azure_storage_blob/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,16 @@
// Licensed under the MIT License. See License.txt in the project root for license information.
// Code generated by Microsoft (R) Rust Code Generator. DO NOT EDIT.

pub mod clients;
mod clients;
mod generated;
pub mod pipeline;

pub(crate) mod pipeline;

pub use clients::*;

pub mod models;

pub use crate::generated::clients::*;
pub use blob_client::BlobClient as GeneratedBlobClient;

pub use crate::blob_client::BlobClientOptions;
pub use crate::clients::BlobClient;
pub use crate::generated::clients::blob_blob_client::BlobBlobClientGetPropertiesOptions;
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ pub struct BlobProperties {
}

impl BlobProperties {
pub fn build_from_response_headers(response_headers: &Headers) -> BlobProperties {
pub(crate) fn build_from_response_headers(response_headers: &Headers) -> BlobProperties {
vincenttran-msft marked this conversation as resolved.
Show resolved Hide resolved
let mut properties = BlobProperties {
..Default::default()
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
// Licensed under the MIT License.

use async_trait::async_trait;
use azure_core::{headers::HeaderName, Context, Policy, PolicyResult, Request};
use azure_core::{headers::CLIENT_REQUEST_ID, Context, Policy, PolicyResult, Request};
use std::sync::Arc;
use uuid::Uuid;

Expand All @@ -18,16 +18,13 @@ impl Policy for StorageHeadersPolicy {
request: &mut Request,
next: &[Arc<dyn Policy>],
) -> PolicyResult {
let result = request
if request
.headers()
.get_str(&HeaderName::from("x-ms-client-request-id"));

match result {
Ok(_) => {}
Err(_) => {
let request_id = Uuid::new_v4().to_string();
request.insert_header("x-ms-client-request-id", request_id);
}
.get_optional_string(&CLIENT_REQUEST_ID)
.is_none()
{
let request_id = Uuid::new_v4().to_string();
request.insert_header(CLIENT_REQUEST_ID, &request_id);
}
next[0].send(ctx, request, &next[1..]).await
}
Expand Down
108 changes: 51 additions & 57 deletions sdk/storage/azure_storage_blob/tests/test_blob_client.rs
vincenttran-msft marked this conversation as resolved.
Show resolved Hide resolved
Original file line number Diff line number Diff line change
Expand Up @@ -6,62 +6,56 @@ use azure_identity::DefaultAzureCredentialBuilder;
use azure_storage_blob::{BlobBlobClientGetPropertiesOptions, BlobClient, BlobClientOptions};
use std::{env, error::Error};

#[cfg(test)]
mod tests {

use super::*;

#[recorded::test(live)]
async fn test_get_blob_properties() -> Result<(), Box<dyn Error>> {
// Setup
let storage_account_name = env::var("AZURE_STORAGE_ACCOUNT_NAME")
.expect("Failed to get environment variable: AZURE_STORAGE_ACCOUNT_NAME");
let endpoint = format!("https://{}.blob.core.windows.net/", storage_account_name);
let credential = DefaultAzureCredentialBuilder::default().build()?;

// Act
let blob_client = BlobClient::new(
endpoint,
String::from("testcontainer"),
String::from("test_blob.txt"),
credential,
Some(BlobClientOptions::default()),
)?;
let response = blob_client
.get_blob_properties(Some(BlobBlobClientGetPropertiesOptions::default()))
.await;

// Assert
assert!(response.is_ok());
Ok(())
}

#[recorded::test(live)]
async fn test_get_blob_properties_invalid_container() -> Result<(), Box<dyn Error>> {
// Setup
let storage_account_name = env::var("AZURE_STORAGE_ACCOUNT_NAME")
.expect("Failed to get environment variable: AZURE_STORAGE_ACCOUNT_NAME");
let endpoint = format!("https://{}.blob.core.windows.net/", storage_account_name);
let credential = DefaultAzureCredentialBuilder::default().build()?;

// Act
let blob_client = BlobClient::new(
endpoint,
String::from("missingcontainer"),
String::from("test_blob.txt"),
credential,
Some(BlobClientOptions::default()),
)?;
let response = blob_client
.get_blob_properties(Some(BlobBlobClientGetPropertiesOptions::default()))
.await;

// Assert
assert_eq!(
String::from("HttpResponse(NotFound, \"ContainerNotFound\")"),
response.unwrap_err().kind().to_string()
);
#[recorded::test(live)]
async fn test_get_blob_properties() -> Result<(), Box<dyn Error>> {
// Setup
let storage_account_name = env::var("AZURE_STORAGE_ACCOUNT_NAME")
.expect("Failed to get environment variable: AZURE_STORAGE_ACCOUNT_NAME");
let endpoint = format!("https://{}.blob.core.windows.net/", storage_account_name);
let credential = DefaultAzureCredentialBuilder::default().build()?;

// Act
let blob_client = BlobClient::new(
&endpoint,
String::from("testcontainer"),
String::from("test_blob.txt"),
credential,
Some(BlobClientOptions::default()),
)?;
let response = blob_client
.get_blob_properties(Some(BlobBlobClientGetPropertiesOptions::default()))
.await;

// Assert
assert!(response.is_ok());
Ok(())
}

Ok(())
}
#[recorded::test(live)]
async fn test_get_blob_properties_invalid_container() -> Result<(), Box<dyn Error>> {
// Setup
let storage_account_name = env::var("AZURE_STORAGE_ACCOUNT_NAME")
.expect("Failed to get environment variable: AZURE_STORAGE_ACCOUNT_NAME");
let endpoint = format!("https://{}.blob.core.windows.net/", storage_account_name);
let credential = DefaultAzureCredentialBuilder::default().build()?;

// Act
let blob_client = BlobClient::new(
&endpoint,
String::from("missingcontainer"),
String::from("test_blob.txt"),
credential,
Some(BlobClientOptions::default()),
)?;
let response = blob_client
.get_blob_properties(Some(BlobBlobClientGetPropertiesOptions::default()))
.await;

// Assert
assert_eq!(
String::from("HttpResponse(NotFound, \"ContainerNotFound\")"),
response.unwrap_err().kind().to_string()
);

Ok(())
}
Loading