Amazon-S3-Thin

 view release on metacpan or  search on metacpan

eg/s3  view on Meta::CPAN

#!/usr/bin/env perl
# command tool like aws s3
use strict;
use warnings;
use FindBin;
use lib $FindBin::Bin . '/../lib';
use Amazon::S3::Thin;
use Config::Tiny;

S3::CLI->new->run(@ARGV);


package S3::CLI;
use strict;
use warnings;
use Getopt::Long;
use Amazon::S3::Thin;
use Data::Dumper;

sub new {
    return bless {}, shift;
}

sub help {
    my ($self, @args) = @_;
    require Pod::Usage;
    Pod::Usage::pod2usage(0);
}

sub run {
    my ($self, @args) = @_;

    our $VERSION = "0.25";

    my $p = Getopt::Long::Parser->new(
        config => [qw(posix_default no_ignore_case bundling)],
        );

    $p->getoptionsfromarray(
        \@args,
        "p|profile=s"   => \(my $profile = "default"),
        "r|region=s"    => \(my $region),
        "h|help"        => \(my $help),
        "version"       => \(my $version),
        "v|verbose"     => \($self->{verbose}),
    );
    if ($version) {
        printf "s3 %s\n", $VERSION;
        exit 0;
    }
    if ($help) {
        $self->help();
        exit 0;
    }

    # https://docs.aws.amazon.com/cli/latest/userguide/cli-config-files.html
    my $cred_file = $ENV{HOME} . "/.aws/credentials";
    my $config_file = $ENV{HOME} . "/.aws/config";

    if (-f $config_file and !defined $region) {
        my $config = Config::Tiny->read($config_file)->{$profile};
        $region = $config->{region};
    }

    if (-f $cred_file) {
        my $crd = Config::Tiny->read($cred_file)->{$profile};

        my $opt = +{
            aws_access_key_id     => $crd->{aws_access_key_id},
            aws_secret_access_key => $crd->{aws_secret_access_key},
            region                => $region
        };
        $self->{thin_client} = Amazon::S3::Thin->new($opt);
    }
    elsif (defined $ENV{AWS_ACCESS_KEY_ID} and defined $ENV{AWS_SECRET_ACCESS_KEY}) {
        my $opt = +{
            credential_provider => 'env',
            region              => $region
        };
        $self->{thin_client} = Amazon::S3::Thin->new($opt);
    }
    elsif (defined $ENV{AWS_CONTAINER_CREDENTIALS_RELATIVE_URI}) {
        my $opt = +{
            credential_provider => 'ecs_container',
            region              => $region
        };
        $self->{thin_client} = Amazon::S3::Thin->new($opt);
    }
    else {
        my $opt = +{
            credential_provider => 'metadata',
            version             => 1,
            region              => $region
        };
        $self->{thin_client} = Amazon::S3::Thin->new($opt);
    }

    my $subcmd = shift @args;
    if (!$subcmd) {
        $self->help();
        exit 0;
    }
    #warn Dumper $subcmd, $profile , \@args;    n
    if ($subcmd eq "ls") {
        $self->cmd_ls(@args);
    } elsif ($subcmd eq "rm") {
        $self->cmd_rm(@args);
    } elsif ($subcmd eq "cp") {
        $self->cmd_cp(@args);
    } elsif ($subcmd eq "rb") {
        $self->cmd_rb(@args);
    } else {        
        die "invalid sub command";
    }

}



( run in 1.485 second using v1.01-cache-2.11-cpan-98e64b0badf )