AWS-S3

 view release on metacpan or  search on metacpan

t/010_basic.t  view on Meta::CPAN

    # use alternative update method
    $new_contents = 'More new content';
    $file->update( contents => \$new_contents );
    is ${$bucket->file($filename)->contents}, $new_contents, "set file.update works";
  };
  
  DELETE_FILE: {
    eval { $bucket->delete };
    ok $@, 'bucket.delete fails when bucket is not empty.';
    like $@, qr/BucketNotEmpty/, 'error looks like BucketNotEmpty';
    ok $bucket->file($filename)->delete, 'file.delete';
    ok ! $bucket->file($filename), 'file no longer exists in bucket';
  };
  
  ADD_MANY_FILES: {
    my %info = ( );
    
    # Add the files:
    for( 0..25 )
    {
      my $contents  = "Contents of file $_\n"x4;
      my $key       = "bar/baz/foo." . sprintf("%03d", $_) . ".txt";
      $info{$key} = $contents;
      ok $bucket->add_file(
        key       => $key,
        contents  => \$contents,
      ), "Added file $_";
    }# end for()
    
    # Make sure they all worked:
    my $counted = 0;
    foreach my $key ( sort keys %info )
    {
      my $contents = $info{$key};
      ok my $file = $bucket->file($key), "bucket.file($key) returned a file";
      is $file->size, length($contents), 'file.size is correct';
      is ${$file->contents}, $contents, 'file.contents is correct';
      my $expiration_date = time() + 3600;
      my $url = $file->signed_url( $expiration_date );
      is( $file->signed_url( $expiration_date ),$url,'signed_url same' ) for 1 .. 10;
warn "--->$url";
      my $res = $s3->ua->get( $url );
      ok( $res->is_success,'get signed_url' );
      isnt( $res->code,403,'not forbidden' );
      last if $counted++ > 4;
    }# end for()
    
    # Try iterating through the files:
    my $iter = $bucket->files( page_size => 2, page_number => 1 );
    $counted = 0;
    while( my @files = $iter->next_page )
    {
      foreach my $file ( @files )
      {
        is ${$file->contents}, $info{$file->key}, "file(@{[$file->key]}).contents works on iterated files";
        last if $counted++ > 4;
      }# end foreach()
      last;
    }# end while()
    
    # Make sure that if we say we want to start on page 11, we *start* on page 11:
    $iter = $bucket->files( page_size => 1, page_number => 18 );
    SMALL_ITER: {
      for( 18..25 )
      {
        my ($file) = $iter->next_page;
        my $number = sprintf('%03d', $_);
        is $file->key, "bar/baz/foo.$number.txt", "file $number is what we expected";
      }# end for()
    };
    
    # How about when our page size is larger than what we get back from S3?:
#    $iter = $bucket->files( page_size => 105, page_number => 2 );
#    BIG_ITER: {
#      my @files = $iter->next_page;
#      for( 106..116 )
#      {
#        my $file = shift(@files);
#        is $file->key, "bar/baz/foo.$_.txt", "file $_ is what we expected";
#      }# end for()
#    };
    
    # Delete the files:
    ok($bucket->delete_multi( map { $_ } sort keys %info ), 'bucket.delete_multi(@keys)' );
    
    # Now make sure that not a single one still exists:
    foreach( sort keys %info )
    {
      ok ! eval {$bucket->file($_)}, "bucket(@{[ $bucket->name ]}).file($_) doesn't exist";
    }# end foreach()
#    map {
#      ok $bucket->file($_)->delete && ! $bucket->file($_), "bucket.file($_).delete worked"
#    } sort keys %info;
  };
  
  
  # proof content type reading and writing
  CONTENT_TYPE: {
    
    foreach my $ct( qw( text/plain image/jpeg application/zip ) ) {
      
      # write file with specific content type
      ( my $ct_name = $ct ) =~ s#/#-#;
      ok( $bucket->add_file(
        key         => "$ct_name.dat",
        contents    => \( 'This is '. $ct ),
        contenttype => $ct
      ), "Put file with content type $ct" );
      
      # read file
      my $ct_file = $bucket->file( "$ct_name.dat" );
      ok( $ct_file && $ct_file->contenttype eq $ct, 'Content type '. $ct. ' read' );
      
      # change content type
      $ct_file->update( contenttype => 'text/csv' );
      $ct_file = $bucket->file( "$ct_name.dat" );
      ok( $ct_file && $ct_file->contenttype eq 'text/csv', 'Content type '. $ct. ' changed to text/csv' );
      
      # remove file
      $ct_file->delete();
    }



( run in 0.566 second using v1.01-cache-2.11-cpan-99c4e6809bf )