summaryrefslogtreecommitdiff
path: root/IkiWiki/Plugin/amazon_s3.pm
blob: 187700f30d911dee95009a7d2515ef3aca61e176 (plain)
  1. #!/usr/bin/perl
  2. package IkiWiki::Plugin::amazon_s3;
  3. use warnings;
  4. no warnings 'redefine';
  5. use strict;
  6. use IkiWiki 2.00;
  7. use IkiWiki::Render;
  8. use Net::Amazon::S3;
  9. # Store references to real subs before overriding them.
  10. our %subs;
  11. BEGIN {
  12. foreach my $sub (qw{IkiWiki::writefile IkiWiki::prune}) {
  13. $subs{$sub}=\&$sub;
  14. }
  15. };
  16. sub import { #{{{
  17. hook(type => "getopt", id => "amazon_s3", call => \&getopt);
  18. hook(type => "checkconfig", id => "amazon_s3", call => \&checkconfig);
  19. } # }}}
  20. sub getopt () { #{{{
  21. eval q{use Getopt::Long};
  22. error($@) if $@;
  23. Getopt::Long::Configure('pass_through');
  24. GetOptions("delete-bucket" => sub {
  25. my $bucket=getbucket();
  26. debug(gettext("deleting bucket.."));
  27. my $resp = $bucket->list_all or die $bucket->err . ": " . $bucket->errstr;
  28. foreach my $key (@{$resp->{keys}}) {
  29. debug("\t".$key->{key});
  30. $bucket->delete_key($key->{key}) or die $bucket->err . ": " . $bucket->errstr;
  31. }
  32. $bucket->delete_bucket or die $bucket->err . ": " . $bucket->errstr;
  33. debug(gettext("done"));
  34. exit(0);
  35. });
  36. } #}}}
  37. sub checkconfig { #{{{
  38. foreach my $field (qw{amazon_s3_key_id amazon_s3_key_file
  39. amazon_s3_bucket}) {
  40. if (! exists $config{$field} || ! defined $config{$field}) {
  41. error(sprintf(gettext("Must specify %s"), $field));
  42. }
  43. }
  44. if (! exists $config{amazon_s3_prefix} ||
  45. ! defined $config{amazon_s3_prefix}) {
  46. $config{amazon_s3_prefix}="wiki/";
  47. }
  48. } #}}}
  49. {
  50. my $bucket;
  51. sub getbucket { #{{{
  52. return $bucket if defined $bucket;
  53. open(IN, "<", $config{amazon_s3_key_file}) || error($config{amazon_s3_key_file}.": ".$!);
  54. my $key=<IN>;
  55. chomp $key;
  56. close IN;
  57. my $s3=Net::Amazon::S3->new({
  58. aws_access_key_id => $config{amazon_s3_key_id},
  59. aws_secret_access_key => $key,
  60. retry => 1,
  61. });
  62. # make sure the bucket exists
  63. if (exists $config{amazon_s3_location}) {
  64. $bucket=$s3->add_bucket({
  65. bucket => $config{amazon_s3_bucket},
  66. location_constraint => $config{amazon_s3_location},
  67. });
  68. }
  69. else {
  70. $bucket=$s3->add_bucket({
  71. bucket => $config{amazon_s3_bucket},
  72. });
  73. }
  74. if (! $bucket) {
  75. error(gettext("Failed to create bucket in S3: ").
  76. $s3->err.": ".$s3->errstr."\n");
  77. }
  78. return $bucket;
  79. } #}}}
  80. }
  81. # Given a file, return any S3 keys associated with it.
  82. sub file2keys ($) { #{{{
  83. my $file=shift;
  84. my @keys;
  85. if ($file =~ /^\Q$config{destdir}\/\E(.*)/) {
  86. push @keys, $config{amazon_s3_prefix}.$1;
  87. # Munge foo/index.html to foo/
  88. if ($keys[0]=~/(^|.*\/)index.$config{htmlext}$/) {
  89. # A duplicate might need to be stored under the
  90. # unmunged name too.
  91. if (!$config{usedirs} || $config{amazon_s3_dupindex}) {
  92. push @keys, $1;
  93. }
  94. else {
  95. @keys=($1);
  96. }
  97. }
  98. }
  99. return @keys;
  100. } #}}}
  101. package IkiWiki;
  102. use File::MimeInfo;
  103. use Encode;
  104. # This is a wrapper around the real writefile.
  105. sub writefile ($$$;$$) { #{{{
  106. my $file=shift;
  107. my $destdir=shift;
  108. my $content=shift;
  109. my $binary=shift;
  110. my $writer=shift;
  111. # First, write the file to disk.
  112. my $ret=$IkiWiki::Plugin::amazon_s3::subs{'IkiWiki::writefile'}->($file, $destdir, $content, $binary, $writer);
  113. my @keys=IkiWiki::Plugin::amazon_s3::file2keys("$destdir/$file");
  114. # Store the data in S3.
  115. if (@keys) {
  116. my $bucket=IkiWiki::Plugin::amazon_s3::getbucket();
  117. # The http layer tries to downgrade utf-8
  118. # content, but that can fail (see
  119. # http://rt.cpan.org/Ticket/Display.html?id=35710),
  120. # so force convert it to bytes.
  121. $content=encode_utf8($content) if defined $content;
  122. my %opts=(
  123. acl_short => 'public-read',
  124. content_type => mimetype("$destdir/$file"),
  125. );
  126. # If there are multiple keys to write, data is sent
  127. # multiple times.
  128. # TODO: investigate using the new copy operation.
  129. # (It may not be robust enough.)
  130. foreach my $key (@keys) {
  131. my $res;
  132. if (! $writer) {
  133. $res=$bucket->add_key($key, $content, \%opts);
  134. }
  135. else {
  136. # This test for empty files is a workaround
  137. # for this bug:
  138. # http://rt.cpan.org//Ticket/Display.html?id=35731
  139. if (-z "$destdir/$file") {
  140. $res=$bucket->add_key($key, "", \%opts);
  141. }
  142. else {
  143. # read back in the file that the writer emitted
  144. $res=$bucket->add_key_filename($key, "$destdir/$file", \%opts);
  145. }
  146. }
  147. if (! $res) {
  148. error(gettext("Failed to save file to S3: ").
  149. $bucket->err.": ".$bucket->errstr."\n");
  150. }
  151. }
  152. }
  153. return $ret;
  154. } #}}}
  155. # This is a wrapper around the real prune.
  156. sub prune ($) { #{{{
  157. my $file=shift;
  158. my @keys=IkiWiki::Plugin::amazon_s3::file2keys($file);
  159. # Prune files out of S3 too.
  160. if (@keys) {
  161. my $bucket=IkiWiki::Plugin::amazon_s3::getbucket();
  162. foreach my $key (@keys) {
  163. my $res=$bucket->delete_key($key);
  164. if (! $res) {
  165. error(gettext("Failed to delete file from S3: ").
  166. $bucket->err.": ".$bucket->errstr."\n");
  167. }
  168. }
  169. }
  170. return $IkiWiki::Plugin::amazon_s3::subs{'IkiWiki::prune'}->($file);
  171. } #}}}
  172. 1