Unverified Commit b3a5d07e authored by Doug's avatar Doug Committed by GitHub
Browse files

Doug refactor s3 (#623)



* Updated S3 code examples to use common pattern; cleaned up Cargo.toml

* Removed unused crate from S3 Cargo.toml

* Updated S3 code examples; added delete-object and list-object-versions code examples

* Updated S3 list-objects code example

Co-authored-by: default avatarRussell Cohen <rcoh@amazon.com>
parent ae040559
Loading
Loading
Loading
Loading
+1 −6
Original line number Diff line number Diff line
@@ -7,13 +7,8 @@ edition = "2018"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
s3 = { package = "aws-sdk-s3", path = "../../build/aws-sdk/s3" }
aws-sdk-s3 = { package = "aws-sdk-s3", path = "../../build/aws-sdk/s3" }
aws-types = { path = "../../build/aws-sdk/aws-types" }

tokio = { version = "1", features = ["full"] }

structopt = { version = "0.3", default-features = false }
tracing-subscriber = "0.2.18"

[profile.dev]
split-debuginfo = "unpacked"
+36 −51
Original line number Diff line number Diff line
@@ -3,91 +3,76 @@
 * SPDX-License-Identifier: Apache-2.0.
 */

use std::process;

use s3::{Client, Config, Region};

use s3::model::{BucketLocationConstraint, CreateBucketConfiguration};

use aws_sdk_s3::model::{BucketLocationConstraint, CreateBucketConfiguration};
use aws_sdk_s3::{Client, Config, Error, Region, PKG_VERSION};
use aws_types::region;
use aws_types::region::ProvideRegion;

use structopt::StructOpt;
use tracing_subscriber::fmt::format::FmtSpan;
use tracing_subscriber::fmt::SubscriberBuilder;

#[derive(Debug, StructOpt)]
struct Opt {
    /// The default region
    /// The AWS Region.
    #[structopt(short, long)]
    default_region: Option<String>,
    region: Option<String>,

    /// The name of the bucket
    /// The name of the bucket.
    #[structopt(short, long)]
    name: String,
    bucket: String,

    /// Whether to display additional information
    /// Whether to display additional information.
    #[structopt(short, long)]
    verbose: bool,
}

/// Creates an Amazon S3 bucket
/// Creates an Amazon S3 bucket in the Region.
/// # Arguments
///
/// * `-n NAME` - The name of the bucket.
/// * `[-d DEFAULT-REGION]` - The region containing the bucket.
///   If not supplied, uses the value of the **AWS_DEFAULT_REGION** environment variable.
/// * `-b BUCKET` - The name of the bucket.
/// * `[-r REGION]` - The Region in which the client is created.
///   If not supplied, uses the value of the **AWS_REGION** environment variable.
///   If the environment variable is not set, defaults to **us-west-2**.
/// * `[-v]` - Whether to display additional information.
#[tokio::main]
async fn main() {
async fn main() -> Result<(), Error> {
    tracing_subscriber::fmt::init();

    let Opt {
        default_region,
        name,
        region,
        bucket,
        verbose,
    } = Opt::from_args();

    let region = default_region
        .as_ref()
        .map(|region| Region::new(region.clone()))
        .or_else(|| aws_types::region::default_provider().region())
        .unwrap_or_else(|| Region::new("us-west-2"));
    let region = region::ChainProvider::first_try(region.map(Region::new))
        .or_default_provider()
        .or_else(Region::new("us-west-2"));

    let r: &str = &region.as_ref();
    println!();

    if verbose {
        println!("S3 client version: {}", s3::PKG_VERSION);
        println!("Region:            {:?}", &region);
    let r_rgr = region.region().unwrap();
    let r_str = r_rgr.as_ref();

        SubscriberBuilder::default()
            .with_env_filter("info")
            .with_span_events(FmtSpan::CLOSE)
            .init();
    if verbose {
        println!("S3 client version: {}", PKG_VERSION);
        println!("Region:            {}", r_str);
        println!("Bucket:            {}", &bucket);
        println!();
    }

    let config = Config::builder().region(&region).build();

    let client = Client::from_conf(config);
    let conf = Config::builder().region(region).build();
    let client = Client::from_conf(conf);

    let constraint = BucketLocationConstraint::from(r);
    let constraint = BucketLocationConstraint::from(r_str);
    let cfg = CreateBucketConfiguration::builder()
        .location_constraint(constraint)
        .build();

    match client
    client
        .create_bucket()
        .create_bucket_configuration(cfg)
        .bucket(&name)
        .bucket(bucket)
        .send()
        .await
    {
        Ok(_) => {
            println!("Created bucket {}", name);
        }
        .await?;
    println!("Created bucket.");

        Err(e) => {
            println!("Got an error creating bucket:");
            println!("{}", e);
            process::exit(1);
        }
    };
    Ok(())
}
+77 −0
Original line number Diff line number Diff line
/*
 * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
 * SPDX-License-Identifier: Apache-2.0.
 */

use aws_sdk_s3::{Client, Config, Error, Region, PKG_VERSION};
use aws_types::region;
use aws_types::region::ProvideRegion;
use structopt::StructOpt;

#[derive(Debug, StructOpt)]
struct Opt {
    /// The AWS Region.
    #[structopt(short, long)]
    region: Option<String>,

    /// The name of the bucket.
    #[structopt(short, long)]
    bucket: String,

    /// The object to delete.
    #[structopt(short, long)]
    key: String,

    /// Whether to display additional information.
    #[structopt(short, long)]
    verbose: bool,
}

/// Deletes an object in an Amazon S3 bucket.
/// # Arguments
///
/// * `-b BUCKET` - The name of the bucket.
/// * `-k KEY` - The names of the object to delete.
/// * `[-r REGION]` - The Region in which the client is created.
///   If not supplied, uses the value of the **AWS_REGION** environment variable.
///   If the environment variable is not set, defaults to **us-west-2**.
/// * `[-v]` - Whether to display additional information.
#[tokio::main]
async fn main() -> Result<(), Error> {
    tracing_subscriber::fmt::init();

    let Opt {
        region,
        bucket,
        key,
        verbose,
    } = Opt::from_args();

    let region = region::ChainProvider::first_try(region.map(Region::new))
        .or_default_provider()
        .or_else(Region::new("us-west-2"));

    println!();

    if verbose {
        println!("S3 client version: {}", PKG_VERSION);
        println!("Region:            {}", region.region().unwrap().as_ref());
        println!("Bucket:            {}", &bucket);
        println!("Key:               {}", &key);
        println!();
    }

    let conf = Config::builder().region(region).build();
    let client = Client::from_conf(conf);

    client
        .delete_object()
        .bucket(&bucket)
        .key(key)
        .send()
        .await?;

    println!("Object deleted.");

    Ok(())
}
+31 −57
Original line number Diff line number Diff line
@@ -3,86 +3,60 @@
 * SPDX-License-Identifier: Apache-2.0.
 */

use std::process;

use s3::{Client, Config, Region};

use aws_sdk_s3::{Client, Config, Error, Region, PKG_VERSION};
use aws_types::region;
use aws_types::region::ProvideRegion;

use structopt::StructOpt;
use tracing_subscriber::fmt::format::FmtSpan;
use tracing_subscriber::fmt::SubscriberBuilder;

#[derive(Debug, StructOpt)]
struct Opt {
    /// The default region
    /// The AWS Region.
    #[structopt(short, long)]
    default_region: Option<String>,
    region: Option<String>,

    /// Whether to display additional information
    /// Whether to display additional information.
    #[structopt(short, long)]
    verbose: bool,
}

/// Lists your Amazon S3 buckets
/// Lists your Amazon S3 buckets in the Region.
/// # Arguments
///
/// * `[-d DEFAULT-REGION]` - The region containing the buckets.
///   If not supplied, uses the value of the **AWS_DEFAULT_REGION** environment variable.
/// * `[-r REGION]` - The Region in which the client is created.
///   If not supplied, uses the value of the **AWS_REGION** environment variable.
///   If the environment variable is not set, defaults to **us-west-2**.
/// * `[-g]` - Whether to display buckets in all regions.
/// * `[-v]` - Whether to display additional information.
#[tokio::main]
async fn main() {
    let Opt {
        default_region,
        verbose,
    } = Opt::from_args();

    let region = default_region
        .as_ref()
        .map(|region| Region::new(region.clone()))
        .or_else(|| aws_types::region::default_provider().region())
        .unwrap_or_else(|| Region::new("us-west-2"));
async fn main() -> Result<(), Error> {
    tracing_subscriber::fmt::init();

    if verbose {
        println!("S3 client version: {}", s3::PKG_VERSION);
        println!("Region:            {:?}", &region);

        SubscriberBuilder::default()
            .with_env_filter("info")
            .with_span_events(FmtSpan::CLOSE)
            .init();
    }
    let Opt { region, verbose } = Opt::from_args();

    let config = Config::builder().region(&region).build();
    let region = region::ChainProvider::first_try(region.map(Region::new))
        .or_default_provider()
        .or_else(Region::new("us-west-2"));

    let client = Client::from_conf(config);
    println!();

    let mut num_buckets = 0;
    if verbose {
        println!("S3 client version: {}", PKG_VERSION);
        println!("Region:            {}", region.region().unwrap().as_ref());
        println!();
    }

    match client.list_buckets().send().await {
        Ok(resp) => {
            println!("\nBuckets:\n");
    let conf = Config::builder().region(region).build();
    let client = Client::from_conf(conf);

    let resp = client.list_buckets().send().await?;
    let buckets = resp.buckets.unwrap_or_default();
    let num_buckets = buckets.len();

    for bucket in &buckets {
                match &bucket.name {
                    None => {}
                    Some(b) => {
                        println!("{}", b);
                        num_buckets += 1;
                    }
                }
        println!("{}", bucket.name.as_deref().unwrap_or_default());
    }

            println!("\nFound {} buckets globally", num_buckets);
        }
        Err(e) => {
            println!("Got an error listing buckets:");
            println!("{}", e);
            process::exit(1);
        }
    };
    println!();
    println!("Found {} buckets", num_buckets);

    Ok(())
}
+71 −0
Original line number Diff line number Diff line
/*
 * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
 * SPDX-License-Identifier: Apache-2.0.
 */

use aws_sdk_s3::{Client, Config, Error, Region, PKG_VERSION};
use aws_types::region;
use aws_types::region::ProvideRegion;
use structopt::StructOpt;

#[derive(Debug, StructOpt)]
struct Opt {
    /// The AWS Region.
    #[structopt(short, long)]
    region: Option<String>,

    /// The name of the bucket.
    #[structopt(short, long)]
    bucket: String,

    /// Whether to display additional information.
    #[structopt(short, long)]
    verbose: bool,
}

/// Lists the versions of the objects in an Amazon S3 bucket.
/// # Arguments
///
/// * `-b BUCKET` - The name of the bucket.
/// * `[-r REGION]` - The Region in which the client is created.
///   If not supplied, uses the value of the **AWS_REGION** environment variable.
///   If the environment variable is not set, defaults to **us-west-2**.
/// * `[-v]` - Whether to display additional information.
#[tokio::main]
async fn main() -> Result<(), Error> {
    tracing_subscriber::fmt::init();

    let Opt {
        region,
        bucket,
        verbose,
    } = Opt::from_args();

    let region = region::ChainProvider::first_try(region.map(Region::new))
        .or_default_provider()
        .or_else(Region::new("us-west-2"));

    println!();

    if verbose {
        println!("S3 client version: {}", PKG_VERSION);
        println!("Region:            {}", region.region().unwrap().as_ref());
        println!("Bucket:            {}", &bucket);
        println!();
    }

    let conf = Config::builder().region(region).build();
    let client = Client::from_conf(conf);

    let resp = client.list_object_versions().bucket(&bucket).send().await?;

    for version in resp.versions.unwrap_or_default() {
        println!(" {}", version.key.as_deref().unwrap_or_default());
        println!(
            "  version ID: {}",
            version.version_id.as_deref().unwrap_or_default()
        );
    }

    Ok(())
}
Loading