Skip to content

Commit

Permalink
use the StorageCredentials helpers instead of using the enum variants…
Browse files Browse the repository at this point in the history
… directly (#1420)
  • Loading branch information
demoray authored Sep 27, 2023
1 parent 5ca61da commit d8bf5ed
Show file tree
Hide file tree
Showing 63 changed files with 88 additions and 116 deletions.
2 changes: 1 addition & 1 deletion sdk/data_tables/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ async fn main() -> azure_core::Result<()> {
std::env::var("STORAGE_ACCESS_KEY").expect("Set env variable STORAGE_ACCESS_KEY first!");
let table_name = std::env::var("STORAGE_TABLE_NAME").expect("Set env variable STORAGE_TABLE_NAME first!");

let storage_credentials = StorageCredentials::Key(account.clone(), access_key);
let storage_credentials = StorageCredentials::access_key(account.clone(), access_key);
let table_service = TableServiceClient::new(account, storage_credentials);

let table_client = table_service.table_client(table_name);
Expand Down
2 changes: 1 addition & 1 deletion sdk/data_tables/examples/table.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ async fn main() -> azure_core::Result<()> {
.nth(1)
.expect("please specify the table name as first command line parameter");

let storage_credentials = StorageCredentials::Key(account.clone(), access_key);
let storage_credentials = StorageCredentials::access_key(account.clone(), access_key);
let table_service = TableServiceClient::new(account, storage_credentials);

let table_client = table_service.table_client(table_name);
Expand Down
2 changes: 1 addition & 1 deletion sdk/data_tables/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ async fn main() -> azure_core::Result<()> {
std::env::var("STORAGE_ACCESS_KEY").expect("Set env variable STORAGE_ACCESS_KEY first!");
let table_name = std::env::var("STORAGE_TABLE_NAME").expect("Set env variable STORAGE_TABLE_NAME first!");
let storage_credentials = StorageCredentials::Key(account.clone(), access_key);
let storage_credentials = StorageCredentials::access_key(account.clone(), access_key);
let table_service = TableServiceClient::new(account, storage_credentials);
let table_client = table_service.table_client(table_name);
Expand Down
33 changes: 3 additions & 30 deletions sdk/storage/src/connection_string.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
use azure_core::error::{Error, ErrorKind, ResultExt};

use crate::StorageCredentials;
use azure_core::error::{Error, ErrorKind};

// Key names.
pub const ACCOUNT_KEY_KEY_NAME: &str = "AccountKey";
Expand Down Expand Up @@ -188,15 +187,13 @@ impl<'a> ConnectionString<'a> {
if self.account_key.is_some() {
log::warn!("Both account key and SAS defined in connection string. Using only the provided SAS.");
}
Ok(StorageCredentials::SASToken(get_sas_token_parms(
sas_token,
)?))
StorageCredentials::sas_token(*sas_token)
}
ConnectionString {
account_name: Some(account),
account_key: Some(key),
..
} => Ok(StorageCredentials::Key((*account).to_string(), (*key).to_string())),
} => Ok(StorageCredentials::access_key(*account, *key)),
_ => {
Err(Error::message(ErrorKind::Credential,
"Could not create a `StorageCredentail` from the provided connection string. Please validate that you have specified a means of authentication (key, SAS, etc.)."
Expand All @@ -206,30 +203,6 @@ impl<'a> ConnectionString<'a> {
}
}

fn get_sas_token_parms(sas_token: &str) -> azure_core::Result<Vec<(String, String)>> {
// Any base url will do: we just need to parse the SAS token
// to get its query pairs.
let base_url = url::Url::parse("https://blob.core.windows.net").unwrap();

let url = url::Url::options().base_url(Some(&base_url));

// this code handles the leading ?
// we support both with or without
let url = if sas_token.starts_with('?') {
url.parse(sas_token)
} else {
url.parse(&format!("?{sas_token}"))
}
.with_context(ErrorKind::DataConversion, || {
format!("failed to parse SAS token: {sas_token}")
})?;

Ok(url
.query_pairs()
.map(|p| (String::from(p.0), String::from(p.1)))
.collect())
}

#[cfg(test)]
mod tests {
#[allow(unused_imports)]
Expand Down
2 changes: 1 addition & 1 deletion sdk/storage_blobs/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ async fn main() -> azure_core::Result<()> {
let container = std::env::var("STORAGE_CONTAINER").expect("missing STORAGE_CONTAINER");
let blob_name = std::env::var("STORAGE_BLOB_NAME").expect("missing STORAGE_BLOB_NAME");

let storage_credentials = StorageCredentials::Key(account.clone(), access_key);
let storage_credentials = StorageCredentials::access_key(account.clone(), access_key);
let blob_client = ClientBuilder::new(account, storage_credentials).blob_client(&container, blob_name);

blob_client.put_block_blob("hello world").content_type("text/plain").await?;
Expand Down
2 changes: 1 addition & 1 deletion sdk/storage_blobs/examples/account.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ async fn main() -> azure_core::Result<()> {
let access_key =
std::env::var("STORAGE_ACCESS_KEY").expect("Set env variable STORAGE_ACCESS_KEY first!");

let storage_credentials = StorageCredentials::Key(account.clone(), access_key);
let storage_credentials = StorageCredentials::access_key(account.clone(), access_key);
let service_client = BlobServiceClient::new(account, storage_credentials);

let account = service_client.get_account_information().await?;
Expand Down
2 changes: 1 addition & 1 deletion sdk/storage_blobs/examples/anonymous_access.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ async fn main() -> azure_core::Result<()> {
let account = std::env::var("STORAGE_ACCOUNT").expect("Set env variable STORAGE_ACCOUNT");
let container = std::env::var("STORAGE_CONTAINER").expect("Set env variable STORAGE_CONTAINER");

let storage_credentials = StorageCredentials::Anonymous;
let storage_credentials = StorageCredentials::anonymous();
let container_client =
BlobServiceClient::new(account, storage_credentials).container_client(container);

Expand Down
2 changes: 1 addition & 1 deletion sdk/storage_blobs/examples/blob_00.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ async fn main() -> azure_core::Result<()> {
.nth(2)
.expect("please specify blob name as command line parameter");

let storage_credentials = StorageCredentials::Key(account.clone(), access_key);
let storage_credentials = StorageCredentials::access_key(account.clone(), access_key);
let service_client = BlobServiceClient::new(account, storage_credentials);

// this is how you would use the SAS token:
Expand Down
2 changes: 1 addition & 1 deletion sdk/storage_blobs/examples/blob_01.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ async fn main() -> azure_core::Result<()> {
.nth(1)
.expect("please specify container name as command line parameter");

let storage_credentials = StorageCredentials::Key(account.clone(), access_key);
let storage_credentials = StorageCredentials::access_key(account.clone(), access_key);
let service_client = BlobServiceClient::new(account, storage_credentials);
let container_client = service_client.container_client(container_name);
let blob_client = container_client.blob_client("SorgeniaReorganizeRebuildIndexes.zip");
Expand Down
2 changes: 1 addition & 1 deletion sdk/storage_blobs/examples/blob_02_bearer_token.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ async fn main() -> azure_core::Result<()> {
.nth(4)
.expect("please specify the bearer token as fourth command line parameter");

let storage_credentials = StorageCredentials::BearerToken(bearer_token);
let storage_credentials = StorageCredentials::bearer_token(bearer_token);
let blob_client = BlobServiceClient::new(account, storage_credentials)
.container_client(&container)
.blob_client(&blob);
Expand Down
2 changes: 1 addition & 1 deletion sdk/storage_blobs/examples/blob_04.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ async fn main() -> azure_core::Result<()> {
.nth(1)
.expect("please specify container name as command line parameter");

let storage_credentials = StorageCredentials::Key(account.clone(), access_key);
let storage_credentials = StorageCredentials::access_key(account.clone(), access_key);
let blob_client = BlobServiceClient::new(account, storage_credentials)
.container_client(container_name)
.blob_client("test1");
Expand Down
15 changes: 7 additions & 8 deletions sdk/storage_blobs/examples/blob_05_default_credential.rs
Original file line number Diff line number Diff line change
@@ -1,14 +1,12 @@
#[macro_use]
extern crate log;

use std::sync::Arc;

use azure_core::{
auth::TokenCredential,
error::{ErrorKind, ResultExt},
};
use azure_identity::DefaultAzureCredential;
use azure_storage_blobs::prelude::*;
use azure_storage::StorageCredentials;
use azure_storage_blobs::prelude::BlobServiceClient;
use log::trace;
use std::sync::Arc;

#[tokio::main]
async fn main() -> azure_core::Result<()> {
Expand All @@ -25,8 +23,9 @@ async fn main() -> azure_core::Result<()> {
.nth(3)
.expect("please specify the blob name as third command line parameter");

let storage_credentials: Arc<dyn TokenCredential> = Arc::new(DefaultAzureCredential::default());
let blob_client = BlobServiceClient::new(account, storage_credentials)
let default_creds: Arc<dyn TokenCredential> = Arc::new(DefaultAzureCredential::default());
let credentials = StorageCredentials::token_credential(default_creds);
let blob_client = BlobServiceClient::new(account, credentials)
.container_client(&container)
.blob_client(&blob);

Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
#[macro_use]
extern crate log;

use azure_core::error::{ErrorKind, ResultExt};
use azure_core::{
auth::TokenCredential,
error::{ErrorKind, ResultExt},
};
use azure_identity::{AutoRefreshingTokenCredential, DefaultAzureCredential};
use azure_storage::prelude::*;
use azure_storage_blobs::prelude::*;
use azure_storage::prelude::StorageCredentials;
use azure_storage_blobs::prelude::BlobServiceClient;
use log::trace;
use std::sync::Arc;

#[tokio::main]
Expand All @@ -23,9 +24,9 @@ async fn main() -> azure_core::Result<()> {
.expect("please specify the blob name as third command line parameter");

let creds = Arc::new(DefaultAzureCredential::default());
let auto_creds = Arc::new(AutoRefreshingTokenCredential::new(creds));
let auto_creds: Arc<dyn TokenCredential> = Arc::new(AutoRefreshingTokenCredential::new(creds));

let storage_credentials = StorageCredentials::TokenCredential(auto_creds);
let storage_credentials = StorageCredentials::token_credential(auto_creds);
let blob_client = BlobServiceClient::new(account, storage_credentials)
.container_client(&container)
.blob_client(&blob);
Expand Down
2 changes: 1 addition & 1 deletion sdk/storage_blobs/examples/blob_range.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ async fn main() -> azure_core::Result<()> {
let container_name = format!("range-example-{}", Uuid::new_v4());
let blob_name = format!("blob-{}.txt", Uuid::new_v4());

let storage_credentials = StorageCredentials::Key(account.clone(), access_key);
let storage_credentials = StorageCredentials::access_key(account.clone(), access_key);
let container_client =
BlobServiceClient::new(account, storage_credentials).container_client(container_name);
container_client.create().await?;
Expand Down
2 changes: 1 addition & 1 deletion sdk/storage_blobs/examples/blob_tags.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ async fn main() -> azure_core::Result<()> {
let blob_name = format!("file-{}.txt", Uuid::new_v4());
let blob_notags_name = format!("file-{}.txt", Uuid::new_v4());

let storage_credentials = StorageCredentials::Key(account.clone(), access_key);
let storage_credentials = StorageCredentials::access_key(account.clone(), access_key);
let container_client =
BlobServiceClient::new(account, storage_credentials).container_client(container_name);
container_client.create().await?;
Expand Down
2 changes: 1 addition & 1 deletion sdk/storage_blobs/examples/container_00.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ async fn main() -> azure_core::Result<()> {
.nth(1)
.expect("please specify container name as command line parameter");

let storage_credentials = StorageCredentials::Key(account.clone(), access_key);
let storage_credentials = StorageCredentials::access_key(account.clone(), access_key);
let service_client = BlobServiceClient::new(account, storage_credentials);
let container_client = service_client.container_client(container_name);

Expand Down
2 changes: 1 addition & 1 deletion sdk/storage_blobs/examples/container_01.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ async fn main() -> azure_core::Result<()> {
.nth(1)
.expect("please specify container name as command line parameter");

let storage_credentials = StorageCredentials::Key(account.clone(), access_key);
let storage_credentials = StorageCredentials::access_key(account.clone(), access_key);
let container_client =
ClientBuilder::new(account, storage_credentials).container_client(container_name);

Expand Down
2 changes: 1 addition & 1 deletion sdk/storage_blobs/examples/container_and_blob.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ async fn main() -> azure_core::Result<()> {
.nth(1)
.expect("please specify container name as command line parameter");

let storage_credentials = StorageCredentials::Key(account.clone(), access_key);
let storage_credentials = StorageCredentials::access_key(account.clone(), access_key);
let container_client =
BlobServiceClient::new(account, storage_credentials).container_client(container_name);

Expand Down
4 changes: 2 additions & 2 deletions sdk/storage_blobs/examples/copy_blob.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,15 +30,15 @@ async fn main() -> azure_core::Result<()> {
.expect("please specify destination blob name as fourth command line parameter");

let destination_storage_credentials =
StorageCredentials::Key(destination_account.clone(), destination_access_key);
StorageCredentials::access_key(destination_account.clone(), destination_access_key);
let destination_service_client =
BlobServiceClient::new(destination_account, destination_storage_credentials);
let destination_blob = destination_service_client
.container_client(&destination_container_name)
.blob_client(&destination_blob_name);

let source_storage_credentials =
StorageCredentials::Key(source_account.clone(), source_access_key);
StorageCredentials::access_key(source_account.clone(), source_access_key);
let source_service_client = BlobServiceClient::new(source_account, source_storage_credentials);
let source_blob = source_service_client
.container_client(&source_container_name)
Expand Down
2 changes: 1 addition & 1 deletion sdk/storage_blobs/examples/copy_blob_from_url.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ async fn main() -> azure_core::Result<()> {
.nth(4)
.expect("please specify destination blob name as fourth command line parameter");

let storage_credentials = StorageCredentials::Key(account.clone(), access_key);
let storage_credentials = StorageCredentials::access_key(account.clone(), access_key);
let blob_service = BlobServiceClient::new(account, storage_credentials);
let blob_client = blob_service
.container_client(&destination_container)
Expand Down
2 changes: 1 addition & 1 deletion sdk/storage_blobs/examples/count_blobs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ async fn main() -> azure_core::Result<()> {
.nth(1)
.expect("please specify container name as command line parameter");

let storage_credentials = StorageCredentials::Key(account.clone(), access_key);
let storage_credentials = StorageCredentials::access_key(account.clone(), access_key);
let container_client =
BlobServiceClient::new(account, storage_credentials).container_client(&container);

Expand Down
2 changes: 1 addition & 1 deletion sdk/storage_blobs/examples/list_blobs_00.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ async fn main() -> azure_core::Result<()> {
.nth(1)
.expect("please specify container name as command line parameter");

let storage_credentials = StorageCredentials::Key(account.clone(), access_key);
let storage_credentials = StorageCredentials::access_key(account.clone(), access_key);
let blob_service = BlobServiceClient::new(account, storage_credentials);
let container_client = blob_service.container_client(&container_name);

Expand Down
2 changes: 1 addition & 1 deletion sdk/storage_blobs/examples/list_blobs_01.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ async fn main() -> azure_core::Result<()> {
.nth(1)
.expect("please specify container name as command line parameter");

let storage_credentials = StorageCredentials::Key(account.clone(), access_key);
let storage_credentials = StorageCredentials::access_key(account.clone(), access_key);
let blob_service = BlobServiceClient::new(account, storage_credentials);
let container_client = blob_service.container_client(&container_name);

Expand Down
2 changes: 1 addition & 1 deletion sdk/storage_blobs/examples/list_blobs_02.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ async fn main() -> azure_core::Result<()> {
.nth(1)
.expect("please specify a non-existing container name as command line parameter");

let storage_credentials = StorageCredentials::Key(account.clone(), access_key);
let storage_credentials = StorageCredentials::access_key(account.clone(), access_key);
let blob_service = BlobServiceClient::new(account, storage_credentials);
let container_client = blob_service.container_client(&container_name);

Expand Down
2 changes: 1 addition & 1 deletion sdk/storage_blobs/examples/list_containers2.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ async fn main() -> azure_core::Result<()> {
let access_key =
std::env::var("STORAGE_ACCESS_KEY").expect("Set env variable STORAGE_ACCESS_KEY first!");

let storage_credentials = StorageCredentials::Key(account.clone(), access_key);
let storage_credentials = StorageCredentials::access_key(account.clone(), access_key);
let service_client = BlobServiceClient::new(account, storage_credentials);

let response = service_client
Expand Down
2 changes: 1 addition & 1 deletion sdk/storage_blobs/examples/list_containers_and_blobs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ async fn main() -> azure_core::Result<()> {
let access_key =
std::env::var("STORAGE_ACCESS_KEY").expect("Set env variable STORAGE_ACCESS_KEY first!");

let storage_credentials = StorageCredentials::Key(account.clone(), access_key);
let storage_credentials = StorageCredentials::access_key(account.clone(), access_key);
let service_client = BlobServiceClient::new(account, storage_credentials);

let mut stream = service_client.list_containers().into_stream();
Expand Down
2 changes: 1 addition & 1 deletion sdk/storage_blobs/examples/missing_blob.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ async fn main() -> azure_core::Result<()> {
let container_name = format!("example-{}", Uuid::new_v4());
let blob_name = format!("missing-{}.txt", Uuid::new_v4());

let storage_credentials = StorageCredentials::Key(account.clone(), access_key);
let storage_credentials = StorageCredentials::access_key(account.clone(), access_key);
let container_client =
ClientBuilder::new(account, storage_credentials).container_client(&container_name);
println!("creating container {container_name}");
Expand Down
2 changes: 1 addition & 1 deletion sdk/storage_blobs/examples/put_append_blob_00.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ async fn main() -> azure_core::Result<()> {
.nth(2)
.expect("please specify blob name as command line parameter");

let storage_credentials = StorageCredentials::Key(account.clone(), access_key);
let storage_credentials = StorageCredentials::access_key(account.clone(), access_key);
let blob_client =
ClientBuilder::new(account, storage_credentials).blob_client(&container, &blob_name);

Expand Down
2 changes: 1 addition & 1 deletion sdk/storage_blobs/examples/put_block_blob_00.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ async fn main() -> azure_core::Result<()> {
.nth(2)
.expect("please specify blob name as command line parameter");

let storage_credentials = StorageCredentials::Key(account.clone(), access_key);
let storage_credentials = StorageCredentials::access_key(account.clone(), access_key);
let blob_client = BlobServiceClient::new(account, storage_credentials)
.container_client(&container)
.blob_client(&blob_name);
Expand Down
2 changes: 1 addition & 1 deletion sdk/storage_blobs/examples/put_multi_block_blob.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ async fn main() -> azure_core::Result<()> {
.nth(2)
.expect("please specify blob name as command line parameter");

let storage_credentials = StorageCredentials::Key(account.clone(), access_key);
let storage_credentials = StorageCredentials::access_key(account.clone(), access_key);
let blob_client = BlobServiceClient::new(account, storage_credentials)
.container_client(&container)
.blob_client(&blob_name);
Expand Down
2 changes: 1 addition & 1 deletion sdk/storage_blobs/examples/put_page_blob_00.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ async fn main() -> azure_core::Result<()> {
.nth(2)
.expect("please specify blob name as command line parameter");

let storage_credentials = StorageCredentials::Key(account.clone(), access_key);
let storage_credentials = StorageCredentials::access_key(account.clone(), access_key);
let blob_client = BlobServiceClient::new(account, storage_credentials)
.container_client(container_name)
.blob_client(&blob_name);
Expand Down
2 changes: 1 addition & 1 deletion sdk/storage_blobs/examples/request_hdr_override.rs
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ async fn main() -> azure_core::Result<()> {
.nth(2)
.expect("please specify blob name as command line parameter");

let storage_credentials = StorageCredentials::Key(account.clone(), access_key);
let storage_credentials = StorageCredentials::access_key(account.clone(), access_key);

let version_header_override_policy =
Arc::new(VersionHeaderOverridePolicy::new()) as Arc<dyn Policy>;
Expand Down
2 changes: 1 addition & 1 deletion sdk/storage_blobs/examples/set_blob_properties_00.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ async fn main() -> azure_core::Result<()> {
// let storage_client = StorageAccountClient::new_sas_token(http_client.clone(), &account,
// "sv=2018-11-09&ss=b&srt=o&se=2021-01-15T12%3A09%3A01Z&sp=r&st=2021-01-15T11%3A09%3A01Z&spr=http,https&sig=some_signature")?;

let storage_credentials = StorageCredentials::Key(account.clone(), access_key);
let storage_credentials = StorageCredentials::access_key(account.clone(), access_key);
let blob_client = BlobServiceClient::new(account, storage_credentials)
.container_client(&container)
.blob_client(blob);
Expand Down
Loading

0 comments on commit d8bf5ed

Please sign in to comment.