@@ -5,17 +5,19 @@ use crate::{
55 clients:: GeneratedBlobClient ,
66 models:: {
77 BlobBlobClientDownloadOptions , BlobBlobClientGetPropertiesOptions ,
8- BlobBlockBlobClientUploadOptions , BlobProperties ,
8+ BlobBlockBlobClientCommitBlockListOptions , BlobBlockBlobClientStageBlockOptions ,
9+ BlobBlockBlobClientUploadOptions , BlobProperties , BlockLookupList ,
910 } ,
1011 pipeline:: StorageHeadersPolicy ,
1112 BlobClientOptions ,
1213} ;
1314use azure_core:: {
14- credentials:: TokenCredential , BearerTokenCredentialPolicy , Bytes , Policy , RequestContent ,
15- Response , Result , Url ,
15+ base64 , credentials:: TokenCredential , BearerTokenCredentialPolicy , Bytes , Policy ,
16+ RequestContent , Response , Result , Url ,
1617} ;
1718use std:: sync:: Arc ;
1819
20+ /// A client to interact with a specific Azure storage blob, although that blob may not yet exist.
1921pub struct BlobClient {
2022 endpoint : Url ,
2123 container_name : String ,
@@ -24,6 +26,15 @@ pub struct BlobClient {
2426}
2527
2628impl BlobClient {
29+ /// Creates a new BlobClient, using Entra ID authentication.
30+ ///
31+ /// # Arguments
32+ ///
33+ /// * `endpoint` - The full URL of the Azure storage account, for example `https://myaccount.blob.core.windows.net/`
34+ /// * `container_name` - The name of the container containing this blob.
35+ /// * `blob_name` - The name of the blob to interact with.
36+ /// * `credential` - An implementation of [`TokenCredential`] that can provide an Entra ID token to use when authenticating.
37+ /// * `options` - Optional configuration for the client.
2738 pub fn new (
2839 endpoint : & str ,
2940 container_name : String ,
@@ -57,18 +68,27 @@ impl BlobClient {
5768 } )
5869 }
5970
71+ /// Gets the endpoint of the Storage account this client is connected to.
6072 pub fn endpoint ( & self ) -> & Url {
6173 & self . endpoint
6274 }
6375
76+ /// Gets the container name of the Storage account this client is connected to.
6477 pub fn container_name ( & self ) -> & str {
6578 & self . container_name
6679 }
6780
81+ /// Gets the blob name of the Storage account this client is connected to.
6882 pub fn blob_name ( & self ) -> & str {
6983 & self . blob_name
7084 }
7185
86+ /// Returns all user-defined metadata, standard HTTP properties, and system properties for the blob.
87+ /// The data returned does not include the content of the blob.
88+ ///
89+ /// # Arguments
90+ ///
91+ /// * `options` - Optional configuration for the request.
7292 pub async fn get_blob_properties (
7393 & self ,
7494 options : Option < BlobBlobClientGetPropertiesOptions < ' _ > > ,
@@ -83,6 +103,11 @@ impl BlobClient {
83103 Ok ( blob_properties)
84104 }
85105
106+ /// Downloads a blob from the service, including its metadata and properties.
107+ ///
108+ /// # Arguments
109+ ///
110+ /// * `options` - Optional configuration for the request.
86111 pub async fn download_blob (
87112 & self ,
88113 options : Option < BlobBlobClientDownloadOptions < ' _ > > ,
@@ -95,7 +120,15 @@ impl BlobClient {
95120 Ok ( response)
96121 }
97122
98- // For now, this is single-shot, block blob hot path only.
123+ /// Creates a new blob from a data source.
124+ ///
125+ /// # Arguments
126+ ///
127+ /// * `data` - The blob data to upload.
128+ /// * `overwrite` - Whether the blob to be uploaded should overwrite the current data. If True, `upload_blob` will overwrite the existing data.
129+ /// If False, the operation will fail with ResourceExistsError.
130+ /// * `content_length` - Total length of the blob data to be uploaded.
131+ /// * `options` - Optional configuration for the request.
99132 pub async fn upload_blob (
100133 & self ,
101134 data : RequestContent < Bytes > ,
@@ -105,7 +138,6 @@ impl BlobClient {
105138 ) -> Result < Response < ( ) > > {
106139 let mut options = options. unwrap_or_default ( ) ;
107140
108- // Check if they want overwrite, by default overwrite=False
109141 if !overwrite {
110142 options. if_none_match = Some ( String :: from ( "*" ) ) ;
111143 }
@@ -117,4 +149,48 @@ impl BlobClient {
117149 . await ?;
118150 Ok ( response)
119151 }
152+
153+ /// Creates a new block to be later committed as part of a blob.
154+ ///
155+ /// # Arguments
156+ ///
157+ /// * `block_id` - The unique identifier for the block. The identifier should be less than or equal to 64 bytes in size.
158+ /// For a given blob, the `block_id` must be the same size for each block.
159+ /// * `content_length` - Total length of the blob data to be staged.
160+ /// * `data` - The content of the blob.
161+ /// * `options` - Optional configuration for the request.
162+ pub async fn stage_block (
163+ & self ,
164+ block_id : & str ,
165+ content_length : i64 ,
166+ data : RequestContent < Bytes > ,
167+ options : Option < BlobBlockBlobClientStageBlockOptions < ' _ > > ,
168+ ) -> Result < Response < ( ) > > {
169+ let block_id = base64:: encode ( block_id) ;
170+ let response = self
171+ . client
172+ . get_blob_block_blob_client ( self . container_name . clone ( ) , self . blob_name . clone ( ) )
173+ . stage_block ( & block_id, content_length, data, options)
174+ . await ?;
175+ Ok ( response)
176+ }
177+
178+ /// Writes to a blob based on blocks specified by the list of IDs and content that make up the blob.
179+ ///
180+ /// # Arguments
181+ ///
182+ /// * `blocks` - The list of Block Blobs to commit.
183+ /// * `options` - Optional configuration for the request.
184+ pub async fn commit_block_list (
185+ & self ,
186+ blocks : RequestContent < BlockLookupList > ,
187+ options : Option < BlobBlockBlobClientCommitBlockListOptions < ' _ > > ,
188+ ) -> Result < Response < ( ) > > {
189+ let response = self
190+ . client
191+ . get_blob_block_blob_client ( self . container_name . clone ( ) , self . blob_name . clone ( ) )
192+ . commit_block_list ( blocks, options)
193+ . await ?;
194+ Ok ( response)
195+ }
120196}
0 commit comments