Perl 和 Amazon 云,第 2 部分:通过 HTML 表单将数据安全地上传到 S3
2010-02-04 00:00:00 来源:WEB开发网清单 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
- ››Perl 6 发布
- ››部分 WM6.5 手机有望升级到 Windows Phone 7
- ››Perl 和 Amazon 云,第 1 部分:通过构建简单的照...
- ››Perl 和 Amazon 云,第 2 部分:通过 HTML 表单将...
- ››Perl 和 Amazon 云,第 3 部分:上传图像并创建、...
- ››Perl 和 Amazon 云,第 4 部分:深入探究完整 mod...
- ››Perl 和 Amazon 云,第 5 部分:了解完整 mod_per...
- ››部分英特尔芯片不支持Windows 7“XP模式”
- ››Perl的5个常见错误
- ››Perl操作mysql数据库的方法
- ››Perl的经典用法
- ››部分VZPP无法正常访问怎么解决?
更多精彩
赞助商链接