use std::path::Path;
use async_trait::async_trait;
use bytes::Bytes;
use object_store::ObjectStore;
use super::{GetResponse, StoreDriver, UploadResponse};
use crate::storage::StorageResult;
pub struct ObjectStoreAdapter {
object_store_impl: Box<dyn object_store::ObjectStore>,
}
impl ObjectStoreAdapter {
#[must_use]
pub fn new(object_store_impl: Box<dyn ObjectStore>) -> Self {
Self { object_store_impl }
}
}
#[async_trait]
impl StoreDriver for ObjectStoreAdapter {
async fn upload(&self, path: &Path, content: &Bytes) -> StorageResult<UploadResponse> {
let path = object_store::path::Path::from(path.display().to_string());
let res = self
.object_store_impl
.put(&path, content.clone().into())
.await?;
Ok(UploadResponse {
e_tag: res.e_tag,
version: res.version,
})
}
async fn get(&self, path: &Path) -> StorageResult<GetResponse> {
let path = object_store::path::Path::from(path.display().to_string());
Ok(self.object_store_impl.get(&path).await?)
}
async fn delete(&self, path: &Path) -> StorageResult<()> {
let path = object_store::path::Path::from(path.display().to_string());
Ok(self.object_store_impl.delete(&path).await?)
}
async fn rename(&self, from: &Path, to: &Path) -> StorageResult<()> {
let from = object_store::path::Path::from(from.display().to_string());
let to = object_store::path::Path::from(to.display().to_string());
Ok(self.object_store_impl.rename(&from, &to).await?)
}
async fn copy(&self, from: &Path, to: &Path) -> StorageResult<()> {
let from = object_store::path::Path::from(from.display().to_string());
let to = object_store::path::Path::from(to.display().to_string());
Ok(self.object_store_impl.copy(&from, &to).await?)
}
async fn exists(&self, path: &Path) -> StorageResult<bool> {
let path = object_store::path::Path::from(path.display().to_string());
Ok(self.object_store_impl.get(&path).await.is_ok())
}
}