|
| 1 | +// Copyright (c) Microsoft Corporation. All rights reserved. |
| 2 | +// Licensed under the MIT License. |
| 3 | + |
| 4 | +use crate::{ |
| 5 | + generated::clients::PageBlobClient as GeneratedPageBlobClient, |
| 6 | + models::{ |
| 7 | + PageBlobClientClearPagesOptions, PageBlobClientClearPagesResult, |
| 8 | + PageBlobClientCreateOptions, PageBlobClientCreateResult, PageBlobClientResizeOptions, |
| 9 | + PageBlobClientResizeResult, PageBlobClientUploadPagesOptions, |
| 10 | + PageBlobClientUploadPagesResult, |
| 11 | + }, |
| 12 | + pipeline::StorageHeadersPolicy, |
| 13 | + BlobClientOptions, PageBlobClientOptions, |
| 14 | +}; |
| 15 | +use azure_core::{ |
| 16 | + credentials::TokenCredential, |
| 17 | + http::{ |
| 18 | + policies::{BearerTokenCredentialPolicy, Policy}, |
| 19 | + NoFormat, RequestContent, Response, Url, |
| 20 | + }, |
| 21 | + Bytes, Result, |
| 22 | +}; |
| 23 | +use std::sync::Arc; |
| 24 | + |
| 25 | +/// A client to interact with a specific Azure storage Page blob, although that blob may not yet exist. |
| 26 | +pub struct PageBlobClient { |
| 27 | + pub(crate) endpoint: Url, |
| 28 | + pub(crate) client: GeneratedPageBlobClient, |
| 29 | +} |
| 30 | + |
| 31 | +impl PageBlobClient { |
| 32 | + /// Creates a new PageBlobClient, using Entra ID authentication. |
| 33 | + /// |
| 34 | + /// # Arguments |
| 35 | + /// |
| 36 | + /// * `endpoint` - The full URL of the Azure storage account, for example `https://myaccount.blob.core.windows.net/` |
| 37 | + /// * `container_name` - The name of the container containing this Page blob. |
| 38 | + /// * `blob_name` - The name of the Page blob to interact with. |
| 39 | + /// * `credential` - An implementation of [`TokenCredential`] that can provide an Entra ID token to use when authenticating. |
| 40 | + /// * `options` - Optional configuration for the client. |
| 41 | + pub fn new( |
| 42 | + endpoint: &str, |
| 43 | + container_name: String, |
| 44 | + blob_name: String, |
| 45 | + credential: Arc<dyn TokenCredential>, |
| 46 | + options: Option<PageBlobClientOptions>, |
| 47 | + ) -> Result<Self> { |
| 48 | + let mut options = options.unwrap_or_default(); |
| 49 | + |
| 50 | + let storage_headers_policy = Arc::new(StorageHeadersPolicy); |
| 51 | + options |
| 52 | + .client_options |
| 53 | + .per_call_policies |
| 54 | + .push(storage_headers_policy); |
| 55 | + |
| 56 | + let oauth_token_policy = BearerTokenCredentialPolicy::new( |
| 57 | + credential.clone(), |
| 58 | + ["https://storage.azure.com/.default"], |
| 59 | + ); |
| 60 | + options |
| 61 | + .client_options |
| 62 | + .per_try_policies |
| 63 | + .push(Arc::new(oauth_token_policy) as Arc<dyn Policy>); |
| 64 | + |
| 65 | + let client = GeneratedPageBlobClient::new( |
| 66 | + endpoint, |
| 67 | + credential.clone(), |
| 68 | + container_name.clone(), |
| 69 | + blob_name.clone(), |
| 70 | + Some(options), |
| 71 | + )?; |
| 72 | + Ok(Self { |
| 73 | + endpoint: endpoint.parse()?, |
| 74 | + client, |
| 75 | + }) |
| 76 | + } |
| 77 | + |
| 78 | + /// Gets the endpoint of the Storage account this client is connected to. |
| 79 | + pub fn endpoint(&self) -> &Url { |
| 80 | + &self.endpoint |
| 81 | + } |
| 82 | + |
| 83 | + /// Gets the container name of the Storage account this client is connected to. |
| 84 | + pub fn container_name(&self) -> &str { |
| 85 | + &self.client.container_name |
| 86 | + } |
| 87 | + |
| 88 | + /// Gets the blob name of the Storage account this client is connected to. |
| 89 | + pub fn blob_name(&self) -> &str { |
| 90 | + &self.client.blob_name |
| 91 | + } |
| 92 | + |
| 93 | + /// Creates a new Page blob. |
| 94 | + /// |
| 95 | + /// # Arguments |
| 96 | + /// |
| 97 | + /// * `content_length` - The maximum size for the Page blob, up to 1TB. The page blob size must |
| 98 | + /// be aligned to a 512-byte boundary. |
| 99 | + /// * `options` - Optional parameters for the request. See [`PageBlobClientCreateOptionsExt`](crate::models::PageBlobClientCreateOptionsExt) for additional usage helpers. |
| 100 | + pub async fn create( |
| 101 | + &self, |
| 102 | + content_length: u64, |
| 103 | + options: Option<PageBlobClientCreateOptions<'_>>, |
| 104 | + ) -> Result<Response<PageBlobClientCreateResult, NoFormat>> { |
| 105 | + self.client.create(content_length, options).await |
| 106 | + } |
| 107 | + |
| 108 | + /// Clears a range of pages. |
| 109 | + /// |
| 110 | + /// # Arguments |
| 111 | + /// |
| 112 | + /// * `range` - The range of bytes to clear. See [`format_page_range()`](crate::format_page_range) for help with the expected String format. |
| 113 | + /// * `options` - Optional parameters for the request. |
| 114 | + pub async fn clear_page( |
| 115 | + &self, |
| 116 | + range: String, |
| 117 | + options: Option<PageBlobClientClearPagesOptions<'_>>, |
| 118 | + ) -> Result<Response<PageBlobClientClearPagesResult, NoFormat>> { |
| 119 | + self.client.clear_pages(range, options).await |
| 120 | + } |
| 121 | + |
| 122 | + /// Resizes a Page blob to the specified size. If the specified value is less than |
| 123 | + /// the current size of the blob, then all pages above the specified value are cleared. |
| 124 | + /// |
| 125 | + /// # Arguments |
| 126 | + /// |
| 127 | + /// * `size` - Size used to resize the blob. Maximum size for a page Blob is up to 1TB. The |
| 128 | + /// Page blob size must be aligned to a 512-byte boundary. |
| 129 | + /// * `options` - Optional parameters for the request. |
| 130 | + pub async fn resize( |
| 131 | + &self, |
| 132 | + size: u64, |
| 133 | + options: Option<PageBlobClientResizeOptions<'_>>, |
| 134 | + ) -> Result<Response<PageBlobClientResizeResult, NoFormat>> { |
| 135 | + self.client.resize(size, options).await |
| 136 | + } |
| 137 | + |
| 138 | + /// The Upload Pages operation writes a range of pages to a Page blob. |
| 139 | + /// |
| 140 | + /// # Arguments |
| 141 | + /// |
| 142 | + /// * `data` - The contents of the page. |
| 143 | + /// * `content_length` - Number of bytes to use for writing to a section of the blob. The |
| 144 | + /// content_length specified must be a modulus of 512. |
| 145 | + /// * `range` - The range of the bytes to write. See [`format_page_range()`](crate::format_page_range) for help with the expected String format. |
| 146 | + /// * `options` - Optional parameters for the request. |
| 147 | + pub async fn upload_page( |
| 148 | + &self, |
| 149 | + data: RequestContent<Bytes>, |
| 150 | + content_length: u64, |
| 151 | + range: String, |
| 152 | + options: Option<PageBlobClientUploadPagesOptions<'_>>, |
| 153 | + ) -> Result<Response<PageBlobClientUploadPagesResult, NoFormat>> { |
| 154 | + self.client |
| 155 | + .upload_pages(data, content_length, range, options) |
| 156 | + .await |
| 157 | + } |
| 158 | +} |
0 commit comments