/
S3.pm
258 lines (174 loc) · 5.85 KB
/
S3.pm
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
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
package Bric::Util::Trans::S3;
=head1 Name
Bric::Util::Trans::S3 - AWS S3 Client interface for distributing resources.
=cut
# Grab the Version Number.
require Bric; our $VERSION = Bric->VERSION;
=head1 Synopsis
use Bric::Util::Trans::S3
=head1 Description
The distribution API uses this class to distribute resources to AWS S3 buckets.
=cut
################################################################################
# Dependencies
################################################################################
# Standard Dependencies
use strict;
use Net::Amazon::S3;
################################################################################
# Programmatic Dependences
use Bric::Util::Fault qw(throw_gen);
use Bric::Util::Trans::FS;
use Bric::Config qw(:dist);
use Bric::Util::ApacheUtil qw(unescape_uri escape_uri);
################################################################################
# Inheritance
################################################################################
use base qw(Bric);
################################################################################
# Function and Closure Prototypes
################################################################################
################################################################################
# Constants
################################################################################
use constant DEBUG => 0;
################################################################################
# Fields
################################################################################
# Public Class Fields
################################################################################
# Private Class Fields
my $fs = Bric::Util::Trans::FS->new;
################################################################################
################################################################################
# Instance Fields
BEGIN { Bric::register_fields() }
################################################################################
# Class Methods
################################################################################
=head1 Interface
=head2 Constructors
NONE.
=head2 Destructors
NONE.
=head2 Public Class Methods
NONE.
=head2 Public Instance Methods
NONE.
=head2 Public Functions
=over
=item my $bool = Bric::Util::Trans::S3->put_res($resources, $st)
Puts the files specified by the $job object into the S3 bucket specified by $st.
B<Throws:>
=over 4
=item *
Unable to connect to AWS API.
=item *
Unable to login to AWS account.
=item *
Unable to set permissions on bucket.
=item *
Unable to put resources into bucket.
=back
B<Side Effects:> NONE.
B<Notes:>
=cut
sub put_res {
my ($pkg, $resources, $st) = @_;
foreach my $s ($st->get_servers) {
# Skip inactive servers.
next unless $s->is_active;
my $s3 = Net::Amazon::S3->new({
aws_access_key_id => $s->get_login,
aws_secret_access_key => $s->get_password,
retry => 1,
}) or
throw_gen
error => 'fail',
payload => 'login: ' . $s->get_login . "\n" . 'password: ' . $s->get_password;
# Instantiate the S3 bucket
my $bucket = $s3->bucket($s->get_host_name);
foreach my $res (@$resources) {
# Get the source and destination paths for the resource.
my $src = $res->get_tmp_path || $res->get_path;
my $dest = $s->get_doc_root . substr $res->get_uri, 1;
my $mime = $res->get_media_type;
#throw_gen error => 'fail', payload => "src: $src\ndest: $dest\nmime: $mime";
# store a file in the bucket
$bucket->add_key_filename( $dest, $src,
{ content_type => $mime, })
or
throw_gen
error => $s3->err,
payload => 'src: ' . $src . "\ndest: " . $dest . ' * ' . $s3->errstr;
# set the ACL so everyone can see the file
$bucket->set_acl({'acl_short' => 'public-read', 'key' => $dest})
or throw_gen
error => $s3->err,
payload => $s3->errstr;
}
return 1;
}
}
################################################################################
=item my $bool = Bric::Util::Trans::S3->del_res($resources, $st)
Deletes the files specified by the $job object from the servers specified by
$st.
B<Throws:>
=over 4
=item *
Unable to connect to remote server.
=item *
Unable to login to remote server.
=item *
Unable to delete resource from remote server.
=item *
Unable to properly close connection to remote server.
=back
B<Side Effects:> NONE.
B<Notes:> See put_res(), above.
=cut
sub del_res {
my ($pkg, $resources, $st) = @_;
foreach my $s ($st->get_servers) {
# Skip inactive servers.
next unless $s->is_active;
my $s3 = Net::Amazon::S3->new({
aws_access_key_id => $s->get_login,
aws_secret_access_key => $s->get_password,
retry => 1,
});
# Instantiate the S3 bucket
my $bucket = $s3->bucket($s->get_host_name);
foreach my $res (@$resources) {
# Get the name of the file to be deleted.
my $target = $s->get_doc_root . $res->get_uri;
$bucket->delete_key($target)
or throw_gen
error => $s3->err,
payload => $s3->errstr;
}
}
return 1;
}
=back
=head1 Private
=head2 Private Class Methods
NONE.
=head2 Private Instance Methods
NONE.
=head2 Private Functions
NONE.
=cut
1;
__END__
=head1 Notes
NONE.
=head1 Author
John Durkin E<lt>john@j2d3.comE<gt>
=head1 See Also
L<Bric|Bric>,
L<Bric::Dist::Action|Bric::Dist::Action>,
L<Bric::Dist::Action::Mover|Bric::Dist::Action::Mover>,
L<Bric::Util::Trans::FS|Bric::Util::Trans::FS>
=cut