Skip to content

armgit / macro

Public Imported from GitHub
0

Macro is a unified workspace for teams: email, chat, docs, tasks, agents, calls, and CRM — @-linked together with shared AI memory.

3.7 KB
Raw Blame
use aws_sdk_s3::presigning::PresignedRequest;

mod copy;
mod delete;
mod exists;
mod get;
mod get_folder;
mod put;

#[derive(Clone, Debug)]
pub struct S3 {
    inner: aws_sdk_s3::Client,
}

impl S3 {
    /// Returns the stored size and SHA-256 checksum without downloading the body.
    pub async fn verified_metadata(
        &self,
        bucket: &str,
        key: &str,
    ) -> anyhow::Result<(i64, Option<String>)> {
        use base64::Engine;
        let response = self
            .inner
            .head_object()
            .bucket(bucket)
            .key(key)
            .checksum_mode(aws_sdk_s3::types::ChecksumMode::Enabled)
            .send()
            .await?;
        let sha = response
            .checksum_sha256()
            .map(|checksum| {
                base64::engine::general_purpose::STANDARD
                    .decode(checksum)
                    .map(hex::encode)
            })
            .transpose()?;
        Ok((response.content_length().unwrap_or_default(), sha))
    }

    pub fn new(inner: aws_sdk_s3::Client) -> Self {
        Self { inner }
    }

    /// Retreives the provided key from the bucket.
    #[tracing::instrument(skip(self))]
    pub async fn get(&self, bucket: &str, key: &str) -> anyhow::Result<Vec<u8>> {
        get::get(&self.inner, bucket, key).await
    }

    /// Gets a presigned url for the provided key.
    #[tracing::instrument(skip(self))]
    pub async fn get_presigned_url(
        &self,
        bucket: &str,
        key: &str,
        duration_seconds: u64,
    ) -> anyhow::Result<PresignedRequest> {
        get::get_presigned_url(&self.inner, bucket, key, duration_seconds).await
    }

    /// Puts the provided content into the bucket at the provided key.
    #[tracing::instrument(skip(self))]
    pub async fn put(&self, bucket: &str, key: &str, content: &[u8]) -> anyhow::Result<()> {
        put::put(&self.inner, bucket, key, content).await
    }

    /// Generates a presigned URL for uploading a file to a bucket.
    #[tracing::instrument(skip(self))]
    pub async fn put_presigned_url(
        &self,
        bucket: &str,
        key: &str,
        sha: &str,
        mime_type: &str,
    ) -> anyhow::Result<String> {
        put::put_presigned_url(&self.inner, bucket, key, sha, mime_type).await
    }

    /// Checks if a given key exists in the bucket.
    #[tracing::instrument(skip(self))]
    pub async fn exists(&self, bucket: &str, key: &str) -> anyhow::Result<bool> {
        exists::exists(&self.inner, bucket, key).await
    }

    /// Deletes the provided key from the bucket.
    #[tracing::instrument(skip(self))]
    pub async fn delete(&self, bucket: &str, key: &str) -> anyhow::Result<()> {
        delete::delete(&self.inner, bucket, key).await
    }

    /// Deletes all items under a given folder
    #[tracing::instrument(skip(self))]
    pub async fn delete_folder(&self, bucket: &str, folder: &str) -> anyhow::Result<()> {
        delete::delete_folder(&self.inner, bucket, folder).await
    }

    /// Gets all the keys in a folder
    /// Returns a list of the (file_name, file_type) for each file in the folder
    #[tracing::instrument(skip(self))]
    pub async fn get_folder_content_names(
        &self,
        bucket: &str,
        folder_path: &str,
    ) -> anyhow::Result<Vec<(String, String)>> {
        get_folder::get_folder_content_names(&self.inner, bucket, folder_path).await
    }

    /// Copies an object from one bucket to another place in the same bucket
    pub async fn inner_bucket_copy(
        &self,
        bucket: &str,
        source_key: &str,
        destination_key: &str,
    ) -> anyhow::Result<()> {
        copy::inner_bucket_copy(&self.inner, bucket, source_key, destination_key).await
    }
}