Skip to content

Instantly share code, notes, and snippets.

@href
Last active July 9, 2020 12:55
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save href/94001b77ebf9361aa36a96e7e07c9159 to your computer and use it in GitHub Desktop.
Save href/94001b77ebf9361aa36a96e7e07c9159 to your computer and use it in GitHub Desktop.
Generates an S3 HTML Upload Form for cloudscale.ch Object Storage
#!/usr/bin/env python
# This script generates an example HTML form that can directly upload files to
# an S3 bucket at cloudscale.ch.
#
# Based on the following work:
#
# - http://archive.is/fZdSu
# - http://archive.is/NXQno
#
# Requires Python 3 and boto3 to work:
#
# python3 -m venv venv
# source venv/bin/activate
# pip install boto3
#
# To use, export the following variables, then run the script:
#
# export CLOUDSCALE_ACCESS_KEY="***"
# export CLOUDSCALE_SECRET_KEY="***"
# export CLOUDSCALE_BUCKET="my-bucket"
# export CLOUDSCALE_REGION="lpg" # or "rma"
#
# python3 generate-form.py > form.html
#
# The form.html can then be opened in a browser and used to upload files to
# the configured bucket.
#
# To add specific conditions for your use-case, have a look at the
# generate_presigned_post documentation and adjust the code below:
#
# - http://archive.is/1mcfG#S3.Client.generate_presigned_post
#
# Notably, you might want to change the ExpiresIn default value of 1h (3600s),
# if you are not dynamically generating this form.
#
import boto3
import os
import textwrap
ACCESS_KEY = os.environ['CLOUDSCALE_ACCESS_KEY']
SECRET_KEY = os.environ['CLOUDSCALE_SECRET_KEY']
BUCKET = os.environ['CLOUDSCALE_BUCKET']
REGION = os.environ['CLOUDSCALE_REGION']
session = boto3.session.Session(
aws_access_key_id=ACCESS_KEY,
aws_secret_access_key=SECRET_KEY,
region_name=REGION)
s3_client = session.client(
service_name='s3',
endpoint_url=f"https://objects.{REGION}.cloudscale.ch",
config=boto3.session.Config(signature_version='s3v4'),)
form_data = s3_client.generate_presigned_post(
Conditions=[
["starts-with", "$Content-Type", ""],
],
Bucket=BUCKET,
Key=r'${filename}',
ExpiresIn=3600)
input_fields = '\n'.join(
f'<input type="hidden" name="{k}" value="{v}" />'
for k, v in form_data['fields'].items())
print(f"""\
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
</head>
<body>
<form action="{form_data['url']}" method="post"
enctype="multipart/form-data">
{textwrap.indent(input_fields, ' ' * 12)}
<input type="file" name="file" /> <br />
<input type="submit" name="submit" value="Upload to object storage" />
</form>
</html>
""")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment