no POD tests, base64 encodes right stuff
[Net-Amazon-S3-Policy.git] / eg / sample-form.pl
blob8b7f87e65ce5d2edf59ce8b8615e15d53ec92004
1 #!/usr/bin/env perl
2 use strict;
3 use warnings;
5 use Template::Perlish qw( render );
7 use lib '../lib';
8 use Net::Amazon::S3::Policy;
10 if (@ARGV != 3) {
11 print {*STDERR} <<'DOCUMENTATION';
12 perl sample-form.pl <AWS-ID> <AWS-secret> <bucket>
14 Prints the policy on STDERR, prints the sample web page with the form
15 on STDOUT.
17 DOCUMENTATION
19 exit 1;
20 } ## end if (@ARGV != 3)
22 my ($aws_key, $aws_secret, $bucket) = @ARGV;
23 my $policy = Net::Amazon::S3::Policy->new(
24 expiration => time() + 60 * 60, # one-hour policy
25 conditions => [
26 'key starts-with restricted/', # restrict to here
27 "success_action_redirect starts-with http://$bucket.s3.amazonaws.com/restricted/",
28 "bucket eq $bucket",
29 'Content-Type starts-with image/',
30 'x-amz-meta-colour *',
31 'acl eq public-read',
35 print {*STDERR} $policy->json(), "\n";
37 my $template = do { local $/; <DATA> };
38 print {*STDOUT} render(
39 $template,
40 policy => $policy->base64(),
41 signature => $policy->signature_base64($aws_secret),
42 AWSAccessKeyId => $aws_key,
43 bucket => $bucket,
46 __END__
47 <?xml version="1.0" encoding="utf-8"?>
48 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
49 "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
50 <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
52 <head>
53 <title>An example form page for Amazon S3 HTTP POST interface</title>
54 <meta http-equiv="content-type" content="text/html;charset=utf-8" />
55 <meta http-equiv="Content-Style-Type" content="text/css" />
56 <style><!--
57 * {margin:0; padding:0;}
59 html { padding:1em; }
60 div.clearer { clear: both; }
61 form { width:27em; }
62 fieldset {
63 padding: 0.5em;
64 border: 1px solid #ccc;
66 legend {
67 color:#000;
68 font-size:1.2em;
70 label, input {
71 padding:0.15em;
72 margin: 0.3em;
73 float:left;
75 label {
76 width: 5em;
77 text-align:right;
79 input {
80 width:10em;
81 border:1px solid #ddd;
82 background:#fafafa;
84 --></style>
85 </head>
87 <body>
89 <h1>Amazon S3 HTTP POST</h1>
91 <form action="https://[% bucket %].s3.amazonaws.com/" method="post"
92 enctype="multipart/form-data" id="uploader">
93 <fieldset>
94 <legend>File Upload</legend>
96 <!-- inputs needed because bucket is not publicly writeable -->
97 <input type="hidden" name="AWSAccessKeyId" value="[% AWSAccessKeyId %]" />
98 <input type="hidden" name="policy" value="[% policy %]" />
99 <input type="hidden" name="signature" value="[% signature %]" />
101 <!-- input needed by AWS-S3 logic: there MUST be a key -->
102 <input type="hidden" name="key" value="restricted/${filename}" />
104 <!-- inputs that you want to include in your form -->
105 <input type="hidden" name="acl" value="public-read" />
106 <input type="hidden" name="Content-Type" value="image/jpeg" />
107 <input type="hidden" name="success_action_redirect"
108 value="http://[% bucket %].s3.amazonaws.com/restricted/${filename}" />
110 <label for="colour">Colour:</label>
111 <input type="text" name="x-amz-meta-colour" id="colour" value="green" />
113 <div class="clearer" />
115 <!-- input needed to have something to upload. LAST IN FORM! -->
116 <label for="file">File:</label>
117 <input type="file" id="file" name="file" />
119 <div class="clearer" />
121 <label for="submit"></label>
122 <input type="submit" id="submit" name="submit" value="Upload!" />
124 </fieldset>
126 </form>
128 </body>
130 </html>