durch / rust-s3

Rust library for interfacing with S3 API compatible services
MIT License
498 stars 195 forks source link

Implement a new `ListBuckets` command and its consumer API. #348

Closed aalekhpatel07 closed 8 months ago

aalekhpatel07 commented 1 year ago

Summary

- Feature: The serde_types now store chrono::DateTime<chrono::Utc> instead of String, wherever there was any datetime-like data to be stored. Turns out we get different formats across different datetime fields, so we can't specialize it into one type, unless we add a deserializer that guesses the format and tries to convert any datetime strings into a chrono::DateTime<chrono::Utc> by trying parsing different formats (and that probably merits another PR).

Bucket::list_buckets(region, credentials)

Given a region, and credentials, list all the buckets that are visible.

Usage

use s3::{Bucket, BucketConfiguration};
use s3::creds::Credentials;
use s3::region::Region;
use anyhow::Result;

#[tokio::main]
async fn main() -> Result<()> {
    let region = Region::Custom {
      region: "eu-central-1".to_owned(),
      endpoint: "http://localhost:9000".to_owned()
    };
    let credentials = Credentials::default()?;

    // Async variant with `tokio` or `async-std` features
    let response = Bucket::list_buckets(region, credentials).await?;

    // `sync` feature will produce an identical method
    #[cfg(feature = "sync")]
    let response = Bucket::list_buckets(region, credentials)?;

    // Blocking variant, generated with `blocking` feature in combination
    // with `tokio` or `async-std` features.
    #[cfg(feature = "blocking")]
    let response = Bucket::list_buckets_blocking(region, credentials)?;

    let found_buckets = response.bucket_names().collect::<Vec<String>>();
    println!("found buckets: {:#?}", found_buckets);
    Ok(())
}

Bucket::exists

Determine whether the instantitated bucket exists.

Usage

use s3::{Bucket, BucketConfiguration};
use s3::creds::Credentials;
use s3::region::Region;
use anyhow::Result;

#[tokio::main]
async fn main() -> Result<()> {
    let bucket_name = "some-bucket-that-is-known-to-exist";
    let region = Region::Custom {
      region: "eu-central-1".to_owned(),
      endpoint: "http://localhost:9000".to_owned()
    };
    let credentials = Credentials::default()?;

    let bucket = Bucket::new(bucket_name, region, credentials)?;

    // Async variant with `tokio` or `async-std` features
    let exists = bucket.exists().await?;

    // `sync` feature will produce an identical method
    #[cfg(feature = "sync")]
    let exists = bucket.exists()?;

    // Blocking variant, generated with `blocking` feature in combination
    // with `tokio` or `async-std` features.
    #[cfg(feature = "blocking")]
    let exists = bucket.exists_blocking()?;

    assert_eq!(exists, true);
    Ok(())
}
aalekhpatel07 commented 1 year ago

haha. I didn't know when I issued this PR but this also closes #251. Initially I was working towards the sync API but I realized we need a few more building blocks to get there.

durch commented 8 months ago

@aalekhpatel07 Nice work, thank you!

aalekhpatel07 commented 8 months ago

Thank you for merging!