Skip to main content

azure_storage_blob/clients/
page_blob_client.rs

1// Copyright (c) Microsoft Corporation. All rights reserved.
2// Licensed under the MIT License.
3
4pub use crate::generated::clients::{PageBlobClient, PageBlobClientOptions};
5
6use azure_core::{
7    credentials::TokenCredential,
8    http::{
9        policies::{auth::BearerTokenAuthorizationPolicy, Policy},
10        Pipeline, Url,
11    },
12    tracing, Result,
13};
14use std::sync::Arc;
15
16impl PageBlobClient {
17    /// Creates a new PageBlobClient from a blob URL.
18    ///
19    /// # Arguments
20    ///
21    /// * `blob_url` - The full URL of the Page blob, for example `https://myaccount.blob.core.windows.net/mycontainer/myblob`.
22    ///   The caller is responsible for percent-encoding the URL correctly; it will be used as-is.
23    /// * `credential` - An optional implementation of [`TokenCredential`] that can provide an Entra ID token to use when authenticating.
24    /// * `options` - Optional configuration for the client.
25    #[tracing::new("Storage.Blob.PageBlob")]
26    pub fn new(
27        blob_url: Url,
28        credential: Option<Arc<dyn TokenCredential>>,
29        options: Option<PageBlobClientOptions>,
30    ) -> Result<Self> {
31        // Storage endpoints must be base URLs.
32        if blob_url.cannot_be_a_base() {
33            return Err(azure_core::Error::with_message(
34                azure_core::error::ErrorKind::Other,
35                format!("{blob_url} is not a valid base URL"),
36            ));
37        }
38
39        let mut options = options.unwrap_or_default();
40        super::apply_client_defaults(&mut options.client_options);
41
42        let mut per_retry_policies: Vec<Arc<dyn Policy>> = Vec::default();
43        if let Some(token_credential) = credential {
44            if !blob_url.scheme().starts_with("https") {
45                return Err(azure_core::Error::with_message(
46                    azure_core::error::ErrorKind::Other,
47                    format!("{blob_url} must use https"),
48                ));
49            }
50            per_retry_policies.push(Arc::new(BearerTokenAuthorizationPolicy::new(
51                token_credential,
52                vec!["https://storage.azure.com/.default"],
53            )));
54        }
55
56        let pipeline = Pipeline::new(
57            option_env!("CARGO_PKG_NAME"),
58            option_env!("CARGO_PKG_VERSION"),
59            options.client_options.clone(),
60            Vec::default(),
61            per_retry_policies,
62            None,
63        );
64
65        Ok(Self {
66            endpoint: blob_url,
67            version: options.version,
68            pipeline,
69        })
70    }
71
72    /// Gets the URL of the blob.
73    pub fn url(&self) -> &Url {
74        &self.endpoint
75    }
76}