use std::time::Instant;
use bytes::Bytes;
use minio::s3::creds::StaticProvider;
use minio::s3::http::BaseUrl;
use minio::s3::types::{S3Api, ToStream};
use minio::s3::Client;
use tokio_stream::StreamExt;
use vil_log::dict::register_str;
use vil_log::{db_log, types::DbPayload};
use crate::config::S3Config;
use crate::error::S3Fault;
use crate::stream::collect_content;
const OP_PUT: u8 = 1; const OP_GET: u8 = 0; const OP_DELETE: u8 = 3; const OP_LIST: u8 = 0; const OP_HEAD: u8 = 0;
#[derive(Debug, Clone)]
pub struct PutResult {
pub e_tag: Option<String>,
pub version_id: Option<String>,
}
#[derive(Debug, Clone)]
pub struct ObjectMeta {
pub key: String,
pub size: u64,
pub last_modified: Option<String>,
pub e_tag: Option<String>,
}
pub struct S3Client {
inner: Client,
bucket: String,
config_hash: u32,
}
impl S3Client {
pub async fn new(config: S3Config) -> Result<Self, S3Fault> {
let endpoint = config
.endpoint
.as_deref()
.unwrap_or("https://s3.amazonaws.com");
let config_hash = register_str(endpoint);
let base_url: BaseUrl = endpoint.parse().map_err(|_| S3Fault::ConnectionFailed {
endpoint_hash: config_hash,
reason_code: 1,
})?;
let provider: Option<Box<dyn minio::s3::creds::Provider + Send + Sync>> =
match (config.access_key.as_deref(), config.secret_key.as_deref()) {
(Some(ak), Some(sk)) => Some(Box::new(StaticProvider::new(ak, sk, None))),
_ => None,
};
let inner =
Client::new(base_url, provider, None, None).map_err(|_| S3Fault::ConnectionFailed {
endpoint_hash: config_hash,
reason_code: 2,
})?;
Ok(Self {
inner,
bucket: config.bucket,
config_hash,
})
}
fn emit_log(&self, key_hash: u32, elapsed_ns: u64, rows: u32, op: u8, err: u8) {
db_log!(
Info,
DbPayload {
db_hash: self.config_hash,
table_hash: register_str(&self.bucket),
query_hash: key_hash,
duration_ns: elapsed_ns,
rows_affected: rows,
op_type: op,
error_code: err,
..Default::default()
}
);
}
pub async fn put_object(&self, key: &str, body: Bytes) -> Result<PutResult, S3Fault> {
let start = Instant::now();
let key_hash = register_str(key);
let size = body.len() as u64;
let result = self
.inner
.put_object_content(&self.bucket, key, body)
.send()
.await;
let ns = start.elapsed().as_nanos() as u64;
match result {
Ok(resp) => {
self.emit_log(key_hash, ns, 1, OP_PUT, 0);
Ok(PutResult {
e_tag: Some(resp.etag),
version_id: resp.version_id,
})
}
Err(_) => {
self.emit_log(key_hash, ns, 0, OP_PUT, 1);
Err(S3Fault::UploadFailed { key_hash, size })
}
}
}
pub async fn get_object(&self, key: &str) -> Result<Bytes, S3Fault> {
let start = Instant::now();
let key_hash = register_str(key);
let result = self.inner.get_object(&self.bucket, key).send().await;
let ns = start.elapsed().as_nanos() as u64;
match result {
Ok(resp) => match collect_content(resp.content).await {
Ok(data) => {
self.emit_log(key_hash, ns, 1, OP_GET, 0);
Ok(data)
}
Err(fault) => {
self.emit_log(key_hash, ns, 0, OP_GET, 1);
Err(fault)
}
},
Err(_) => {
self.emit_log(key_hash, ns, 0, OP_GET, 1);
Err(S3Fault::NotFound { key_hash })
}
}
}
pub async fn delete_object(&self, key: &str) -> Result<(), S3Fault> {
let start = Instant::now();
let key_hash = register_str(key);
let result = self.inner.delete_object(&self.bucket, key).send().await;
let ns = start.elapsed().as_nanos() as u64;
match result {
Ok(_) => {
self.emit_log(key_hash, ns, 1, OP_DELETE, 0);
Ok(())
}
Err(_) => {
self.emit_log(key_hash, ns, 0, OP_DELETE, 1);
Err(S3Fault::Unknown {
message_hash: register_str("delete_object_error"),
})
}
}
}
pub async fn list_objects(&self, prefix: &str) -> Result<Vec<ObjectMeta>, S3Fault> {
let start = Instant::now();
let prefix_hash = register_str(prefix);
let mut stream = self
.inner
.list_objects(&self.bucket)
.prefix(Some(prefix.to_string()))
.to_stream()
.await;
let mut objects = Vec::new();
while let Some(page) = stream.next().await {
match page {
Ok(resp) => {
for entry in resp.contents {
if entry.is_prefix {
continue; }
objects.push(ObjectMeta {
key: entry.name,
size: entry.size.unwrap_or(0),
last_modified: entry
.last_modified
.as_ref()
.map(|t| t.to_string()),
e_tag: entry.etag,
});
}
}
Err(_) => {
let ns = start.elapsed().as_nanos() as u64;
self.emit_log(prefix_hash, ns, 0, OP_LIST, 1);
return Err(S3Fault::BucketNotFound {
bucket_hash: register_str(&self.bucket),
});
}
}
}
let count = objects.len() as u32;
let ns = start.elapsed().as_nanos() as u64;
self.emit_log(prefix_hash, ns, count, OP_LIST, 0);
Ok(objects)
}
pub async fn head_object(&self, key: &str) -> Result<ObjectMeta, S3Fault> {
let start = Instant::now();
let key_hash = register_str(key);
let result = self.inner.stat_object(&self.bucket, key).send().await;
let ns = start.elapsed().as_nanos() as u64;
match result {
Ok(resp) => {
self.emit_log(key_hash, ns, 1, OP_HEAD, 0);
Ok(ObjectMeta {
key: key.to_owned(),
size: resp.size,
last_modified: resp.last_modified.as_ref().map(|t| t.to_string()),
e_tag: Some(resp.etag),
})
}
Err(_) => {
self.emit_log(key_hash, ns, 0, OP_HEAD, 1);
Err(S3Fault::NotFound { key_hash })
}
}
}
pub async fn presigned_url(&self, key: &str, expires_secs: u64) -> Result<String, S3Fault> {
let start = Instant::now();
let key_hash = register_str(key);
let result = self
.inner
.get_presigned_object_url(&self.bucket, key, http::Method::GET)
.expiry_seconds(expires_secs as u32)
.send()
.await;
let ns = start.elapsed().as_nanos() as u64;
match result {
Ok(resp) => {
self.emit_log(key_hash, ns, 1, OP_GET, 0);
Ok(resp.url)
}
Err(_) => {
self.emit_log(key_hash, ns, 0, OP_GET, 1);
Err(S3Fault::Unknown {
message_hash: register_str("presign_error"),
})
}
}
}
}