Amazon-S3-FastUploader

 view release on metacpan or  search on metacpan

lib/Amazon/S3/FastUploader.pm  view on Meta::CPAN

package Amazon::S3::FastUploader;
use strict;
use warnings;
use File::Find;
use Amazon::S3;
use Amazon::S3::FastUploader::File;
use Parallel::ForkManager;
use base qw( Class::Accessor );
__PACKAGE__->mk_accessors( qw(config) );

our $VERSION = '0.08';

sub new {
    my $class = shift;
    my $config = shift;
    bless { config => $config }, $class;
}


sub upload {

    my $self = shift;
    my $local_dir = shift;
    my $bucket_name = shift;
    my $target_dir = shift;

    my $config = $self->config;

    my $process = $config->{process};
    my $s3 = Amazon::S3->new($config);

    my $bucket = $s3->bucket($bucket_name) or die 'cannot get bucket';

    $self->_print("local  dir : " . $local_dir . "\n");
    $self->_print("remote dir : " . $target_dir . "\n");
    $self->_print("max process: " . $process . "\n");
    $self->_print("use SSL: " . $config->{secure}. "\n");
    $self->_print("use encryption: " . $config->{encrypt}. "\n");

    my @local_files;

    my $callback = sub {
        return unless -f ;
        my $file = Amazon::S3::FastUploader::File->new({
            s3         => $s3,
            local_path => $File::Find::name,
            target_dir => $target_dir,
            bucket     => $bucket,
            config     => $config,
        });
        push @local_files , $file;
    };

    chdir $local_dir;
    find($callback, '.');

    if ($process > 1) {
        $self->_upload_parallel(\@local_files, $process);
    } else {
        $self->_upload_single(\@local_files);
    }
}

sub _upload_single {
    my $self = shift;
    my @files = @{ shift; };

    $self->_print("uploading by a single process\n");

    my $i = 0;
    my $total_num = @files;

    for my $file (@files) {
        $i++;

        $file->upload();



( run in 1.673 second using v1.01-cache-2.11-cpan-39bf76dae61 )