2 package IkiWiki::Plugin::amazon_s3;
5 no warnings 'redefine';
11 # Store references to real subs before overriding them.
14 foreach my $sub (qw{IkiWiki::writefile IkiWiki::prune}) {
20 hook(type => "getopt", id => "amazon_s3", call => \&getopt);
21 hook(type => "checkconfig", id => "amazon_s3", call => \&checkconfig);
25 eval q{use Getopt::Long};
27 Getopt::Long::Configure('pass_through');
28 GetOptions("delete-bucket" => sub {
29 my $bucket=getbucket();
30 debug(gettext("deleting bucket.."));
31 my $resp = $bucket->list_all or die $bucket->err . ": " . $bucket->errstr;
32 foreach my $key (@{$resp->{keys}}) {
33 debug("\t".$key->{key});
34 $bucket->delete_key($key->{key}) or die $bucket->err . ": " . $bucket->errstr;
36 $bucket->delete_bucket or die $bucket->err . ": " . $bucket->errstr;
37 debug(gettext("done"));
42 sub checkconfig { #{{{
43 foreach my $field (qw{amazon_s3_key_id amazon_s3_key_file
45 if (! exists $config{$field} || ! defined $config{$field}) {
46 error(sprintf(gettext("Must specify %s"), $field));
49 if (! exists $config{amazon_s3_prefix} ||
50 ! defined $config{amazon_s3_prefix}) {
51 $config{amazon_s3_prefix}="wiki/";
58 return $bucket if defined $bucket;
60 open(IN, "<", $config{amazon_s3_key_file}) || error($config{amazon_s3_key_file}.": ".$!);
65 my $s3=Net::Amazon::S3->new({
66 aws_access_key_id => $config{amazon_s3_key_id},
67 aws_secret_access_key => $key,
71 # make sure the bucket exists
72 if (exists $config{amazon_s3_location}) {
73 $bucket=$s3->add_bucket({
74 bucket => $config{amazon_s3_bucket},
75 location_constraint => $config{amazon_s3_location},
79 $bucket=$s3->add_bucket({
80 bucket => $config{amazon_s3_bucket},
85 error(gettext("Failed to create bucket in S3: ").
86 $s3->err.": ".$s3->errstr."\n");
93 # Given a file, return any S3 keys associated with it.
94 sub file2keys ($) { #{{{
98 if ($file =~ /^\Q$config{destdir}\/\E(.*)/) {
99 push @keys, $config{amazon_s3_prefix}.$1;
101 # Munge foo/index.html to foo/
102 if ($keys[0]=~/(^|.*\/)index.$config{htmlext}$/) {
103 # A duplicate might need to be stored under the
105 if (!$config{usedirs} || $config{amazon_s3_dupindex}) {
120 # This is a wrapper around the real writefile.
121 sub writefile ($$$;$$) { #{{{
128 # First, write the file to disk.
129 my $ret=$IkiWiki::Plugin::amazon_s3::subs{'IkiWiki::writefile'}->($file, $destdir, $content, $binary, $writer);
131 my @keys=IkiWiki::Plugin::amazon_s3::file2keys("$destdir/$file");
133 # Store the data in S3.
135 my $bucket=IkiWiki::Plugin::amazon_s3::getbucket();
137 # The http layer tries to downgrade utf-8
138 # content, but that can fail (see
139 # http://rt.cpan.org/Ticket/Display.html?id=35710),
140 # so force convert it to bytes.
141 $content=encode_utf8($content) if defined $content;
144 acl_short => 'public-read',
145 content_type => mimetype("$destdir/$file"),
148 # If there are multiple keys to write, data is sent
150 # TODO: investigate using the new copy operation.
151 # (It may not be robust enough.)
152 foreach my $key (@keys) {
155 $res=$bucket->add_key($key, $content, \%opts);
158 # This test for empty files is a workaround
160 # http://rt.cpan.org//Ticket/Display.html?id=35731
161 if (-z "$destdir/$file") {
162 $res=$bucket->add_key($key, "", \%opts);
165 # read back in the file that the writer emitted
166 $res=$bucket->add_key_filename($key, "$destdir/$file", \%opts);
170 error(gettext("Failed to save file to S3: ").
171 $bucket->err.": ".$bucket->errstr."\n");
179 # This is a wrapper around the real prune.
183 my @keys=IkiWiki::Plugin::amazon_s3::file2keys($file);
185 # Prune files out of S3 too.
187 my $bucket=IkiWiki::Plugin::amazon_s3::getbucket();
189 foreach my $key (@keys) {
190 my $res=$bucket->delete_key($key);
192 error(gettext("Failed to delete file from S3: ").
193 $bucket->err.": ".$bucket->errstr."\n");
198 return $IkiWiki::Plugin::amazon_s3::subs{'IkiWiki::prune'}->($file);