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 => "getsetup", id => "amazon_s3", call => \&getsetup);
22 hook(type => "checkconfig", id => "amazon_s3", call => \&checkconfig);
26 eval q{use Getopt::Long};
28 Getopt::Long::Configure('pass_through');
29 GetOptions("delete-bucket" => sub {
30 my $bucket=getbucket();
31 debug(gettext("deleting bucket.."));
32 my $resp = $bucket->list_all or die $bucket->err . ": " . $bucket->errstr;
33 foreach my $key (@{$resp->{keys}}) {
34 debug("\t".$key->{key});
35 $bucket->delete_key($key->{key}) or die $bucket->err . ": " . $bucket->errstr;
37 $bucket->delete_bucket or die $bucket->err . ": " . $bucket->errstr;
38 debug(gettext("done"));
48 section => "special-purpose",
52 example => "XXXXXXXXXXXXXXXXXXXX",
53 description => "public access key id",
59 example => "$ENV{HOME}/.s3_key",
60 description => "file holding secret key (must not be readable by others!)",
61 safe => 0, # ikiwiki reads this file
67 description => "globally unique name of bucket to store wiki in",
74 description => "a prefix to prepend to each page name",
78 amazon_s3_location => {
81 description => "which S3 datacenter to use (leave blank for default)",
85 amazon_s3_dupindex => {
88 description => "store each index file twice? (allows urls ending in \"/index.html\" and \"/\")",
95 foreach my $field (qw{amazon_s3_key_id amazon_s3_key_file
97 if (! exists $config{$field} || ! defined $config{$field}) {
98 error(sprintf(gettext("Must specify %s"), $field));
101 if (! exists $config{amazon_s3_prefix} ||
102 ! defined $config{amazon_s3_prefix}) {
103 $config{amazon_s3_prefix}="wiki/";
110 return $bucket if defined $bucket;
112 open(IN, "<", $config{amazon_s3_key_file}) || error($config{amazon_s3_key_file}.": ".$!);
117 my $s3=Net::Amazon::S3->new({
118 aws_access_key_id => $config{amazon_s3_key_id},
119 aws_secret_access_key => $key,
123 # make sure the bucket exists
124 if (exists $config{amazon_s3_location}) {
125 $bucket=$s3->add_bucket({
126 bucket => $config{amazon_s3_bucket},
127 location_constraint => $config{amazon_s3_location},
131 $bucket=$s3->add_bucket({
132 bucket => $config{amazon_s3_bucket},
137 # Try to use existing bucket.
138 $bucket=$s3->bucket($config{amazon_s3_bucket});
141 error(gettext("Failed to create S3 bucket: ").
142 $s3->err.": ".$s3->errstr."\n");
149 # Given a file, return any S3 keys associated with it.
154 if ($file =~ /^\Q$config{destdir}\/\E(.*)/) {
155 push @keys, $config{amazon_s3_prefix}.$1;
157 # Munge foo/index.html to foo/
158 if ($keys[0]=~/(^|.*\/)index.$config{htmlext}$/) {
159 # A duplicate might need to be stored under the
161 if (!$config{usedirs} || $config{amazon_s3_dupindex}) {
176 # This is a wrapper around the real writefile.
177 sub writefile ($$$;$$) {
184 # First, write the file to disk.
185 my $ret=$IkiWiki::Plugin::amazon_s3::subs{'IkiWiki::writefile'}->($file, $destdir, $content, $binary, $writer);
187 my @keys=IkiWiki::Plugin::amazon_s3::file2keys("$destdir/$file");
189 # Store the data in S3.
191 my $bucket=IkiWiki::Plugin::amazon_s3::getbucket();
193 # The http layer tries to downgrade utf-8
194 # content, but that can fail (see
195 # http://rt.cpan.org/Ticket/Display.html?id=35710),
196 # so force convert it to bytes.
197 $content=encode_utf8($content) if defined $content;
200 acl_short => 'public-read',
201 content_type => mimetype("$destdir/$file"),
204 # If there are multiple keys to write, data is sent
206 # TODO: investigate using the new copy operation.
207 # (It may not be robust enough.)
208 foreach my $key (@keys) {
211 $res=$bucket->add_key($key, $content, \%opts);
214 # This test for empty files is a workaround
216 # http://rt.cpan.org//Ticket/Display.html?id=35731
217 if (-z "$destdir/$file") {
218 $res=$bucket->add_key($key, "", \%opts);
221 # read back in the file that the writer emitted
222 $res=$bucket->add_key_filename($key, "$destdir/$file", \%opts);
226 error(gettext("Failed to save file to S3: ").
227 $bucket->err.": ".$bucket->errstr."\n");
235 # This is a wrapper around the real prune.
239 my @keys=IkiWiki::Plugin::amazon_s3::file2keys($file);
241 # Prune files out of S3 too.
243 my $bucket=IkiWiki::Plugin::amazon_s3::getbucket();
245 foreach my $key (@keys) {
246 my $res=$bucket->delete_key($key);
248 error(gettext("Failed to delete file from S3: ").
249 $bucket->err.": ".$bucket->errstr."\n");
254 return $IkiWiki::Plugin::amazon_s3::subs{'IkiWiki::prune'}->($file);