72 lines
2.1 KiB
Rust
72 lines
2.1 KiB
Rust
use crate::error::AdapterError;
|
|
use async_trait::async_trait;
|
|
use azure_storage::prelude::*;
|
|
use azure_storage_blobs::prelude::*;
|
|
|
|
use super::ObjectStorageClient;
|
|
|
|
pub struct AzureBlobClient {
|
|
client: BlobServiceClient,
|
|
}
|
|
|
|
impl AzureBlobClient {
|
|
pub fn new(account_name: &str, account_key: String) -> Self {
|
|
let storage_credentials = StorageCredentials::access_key(account_name, account_key);
|
|
let client = BlobServiceClient::new(account_name, storage_credentials);
|
|
|
|
Self { client }
|
|
}
|
|
|
|
// Helper method to get container client
|
|
fn get_container_client(&self, container_name: &str) -> ContainerClient {
|
|
self.client.container_client(container_name)
|
|
}
|
|
|
|
// Helper method to get blob client
|
|
fn get_blob_client(&self, container_name: &str, blob_name: &str) -> BlobClient {
|
|
self.get_container_client(container_name)
|
|
.blob_client(blob_name)
|
|
}
|
|
}
|
|
|
|
#[async_trait]
|
|
impl ObjectStorageClient for AzureBlobClient {
|
|
type Error = AdapterError;
|
|
|
|
async fn put_object(
|
|
&self,
|
|
bucket: &str, // container name
|
|
key: &str, // blob name
|
|
data: Vec<u8>,
|
|
) -> Result<(), Self::Error> {
|
|
let blob_client = self.get_blob_client(bucket, key);
|
|
let _request = blob_client.put_block_blob(data).await.unwrap();
|
|
Ok(())
|
|
}
|
|
|
|
async fn get_object(&self, bucket: &str, key: &str) -> Result<Vec<u8>, Self::Error> {
|
|
let blob_client = self.get_blob_client(bucket, key);
|
|
|
|
let response = blob_client.get_content().await.unwrap();
|
|
Ok(response)
|
|
}
|
|
|
|
async fn delete_object(&self, bucket: &str, key: &str) -> Result<(), Self::Error> {
|
|
let blob_client = self.get_blob_client(bucket, key);
|
|
blob_client.delete().await.unwrap();
|
|
Ok(())
|
|
}
|
|
|
|
async fn list_objects(
|
|
&self,
|
|
_bucket: &str,
|
|
_prefix: Option<&str>,
|
|
) -> Result<Vec<String>, Self::Error> {
|
|
todo!("not impl")
|
|
}
|
|
|
|
async fn object_exists(&self, _bucket: &str, _key: &str) -> Result<bool, Self::Error> {
|
|
todo!("not impl")
|
|
}
|
|
}
|