Skip to content

Instantly share code, notes, and snippets.

@alexdebrie
Created February 1, 2019 01:59
Show Gist options
  • Save alexdebrie/3e8b96217f5aff01227050b17a24e380 to your computer and use it in GitHub Desktop.
Save alexdebrie/3e8b96217f5aff01227050b17a24e380 to your computer and use it in GitHub Desktop.
Using boto3 generate_presigned_post()

Using an S3 presigned POST url.

  1. Copy the generate.py script to your machine.

  2. Update the BUCKET_NAME and KEY_NAME values in the script as needed.

  3. Run python generate.py. It will spit some output like the following:

    $ python3 generate.py
    curl command:
    
    curl -v -F key=cat.jpg \
      -F AWSAccessKeyId=AKIANOTAKEY \
      -F policy=eyKJLJSDFLKJASDLFJLAKSDJFLJASDLFAKLNFLNASDJFLAJSDLKFJ== \
      -F signature=4jsdlkjasdljfljsdljf/5A= \
      -F [email protected] \
      https://presigned-post-example.s3.amazonaws.com/
  4. Copy and paste the curl command into your terminal to upload the file.

import boto3
# Change BUCKET_NAME to your bucket name and
# KEY_NAME to the name of a file in the directory where you'll run the curl command.
BUCKET_NAME = 'presigned-post-example'
KEY_NAME = 'cat.jpg'
s3 = boto3.client('s3')
resp = s3.generate_presigned_post(
Bucket=BUCKET_NAME,
Key=KEY_NAME,
)
resp['fields']['file'] = '@{key}'.format(key=KEY_NAME)
form_values = "\n ".join(["-F {key}={value} \\".format(key=key, value=value)
for key, value in resp['fields'].items()])
print('curl command: \n')
print('curl -v {form_values} \n {url}'.format(form_values=form_values, url=resp['url']))
print('')
@MBejjarapu
Copy link

Can we use wildcard for the key name like (/path/*)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment