rgw-s3-aws4-form.py
1 |
#!/usr/bin/python
|
---|---|
2 |
|
3 |
import boto3 |
4 |
|
5 |
# data
|
6 |
|
7 |
REGION = 'eu-west-1'
|
8 |
BUCKET = 'test-1-2-1-bucket'
|
9 |
KEY = 'test-1-2-1-key'
|
10 |
|
11 |
TEST_FILE = 'test-rgw-s3-aws4-form.html'
|
12 |
|
13 |
# here we go!
|
14 |
|
15 |
session = boto3.session.Session( |
16 |
region_name = REGION |
17 |
) |
18 |
|
19 |
s3_client = session.client( |
20 |
's3',
|
21 |
use_ssl = False,
|
22 |
endpoint_url = "http://s3.amazonaws.com:8000",
|
23 |
config = boto3.session.Config( |
24 |
signature_version = 's3v4'
|
25 |
) |
26 |
) |
27 |
|
28 |
# generate presigned post data
|
29 |
|
30 |
form_data = s3_client.generate_presigned_post( |
31 |
Conditions = [ ["starts-with", "$Content-Type", ""] ], |
32 |
Bucket = BUCKET, |
33 |
Key = KEY |
34 |
) |
35 |
|
36 |
html = """\
|
37 |
<html>
|
38 |
<head>
|
39 |
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
|
40 |
</head>
|
41 |
<body>
|
42 |
<form action="{url}" method="post" enctype="multipart/form-data">
|
43 |
""".format(url=form_data['url']) |
44 |
|
45 |
for k, v in form_data['fields'].items(): |
46 |
html += """
|
47 |
<input type="hidden" name="{key}" value="{value}" />
|
48 |
""".format(key=k,value=v)
|
49 |
|
50 |
html += """
|
51 |
File:
|
52 |
<input type="file" name="file" /> <br />
|
53 |
<input type="submit" name="submit" value="Upload to Amazon S3" />
|
54 |
</form>
|
55 |
</html>
|
56 |
"""
|
57 |
|
58 |
file = open(TEST_FILE, "w") |
59 |
file.write(html)
|
60 |
file.close()
|
61 |
|
62 |
print TEST_FILE + " created." |