Skip to content

Commit 719958f

Browse files
[Storage] Add set_metadata to BlobClient (#2477)
1 parent 0cc2b88 commit 719958f

File tree

8 files changed

+139
-101
lines changed

8 files changed

+139
-101
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_c02de844b6",
4+
"Tag": "rust/azure_storage_blob_0fb4b4b381",
55
"TagPrefix": "rust/azure_storage_blob"
66
}

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

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,9 @@ use crate::{
1111
models::{BlockList, BlockListType, BlockLookupList},
1212
pipeline::StorageHeadersPolicy,
1313
BlobClientDeleteOptions, BlobClientDownloadOptions, BlobClientGetPropertiesOptions,
14-
BlobClientOptions, BlockBlobClientCommitBlockListOptions, BlockBlobClientGetBlockListOptions,
15-
BlockBlobClientStageBlockOptions, BlockBlobClientUploadOptions,
14+
BlobClientOptions, BlobClientSetMetadataOptions, BlockBlobClientCommitBlockListOptions,
15+
BlockBlobClientGetBlockListOptions, BlockBlobClientStageBlockOptions,
16+
BlockBlobClientUploadOptions,
1617
};
1718
use azure_core::{
1819
credentials::TokenCredential,
@@ -149,6 +150,21 @@ impl BlobClient {
149150
Ok(response)
150151
}
151152

153+
/// Sets user-defined metadata for the specified blob as one or more name-value pairs. Each call to this operation
154+
/// replaces all existing metadata attached to the blob. To remove all metadata from the blob, call this operation with
155+
/// no metadata headers.
156+
///
157+
/// # Arguments
158+
///
159+
/// * `options` - Optional configuration for the request.
160+
pub async fn set_metadata(
161+
&self,
162+
options: Option<BlobClientSetMetadataOptions<'_>>,
163+
) -> Result<Response<()>> {
164+
let response = self.client.set_metadata(options).await?;
165+
Ok(response)
166+
}
167+
152168
/// Deletes the blob.
153169
///
154170
/// # Arguments

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

Lines changed: 5 additions & 5 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

sdk/storage/azure_storage_blob/src/generated/models/header_traits.rs

Lines changed: 24 additions & 82 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

sdk/storage/azure_storage_blob/src/generated/models/pub_models.rs

Lines changed: 0 additions & 4 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

sdk/storage/azure_storage_blob/src/lib.rs

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,11 @@ pub use crate::generated::clients::{
1717
};
1818
pub use crate::generated::models::{
1919
BlobClientDeleteOptions, BlobClientDownloadOptions, BlobClientGetPropertiesOptions,
20-
BlobContainerClientCreateOptions, BlobContainerClientDeleteOptions,
21-
BlobContainerClientGetPropertiesOptions, BlobServiceClientGetPropertiesOptions,
22-
BlockBlobClientCommitBlockListOptions, BlockBlobClientGetBlockListOptions,
23-
BlockBlobClientStageBlockOptions, BlockBlobClientUploadOptions,
20+
BlobClientSetMetadataOptions, BlobContainerClientCreateOptions,
21+
BlobContainerClientDeleteOptions, BlobContainerClientGetPropertiesOptions,
22+
BlobServiceClientGetPropertiesOptions, BlockBlobClientCommitBlockListOptions,
23+
BlockBlobClientGetBlockListOptions, BlockBlobClientStageBlockOptions,
24+
BlockBlobClientUploadOptions,
2425
};
2526

2627
pub mod models {

sdk/storage/azure_storage_blob/tests/blob_client.rs

Lines changed: 85 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,11 @@ use azure_storage_blob::{
1111
BlobClientDownloadResultHeaders, BlobClientGetPropertiesResultHeaders, BlockListType,
1212
BlockLookupList, LeaseState,
1313
},
14-
BlobClient, BlobClientOptions, BlobContainerClient, BlobContainerClientOptions,
14+
BlobClient, BlobClientOptions, BlobClientSetMetadataOptions, BlobContainerClient,
15+
BlobContainerClientOptions, BlockBlobClientUploadOptions,
1516
};
1617
use azure_storage_blob_test::recorded_test_setup;
17-
use std::error::Error;
18+
use std::{collections::HashMap, error::Error};
1819

1920
#[recorded::test]
2021
async fn test_get_blob_properties(ctx: TestContext) -> Result<(), Box<dyn Error>> {
@@ -311,6 +312,88 @@ async fn test_download_blob(ctx: TestContext) -> Result<(), Box<dyn Error>> {
311312
Ok(())
312313
}
313314

315+
#[recorded::test]
316+
async fn test_set_blob_metadata(ctx: TestContext) -> Result<(), Box<dyn Error>> {
317+
// Recording Setup
318+
319+
let recording = ctx.recording();
320+
let (options, endpoint) = recorded_test_setup(recording);
321+
let container_name = recording
322+
.random_string::<17>(Some("container"))
323+
.to_ascii_lowercase();
324+
let blob_name = recording
325+
.random_string::<12>(Some("blob"))
326+
.to_ascii_lowercase();
327+
328+
let container_client_options = BlobContainerClientOptions {
329+
client_options: options.clone(),
330+
..Default::default()
331+
};
332+
//Act
333+
let container_client = BlobContainerClient::new(
334+
&endpoint,
335+
container_name.clone(),
336+
recording.credential(),
337+
Some(container_client_options),
338+
)?;
339+
container_client.create_container(None).await?;
340+
341+
let blob_client_options = BlobClientOptions {
342+
client_options: options.clone(),
343+
..Default::default()
344+
};
345+
let blob_client = BlobClient::new(
346+
&endpoint,
347+
container_name,
348+
blob_name,
349+
recording.credential(),
350+
Some(blob_client_options),
351+
)?;
352+
353+
let data = b"hello rusty world";
354+
355+
// Upload Blob With Metadata
356+
let initial_metadata = HashMap::from([("initial".to_string(), "metadata".to_string())]);
357+
358+
let options_with_metadata = BlockBlobClientUploadOptions {
359+
metadata: Some(initial_metadata.clone()),
360+
..Default::default()
361+
};
362+
blob_client
363+
.upload(
364+
RequestContent::from(data.to_vec()),
365+
false,
366+
u64::try_from(data.len())?,
367+
Some(options_with_metadata),
368+
)
369+
.await?;
370+
// Assert
371+
let response = blob_client.get_properties(None).await?;
372+
let response_metadata = response.metadata()?;
373+
assert_eq!(initial_metadata, response_metadata);
374+
375+
// Set Metadata With Values
376+
let update_metadata = HashMap::from([("updated".to_string(), "values".to_string())]);
377+
let set_metadata_options = BlobClientSetMetadataOptions {
378+
metadata: Some(update_metadata.clone()),
379+
..Default::default()
380+
};
381+
blob_client.set_metadata(Some(set_metadata_options)).await?;
382+
// Assert
383+
let response = blob_client.get_properties(None).await?;
384+
let response_metadata = response.metadata()?;
385+
assert_eq!(update_metadata, response_metadata);
386+
387+
// Set Metadata No Values (Clear Metadata)
388+
blob_client.set_metadata(None).await?;
389+
// Assert
390+
let response = blob_client.get_properties(None).await?;
391+
let response_metadata = response.metadata()?;
392+
assert_eq!(HashMap::new(), response_metadata);
393+
394+
Ok(())
395+
}
396+
314397
#[recorded::test]
315398
async fn test_put_block_list(ctx: TestContext) -> Result<(), Box<dyn Error>> {
316399
// Recording Setup
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
directory: specification/storage/Microsoft.BlobStorage
2-
commit: 18f73ced716fd483afc074ac0960a4eb79547e02
2+
commit: ab781596b134018f770fa686893a6ead3be1b5fa
33
repo: Azure/azure-rest-api-specs
44
additionalDirectories:

0 commit comments

Comments
 (0)