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