Skip to content

Commit 9e0d12e

Browse files
committed
feat(*.rs): Implement set_tags trait/function
We need to be able to set tags for our blobs Signed-off-by: Denys Fedoryshchenko <denys.f@collabora.com>
1 parent 19456d4 commit 9e0d12e

2 files changed

Lines changed: 35 additions & 1 deletion

File tree

src/azure.rs

Lines changed: 34 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ impl AzureDriver {
1313
use crate::{Args, ReceivedFile};
1414
use axum::http::{HeaderName, HeaderValue};
1515
use azure_storage::StorageCredentials;
16-
use azure_storage_blobs::prelude::{BlobBlockType, BlockId, BlockList, ClientBuilder};
16+
use azure_storage_blobs::prelude::{BlobBlockType, BlockId, BlockList, ClientBuilder, Tags};
1717
use chksum_hash_sha2_512 as sha2_512;
1818
use clap::Parser;
1919
use headers::HeaderMap;
@@ -278,6 +278,34 @@ async fn get_file_from_blob(filename: String) -> ReceivedFile {
278278
return received_file;
279279
}
280280

281+
// Implement set tags for Azure blob storage
282+
// tags are in format "key=value"
283+
async fn azure_set_filename_tags(filename: String, user_tags: Vec<(String, String)>) -> Result<String, String> {
284+
let azure_cfg = Arc::new(get_azure_credentials("azure"));
285+
let storage_account = azure_cfg.account.as_str();
286+
let storage_key = azure_cfg.key.clone();
287+
let storage_container = azure_cfg.container.as_str();
288+
let storage_blob = filename.as_str();
289+
let storage_credential = StorageCredentials::access_key(storage_account, storage_key);
290+
let blob_client = ClientBuilder::new(storage_account, storage_credential)
291+
.blob_client(storage_container, storage_blob);
292+
let mut tags = Tags::new();
293+
// iterate and add tags, tags are in format "
294+
for tag in user_tags {
295+
let (tag, value) = tag;
296+
tags.insert(tag, value);
297+
}
298+
let res = blob_client.set_tags(tags).await;
299+
match res {
300+
Ok(_) => {
301+
return Ok(String::from("OK"));
302+
}
303+
Err(e) => {
304+
return Err(e.to_string());
305+
}
306+
}
307+
}
308+
281309
/// Implement Driver trait for AzureDriver
282310
impl super::Driver for AzureDriver {
283311
fn write_file(&self, filename: String, data: Vec<u8>, cont_type: String) -> String {
@@ -289,6 +317,11 @@ impl super::Driver for AzureDriver {
289317
});
290318
return filenameret;
291319
}
320+
fn tag_file(&self, filename: String, user_tags: Vec<(String, String)>) -> Result<String, String> {
321+
let rt = tokio::runtime::Runtime::new().unwrap();
322+
let ret = rt.block_on(azure_set_filename_tags(filename, user_tags));
323+
return ret;
324+
}
292325
fn get_file(&self, filename: String) -> ReceivedFile {
293326
/* Call async get_file_from_blob use tokio::task::block_in_place */
294327
let mut received_file = ReceivedFile {

src/main.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,7 @@ struct ReceivedFile {
6464
trait Driver {
6565
fn write_file(&self, filename: String, data: Vec<u8>, cont_type: String) -> String;
6666
fn get_file(&self, filename: String) -> ReceivedFile;
67+
fn tag_file(&self, filename: String, user_tags: Vec<(String, String)>) -> Result<String, String>;
6768
}
6869

6970
fn init_driver(driver_type: &str) -> Box<dyn Driver> {

0 commit comments

Comments
 (0)