File: 05-multipart-upload.t

package info (click to toggle)
libamazon-s3-perl 0.65-1
  • links: PTS, VCS
  • area: main
  • in suites: sid, trixie
  • size: 444 kB
  • sloc: perl: 2,914; makefile: 4
file content (181 lines) | stat: -rw-r--r-- 3,959 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
#!/usr/bin/perl -w

## no critic

use warnings;
use strict;

use lib qw( . lib);

use Carp;

use Data::Dumper;
use Digest::MD5::File qw(file_md5_hex);
use English           qw{-no_match_vars};
use File::Temp        qw{ tempfile };
use Test::More;

use S3TestUtils qw(:constants :subs);

my $host = set_s3_host();

if ( !$ENV{'AMAZON_S3_EXPENSIVE_TESTS'} ) {
  plan skip_all => 'Testing this module for real costs money.';
}
else {
  plan tests => 7;
}

use_ok('Amazon::S3');
use_ok('Amazon::S3::Bucket');

my $s3 = get_s3_service($host);

if ( !$s3 ) {
  BAIL_OUT('could not initialize s3 object');
}

my $bucket_name = make_bucket_name();
my $bucket_obj  = create_bucket( $s3, $bucket_name );

ok( ref $bucket_obj, 'created bucket - ' . $bucket_name );

if ( $EVAL_ERROR || !$bucket_obj ) {
  BAIL_OUT( $s3->err . ": " . $s3->errstr );
} ## end if ( $EVAL_ERROR || !$bucket_obj)

########################################################################
subtest 'multipart-manual' => sub {
########################################################################
  my $key = 'big-object-1';

  my $id = $bucket_obj->initiate_multipart_upload($key);

  my $part_list = {};

  my $part = 0;
  my $data = 'x' x ( 1024 * 1024 * 5 ); # 5 MB part

  my $etag
    = $bucket_obj->upload_part_of_multipart_upload( $key, $id, ++$part, $data,
    length $data );

  $part_list->{$part} = $etag;

  $bucket_obj->complete_multipart_upload( $key, $id, $part_list );

  my $head = $bucket_obj->head_key($key);

  ok( $head, 'uploaded file' );

  ok( $head->{content_length} == 5 * 1024 * 1024, 'uploaded 1 part' )
    or diag( Dumper( [$head] ) );

  ok( $bucket_obj->delete_key($key) );
};

########################################################################
subtest 'multipart-file' => sub {
########################################################################
  my ( $fh, $file ) = tempfile();

  my $buffer = 'x' x ( 1024 * 1024 );

  # 11MB
  foreach ( 0 .. 10 ) {
    $fh->syswrite($buffer);
  }

  $fh->close;

  if ( !open( $fh, '<', $file ) ) {
    carp "could not open $file after writing";

    return;
  }

  my $key = 'big-object-2';

  $bucket_obj->upload_multipart_object( fh => $fh, key => $key );

  close $fh;

  my $head = $bucket_obj->head_key($key);

  ok( $head, 'uploaded file' );

  isa_ok( $head, 'HASH', 'head is a hash' );

  ok( $head->{content_length} == 11 * 1024 * 1024, 'uploaded all parts' );

  $bucket_obj->delete_key($key);

  unlink $file;
};

########################################################################
subtest 'multipart-2-parts' => sub {
########################################################################
  my $length = 1024 * 1024 * 7;

  my $data = 'x' x $length;

  my $key = 'big-object-3';

  $bucket_obj->upload_multipart_object(
    key  => $key,
    data => $data
  );

  my $head = $bucket_obj->head_key($key);

  isa_ok( $head, 'HASH', 'head is a hash' );

  ok( $head, 'uploaded data' );

  ok( $head->{content_length} == $length, 'uploaded all parts' );

  $bucket_obj->delete_key($key);
};

########################################################################
subtest 'multipart-callback' => sub {
########################################################################
  my $key = 'big-object-4';

  my @part = ( 5, 5, 5, 1 );
  my $size;

  $bucket_obj->upload_multipart_object(
    key      => $key,
    callback => sub {
      return ( q{}, 0 ) unless @part;

      my $length = shift @part;
      $length *= 1024 * 1024;

      $size += $length;

      my $data = 'x' x $length;

      return ( \$data, $length );
    }
  );

  my $head = $bucket_obj->head_key($key);

  isa_ok( $head, 'HASH', 'head is a hash' );

  ok( $head, 'uploaded data' );

  ok( $head->{content_length} == $size, 'uploaded all parts' );

  $bucket_obj->delete_key($key);
};

########################################################################

$bucket_obj->delete_bucket()
  or diag( $s3->errstr );

1;