WEB开发网
开发学院服务器云计算 Perl 和 Amazon 云,第 2 部分:通过 HTML 表单将... 阅读

Perl 和 Amazon 云,第 2 部分:通过 HTML 表单将数据安全地上传到 S3

 2010-02-04 00:00:00 来源:WEB开发网   
核心提示: 清单 3. s3form.pl#!/usr/bin/perlusewarnings;usestrict;useData::Dumper;useDigest::HMAC_SHA1qw(hmac_sha1hmac_sha1_hex);useMIME::Base64;my$aws_access_key

清单 3. s3form.pl

#!/usr/bin/perl 
 
use warnings; 
use strict; 
use Data::Dumper; 
use Digest::HMAC_SHA1 qw(hmac_sha1 hmac_sha1_hex); 
use MIME::Base64; 
 
my $aws_access_key_id   = 'get it from 
 http://aws-portal.amazon.com/gp/aws/developer/account/index.html?action=access-key'; 
my $aws_secret_access_key = 'get it from 
 http://aws-portal.amazon.com/gp/aws/developer/account/index.html?action=access-key'; 
 
my $user = 'username'; # this is the user name for the upload 
 
my $policy = '{"expiration": "3000-01-01T00:00:00Z", 
 "conditions": [ 
  {"bucket": "images.share.lifelogs.com"}, 
  {"acl": "public-read"}, 
  ["starts-with", "$key", ""], 
  ["starts-with", "$Content-Type", ""], 
  {"success_action_redirect": "http://share.lifelogs.com/s3uploaded/$user"}, 
  ["content-length-range", 0, 1048576] 
 ] 
}'; 
 
$policy = encode_base64($policy); 
$policy =~ s/\n//g; 
 
my $signature = encode_base64(hmac_sha1($policy, $aws_secret_access_key)); 
 
$signature =~ s/\n//g; 
print <<EOHIPPUS; 
<html> 
 <head> 
  <title>S3 POST Form</title> 
  <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /> 
  <script src="http://ajax.googleapis.com/ajax/libs/prototype/1.6.0.3/prototype.js" 
         type="text/javascript"></script> 
 </head> 
 
 <body> 
 <script language="JavaScript"> 
function submitUploaderForm() 
{ 
 var form = \$('uploader'); // note the escapes we do from Perl 
 var file = form['file']; 
 var ct = form['Content-Type']; 
 
 var filename = ''+\$F(file); // note the escapes we do from Perl 
 var f = filename.toLowerCase(); // always compare against the lowercase version 
 
 if (!navigator['mimeTypes']) 
 { 
 alert("Sorry, your browser can't tell us what type of file you're uploading."); 
 return false; 
 } 
 
 var type = \$A(navigator.mimeTypes).detect(function(m) 
 { 
 // does any of the suffixes match? 
 // note the escapes we do from Perl 
 return m.type.length > 3 && m.type.match('/') && 
    \$A(m.suffixes.split(',')).detect(function(suffix) 
 { 
  return f.match('\\.' + suffix.toLowerCase() + '\$'); 
   // note the escapes we do from Perl 
 }); 
 }); 
 
 if (type && type['type']) 
 { 
 ct.value = type.type; 
 return true; 
 } 
 
 alert("Sorry, we don't know the type for file " + filename); 
 return false; 
} 
</script> 
  <form id="uploader" action="https://images.share.lifelogs.com.s3.amazonaws.com/" 
   method="post" enctype="multipart/form-data" 
   > 
   <input type="hidden" name="key" value="\${filename}"> 
   <input type="hidden" name="AWSAccessKeyId" value="$aws_access_key_id"> 
   <input type="hidden" name="acl" value="public-read"> 
   <input type="hidden" name="success_action_redirect" 
    value="http://share.lifelogs.com/s3uploaded/$user"> 
   <input type="hidden" name="policy" value="$policy"> 
   <input type="hidden" name="Content-Type" value="image/jpeg"> 
   <input type="hidden" name="signature" value="$signature"> 
   Select File to upload to S3: 
   <input name="file" type="file"> 
   <br> 
   <input type="submit" value="Upload File to S3"> 
  </form> 
 </body> 
</html> 
EOHIPPUS 

上一页  1 2 3 4 5  下一页

Tags:Perl Amazon 部分

编辑录入:爽爽 [复制链接] [打 印]
赞助商链接