Project

General

Profile

Feature #18800 ยป rgw-s3-aws4-form.py

Javier M. Mellid, 03/10/2017 01:13 PM

 
#!/usr/bin/python

import boto3

# data

REGION = 'eu-west-1'
BUCKET = 'test-1-2-1-bucket'
KEY = 'test-1-2-1-key'

TEST_FILE = 'test-rgw-s3-aws4-form.html'

# here we go!

session = boto3.session.Session(
region_name = REGION
)

s3_client = session.client(
's3',
use_ssl = False,
endpoint_url = "http://s3.amazonaws.com:8000",
config = boto3.session.Config(
signature_version = 's3v4'
)
)

# generate presigned post data

form_data = s3_client.generate_presigned_post(
Conditions = [ ["starts-with", "$Content-Type", ""] ],
Bucket = BUCKET,
Key = KEY
)

html = """\
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
</head>
<body>
<form action="{url}" method="post" enctype="multipart/form-data">
""".format(url=form_data['url'])

for k, v in form_data['fields'].items():
html += """
<input type="hidden" name="{key}" value="{value}" />
""".format(key=k,value=v)

html += """
File:
<input type="file" name="file" /> <br />
<input type="submit" name="submit" value="Upload to Amazon S3" />
</form>
</html>
"""

file = open(TEST_FILE, "w")
file.write(html)
file.close()

print TEST_FILE + " created."
    (1-1/1)