Show
Ignore:
Timestamp:
10/25/08 17:13:59 (5 years ago)
Author:
lopnor
Message:

implemented list/put/get/remove

Location:
lang/perl/Net-Amazon-HadoopEC2-S3fs/trunk
Files:
4 added
2 modified

Legend:

Unmodified
Added
Removed
  • lang/perl/Net-Amazon-HadoopEC2-S3fs/trunk

    • Property svn:ignore set to
      META.yml
      Makefile
      inc
      blib
      pm_to_blib
  • lang/perl/Net-Amazon-HadoopEC2-S3fs/trunk/lib/Net/Amazon/HadoopEC2/S3fs.pm

    r22083 r22106  
    11package Net::Amazon::HadoopEC2::S3fs; 
     2use Moose; 
     3our $VERSION = '0.01'; 
    24 
    3 use strict; 
    4 use warnings; 
    5 our $VERSION = '0.01'; 
     5use Net::Amazon::S3; 
     6use Net::Amazon::HadoopEC2::S3fs::Inode; 
     7use File::Basename; 
     8 
     9has aws_access_key_id => ( is => 'ro', isa => 'Str', required => 1 ); 
     10has aws_secret_access_key => ( is => 'ro', isa => 'Str', required => 1 ); 
     11has bucket => ( is => 'ro', isa => 'Str', required => 1 ); 
     12has s3 => ( is => 'ro', isa => 'Net::Amazon::S3::Bucket', required => 1, lazy => 1, 
     13    default => sub { 
     14        my $self = shift; 
     15        return Net::Amazon::S3->new( 
     16            { 
     17                aws_access_key_id     => $self->aws_access_key_id, 
     18                aws_secret_access_key => $self->aws_secret_access_key, 
     19                retry                 => 1, 
     20            } 
     21        )->bucket($self->bucket); 
     22    } 
     23); 
     24 
     25__PACKAGE__->meta->make_immutable; 
     26 
     27no Moose; 
     28 
     29sub list { 
     30    my ($self, $path) = @_; 
     31    my $res = $self->s3->list( 
     32        { 
     33            prefix => $path, 
     34        } 
     35    ) 
     36        or die $self->s3->errstr; 
     37    my $response = []; 
     38    for my $key (@{$res->{keys}}) { 
     39        push @{$response}, Net::Amazon::HadoopEC2::S3fs::Inode->new( 
     40            { 
     41                path => $key->{key}, 
     42                _s3 => $self->s3, 
     43            } 
     44        ); 
     45    } 
     46    return $response; 
     47} 
     48 
     49sub put { 
     50    my ($self, $args) = @_; 
     51    my $filename = basename($args->{file}); 
     52    my $list = $self->list(dirname($args->{destination})) or return; 
     53    scalar @{$list} == 0 and return; 
     54    my $inode = Net::Amazon::HadoopEC2::S3fs::Inode->new({ _s3 => $self->s3 , inode_type => 'file'}); 
     55    if ( my $existing = grep { $_->path eq $args->{destination} && $_->inode_type eq 'direcotry' } @{$list}) { 
     56        $inode->path("$args->{destination}$filename"); 
     57    } else { 
     58        $inode->path($args->{destination});     
     59    } 
     60    $inode->put($args->{file}) or return; 
     61    return $inode; 
     62} 
     63 
     64sub mkdir { 
     65    my ($self, $dir) = @_; 
     66    my $list = $self->list($dir) or return; 
     67    scalar @{$list} and return; 
     68    my $inode = Net::Amazon::HadoopEC2::S3fs::Inode->new( 
     69        { 
     70            path => $dir, 
     71            inode_type => 'direcotry', 
     72            _s3 => $self->s3,  
     73        } 
     74    ); 
     75    $inode->put or return; 
     76    return $inode; 
     77} 
     78 
     79sub get { 
     80    my ($self, $args) = @_; 
     81    my ($file) = @{$self->list($args->{file})} or return; 
     82    return $file->get({destination => $args->{destination}});  
     83} 
     84 
     85sub remove { 
     86    my ($self, $path) = @_; 
     87    my $list = $self->list($path) or return; 
     88    scalar @{$list} == 1 or return; 
     89    return $list->[0]->remove; 
     90} 
    691 
    7921; 
     
    1095=head1 NAME 
    1196 
    12 Net::Amazon::HadoopEC2::S3fs - 
     97Net::Amazon::HadoopEC2::S3fs - Perl interface for hadoop s3fs 
    1398 
    1499=head1 SYNOPSIS 
     
    16101  use Net::Amazon::HadoopEC2::S3fs; 
    17102 
     103  my $fs = Net::Amazon::HadoopEC2::S3fs->new( 
     104    { 
     105        aws_access_key_id => $EVN{AWS_ACCESS_KEY_ID}, 
     106        aws_secret_access_key => $ENV{AWS_SECRET_ACCESS_KEY}, 
     107        bucket => 'your_bucket', 
     108    } 
     109  ); 
     110  my $file = $fs->put( 
     111    { 
     112        file => 'filename', 
     113        destination => '.', 
     114    } 
     115  ); 
     116  my $files_listed = $fs->ls( 
     117    { 
     118        path => '/user/root', 
     119    } 
     120  ); 
     121 
     122  for my $file (@{$files_listed}) { 
     123      $file->remove; 
     124  } 
     125 
    18126=head1 DESCRIPTION 
    19127 
    20 Net::Amazon::HadoopEC2::S3fs is 
     128Net::Amazon::HadoopEC2::S3fs is a Perl interface for hadoop s3fs. 
    21129 
    22130=head1 AUTHOR