Skip to content

Commit 5ac7025

Browse files
[Storage] Navigation Methods Between Clients + Storage Test Helpers (#2502)
1 parent 98638b1 commit 5ac7025

File tree

8 files changed

+227
-437
lines changed

8 files changed

+227
-437
lines changed

sdk/storage/assets.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"AssetsRepo": "Azure/azure-sdk-assets",
33
"AssetsRepoPrefixPath": "rust",
4-
"Tag": "rust/azure_storage_blob_9f4842ba91",
4+
"Tag": "rust/azure_storage_blob_99380d9553",
55
"TagPrefix": "rust/azure_storage_blob"
66
}

sdk/storage/azure_storage_blob/src/clients/blob_client.rs

Lines changed: 11 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -104,8 +104,7 @@ impl BlobClient {
104104
&self,
105105
options: Option<BlobClientGetPropertiesOptions<'_>>,
106106
) -> Result<Response<BlobClientGetPropertiesResult>> {
107-
let response = self.client.get_properties(options).await?;
108-
Ok(response)
107+
self.client.get_properties(options).await
109108
}
110109

111110
/// Sets system properties on the blob.
@@ -117,8 +116,7 @@ impl BlobClient {
117116
&self,
118117
options: Option<BlobClientSetPropertiesOptions<'_>>,
119118
) -> Result<Response<()>> {
120-
let response = self.client.set_properties(options).await?;
121-
Ok(response)
119+
self.client.set_properties(options).await
122120
}
123121

124122
/// Downloads a blob from the service, including its metadata and properties.
@@ -128,8 +126,7 @@ impl BlobClient {
128126
&self,
129127
options: Option<BlobClientDownloadOptions<'_>>,
130128
) -> Result<Response<BlobClientDownloadResult>> {
131-
let response = self.client.download(options).await?;
132-
Ok(response)
129+
self.client.download(options).await
133130
}
134131

135132
/// Creates a new blob from a data source.
@@ -156,10 +153,9 @@ impl BlobClient {
156153

157154
let block_blob_client = self.client.get_block_blob_client();
158155

159-
let response = block_blob_client
156+
block_blob_client
160157
.upload(data, content_length, Some(options))
161-
.await?;
162-
Ok(response)
158+
.await
163159
}
164160

165161
/// Sets user-defined metadata for the specified blob as one or more name-value pairs. Each call to this operation
@@ -173,8 +169,7 @@ impl BlobClient {
173169
&self,
174170
options: Option<BlobClientSetMetadataOptions<'_>>,
175171
) -> Result<Response<()>> {
176-
let response = self.client.set_metadata(options).await?;
177-
Ok(response)
172+
self.client.set_metadata(options).await
178173
}
179174

180175
/// Deletes the blob.
@@ -186,8 +181,7 @@ impl BlobClient {
186181
&self,
187182
options: Option<BlobClientDeleteOptions<'_>>,
188183
) -> Result<Response<()>> {
189-
let response = self.client.delete(options).await?;
190-
Ok(response)
184+
self.client.delete(options).await
191185
}
192186

193187
/// Writes to a blob based on blocks specified by the list of IDs and content that make up the blob.
@@ -202,8 +196,7 @@ impl BlobClient {
202196
options: Option<BlockBlobClientCommitBlockListOptions<'_>>,
203197
) -> Result<Response<BlockBlobClientCommitBlockListResult>> {
204198
let block_blob_client = self.client.get_block_blob_client();
205-
let response = block_blob_client.commit_block_list(blocks, options).await?;
206-
Ok(response)
199+
block_blob_client.commit_block_list(blocks, options).await
207200
}
208201

209202
/// Creates a new block to be later committed as part of a blob.
@@ -223,10 +216,9 @@ impl BlobClient {
223216
options: Option<BlockBlobClientStageBlockOptions<'_>>,
224217
) -> Result<Response<BlockBlobClientStageBlockResult>> {
225218
let block_blob_client = self.client.get_block_blob_client();
226-
let response = block_blob_client
219+
block_blob_client
227220
.stage_block(block_id, content_length, body, options)
228-
.await?;
229-
Ok(response)
221+
.await
230222
}
231223

232224
/// Retrieves the list of blocks that have been uploaded as part of a block blob.
@@ -241,8 +233,7 @@ impl BlobClient {
241233
options: Option<BlockBlobClientGetBlockListOptions<'_>>,
242234
) -> Result<Response<BlockList>> {
243235
let block_blob_client = self.client.get_block_blob_client();
244-
let response = block_blob_client.get_block_list(list_type, options).await?;
245-
Ok(response)
236+
block_blob_client.get_block_list(list_type, options).await
246237
}
247238

248239
/// Sets the tier on a blob. Standard tiers are only applicable for Block blobs, while Premium tiers are only applicable

sdk/storage/azure_storage_blob/src/clients/blob_container_client.rs

Lines changed: 30 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,9 @@
44
use crate::{
55
generated::clients::BlobContainerClient as GeneratedBlobContainerClient,
66
generated::models::BlobContainerClientGetPropertiesResult, pipeline::StorageHeadersPolicy,
7-
BlobContainerClientCreateOptions, BlobContainerClientDeleteOptions,
8-
BlobContainerClientGetPropertiesOptions, BlobContainerClientOptions,
9-
BlobContainerClientSetMetadataOptions,
7+
BlobClient, BlobClientOptions, BlobContainerClientCreateOptions,
8+
BlobContainerClientDeleteOptions, BlobContainerClientGetPropertiesOptions,
9+
BlobContainerClientOptions, BlobContainerClientSetMetadataOptions,
1010
};
1111
use azure_core::{
1212
credentials::TokenCredential,
@@ -22,6 +22,7 @@ use std::sync::Arc;
2222
pub struct BlobContainerClient {
2323
endpoint: Url,
2424
container_name: String,
25+
credential: Arc<dyn TokenCredential>,
2526
client: GeneratedBlobContainerClient,
2627
}
2728

@@ -59,18 +60,39 @@ impl BlobContainerClient {
5960

6061
let client = GeneratedBlobContainerClient::new(
6162
endpoint,
62-
credential,
63+
credential.clone(),
6364
container_name.clone(),
6465
Some(options),
6566
)?;
6667

6768
Ok(Self {
6869
endpoint: endpoint.parse()?,
6970
container_name,
71+
credential,
7072
client,
7173
})
7274
}
7375

76+
/// Returns a new instance of BlobClient.
77+
///
78+
/// # Arguments
79+
///
80+
/// * `blob_name` - The name of the blob.
81+
/// * `options` - Optional configuration for the client.
82+
pub fn blob_client(
83+
&self,
84+
blob_name: String,
85+
options: Option<BlobClientOptions>,
86+
) -> Result<BlobClient> {
87+
BlobClient::new(
88+
self.endpoint().as_str(),
89+
self.container_name().to_string(),
90+
blob_name,
91+
self.credential.clone(),
92+
options,
93+
)
94+
}
95+
7496
/// Gets the endpoint of the Storage account this client is connected to.
7597
pub fn endpoint(&self) -> &Url {
7698
&self.endpoint
@@ -90,8 +112,7 @@ impl BlobContainerClient {
90112
&self,
91113
options: Option<BlobContainerClientCreateOptions<'_>>,
92114
) -> Result<Response<()>> {
93-
let response = self.client.create(options).await?;
94-
Ok(response)
115+
self.client.create(options).await
95116
}
96117

97118
/// Sets user-defined metadata for the specified container as one or more name-value pairs. Each call to this operation
@@ -105,8 +126,7 @@ impl BlobContainerClient {
105126
&self,
106127
options: Option<BlobContainerClientSetMetadataOptions<'_>>,
107128
) -> Result<Response<()>> {
108-
let response = self.client.set_metadata(options).await?;
109-
Ok(response)
129+
self.client.set_metadata(options).await
110130
}
111131

112132
/// Marks the specified container for deletion. The container and any blobs contained within are later deleted during garbage collection.
@@ -118,8 +138,7 @@ impl BlobContainerClient {
118138
&self,
119139
options: Option<BlobContainerClientDeleteOptions<'_>>,
120140
) -> Result<Response<()>> {
121-
let response = self.client.delete(options).await?;
122-
Ok(response)
141+
self.client.delete(options).await
123142
}
124143

125144
/// Returns all user-defined metadata and system properties for the specified container.
@@ -132,8 +151,6 @@ impl BlobContainerClient {
132151
&self,
133152
options: Option<BlobContainerClientGetPropertiesOptions<'_>>,
134153
) -> Result<Response<BlobContainerClientGetPropertiesResult>> {
135-
let response = self.client.get_properties(options).await?;
136-
137-
Ok(response)
154+
self.client.get_properties(options).await
138155
}
139156
}

sdk/storage/azure_storage_blob/src/clients/blob_service_client.rs

Lines changed: 25 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@
33

44
use crate::{
55
generated::clients::BlobServiceClient as GeneratedBlobServiceClient,
6-
models::StorageServiceProperties, pipeline::StorageHeadersPolicy,
7-
BlobServiceClientGetPropertiesOptions, BlobServiceClientOptions,
6+
models::StorageServiceProperties, pipeline::StorageHeadersPolicy, BlobContainerClient,
7+
BlobContainerClientOptions, BlobServiceClientGetPropertiesOptions, BlobServiceClientOptions,
88
};
99
use azure_core::{
1010
credentials::TokenCredential,
@@ -19,6 +19,7 @@ use std::sync::Arc;
1919
/// A client to interact with an Azure storage account.
2020
pub struct BlobServiceClient {
2121
endpoint: Url,
22+
credential: Arc<dyn TokenCredential>,
2223
client: GeneratedBlobServiceClient,
2324
}
2425

@@ -52,14 +53,34 @@ impl BlobServiceClient {
5253
.per_try_policies
5354
.push(Arc::new(oauth_token_policy) as Arc<dyn Policy>);
5455

55-
let client = GeneratedBlobServiceClient::new(endpoint, credential, Some(options))?;
56+
let client = GeneratedBlobServiceClient::new(endpoint, credential.clone(), Some(options))?;
5657

5758
Ok(Self {
5859
endpoint: endpoint.parse()?,
60+
credential,
5961
client,
6062
})
6163
}
6264

65+
/// Returns a new instance of BlobContainerClient.
66+
///
67+
/// # Arguments
68+
///
69+
/// * `container_name` - The name of the container.
70+
/// * `options` - Optional configuration for the client.
71+
pub fn blob_container_client(
72+
&self,
73+
container_name: String,
74+
options: Option<BlobContainerClientOptions>,
75+
) -> Result<BlobContainerClient> {
76+
BlobContainerClient::new(
77+
self.endpoint().as_str(),
78+
container_name,
79+
self.credential.clone(),
80+
options,
81+
)
82+
}
83+
6384
/// Gets the endpoint of the Storage account this client is connected to.
6485
pub fn endpoint(&self) -> &Url {
6586
&self.endpoint
@@ -74,7 +95,6 @@ impl BlobServiceClient {
7495
&self,
7596
options: Option<BlobServiceClientGetPropertiesOptions<'_>>,
7697
) -> Result<Response<StorageServiceProperties>> {
77-
let response = self.client.get_properties(options).await?;
78-
Ok(response)
98+
self.client.get_properties(options).await
7999
}
80100
}

0 commit comments

Comments
 (0)