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 => "checkconfig", id => "amazon_s3", call => \&checkconfig);
23 sub checkconfig { #{{{
24 foreach my $field (qw{amazon_s3_key_id amazon_s3_key_file
26 if (! exists $config{$field} || ! defined $config{$field}) {
27 error(sprintf(gettext("Must specify %s"), $field));
30 if (! exists $config{amazon_s3_prefix} ||
31 ! defined $config{amazon_s3_prefix}) {
32 $config{amazon_s3_prefix}="wiki/";
39 return $bucket if defined $bucket;
41 open(IN, "<", $config{amazon_s3_key_file}) || error($config{amazon_s3_key_file}.": ".$!);
46 my $s3=Net::Amazon::S3->new({
47 aws_access_key_id => $config{amazon_s3_key_id},
48 aws_secret_access_key => $key,
52 # make sure the bucket exists
53 if (exists $config{amazon_s3_location}) {
54 $bucket=$s3->add_bucket({
55 bucket => $config{amazon_s3_bucket},
56 location_constraint => $config{amazon_s3_location},
60 $bucket=$s3->add_bucket({
61 bucket => $config{amazon_s3_bucket},
66 error(gettext("Failed to create bucket in S3: ").
67 $s3->err.": ".$s3->errstr."\n");
74 # Given a file, return any S3 keys associated with it.
75 sub file2keys ($) { #{{{
79 if ($file =~ /^\Q$config{destdir}\/\E(.*)/) {
80 push @keys, $config{amazon_s3_prefix}.$1;
82 # Munge foo/index.html to foo/
83 if ($keys[0]=~/(^|.*\/)index.$config{htmlext}$/) {
84 # A duplicate might need to be stored under the
86 if (!$config{usedirs} || $config{amazon_s3_dupindex}) {
101 # This is a wrapper around the real writefile.
102 sub writefile ($$$;$$) { #{{{
109 # First, write the file to disk.
110 my $ret=$IkiWiki::Plugin::amazon_s3::subs{'IkiWiki::writefile'}->($file, $destdir, $content, $binary, $writer);
112 my @keys=IkiWiki::Plugin::amazon_s3::file2keys("$destdir/$file");
114 # Store the data in S3.
116 my $bucket=IkiWiki::Plugin::amazon_s3::getbucket();
118 # The http layer tries to downgrade utf-8
119 # content, but that can fail (see
120 # http://rt.cpan.org/Ticket/Display.html?id=35710),
121 # so force convert it to bytes.
122 $content=encode_utf8($content) if defined $content;
125 acl_short => 'public-read',
126 content_type => mimetype("$destdir/$file"),
129 # If there are multiple keys to write, data is sent
131 # TODO: investigate using the new copy operation.
132 # (It may not be robust enough.)
133 foreach my $key (@keys) {
134 debug("storing $key");
137 $res=$bucket->add_key($key, $content, \%opts);
140 # This test for empty files is a workaround
142 # http://rt.cpan.org//Ticket/Display.html?id=35731
143 if (-z "$destdir/$file") {
144 $res=$bucket->add_key($key, "", \%opts);
147 # read back in the file that the writer emitted
148 $res=$bucket->add_key_filename($key, "$destdir/$file", \%opts);
152 error(gettext("Failed to save file to S3: ").
153 $bucket->err.": ".$bucket->errstr."\n");
161 # This is a wrapper around the real prune.
165 my @keys=IkiWiki::Plugin::amazon_s3::file2keys($file);
167 # Prune files out of S3 too.
169 my $bucket=IkiWiki::Plugin::amazon_s3::getbucket();
171 foreach my $key (@keys) {
172 debug("deleting $key");
173 my $res=$bucket->delete_key($key);
175 error(gettext("Failed to delete file from S3: ").
176 $bucket->err.": ".$bucket->errstr."\n");
181 return $IkiWiki::Plugin::amazon_s3::subs{'IkiWiki::prune'}->($file);