AWS S3 Presigned URL Upload Tutorial in Python

AWS S3 Presigned URL Upload Tutorial in Python

How to generate pre-signed url, and how to upload file using the generated url using python

What are the signed URL?

Pre-signed URLs are useful if you want your user/customer to be able to upload a specific object to your bucket, but you don't require them to have AWS security credentials or permissions. When you create a pre-signed URL, you must provide your security credentials and then specify a bucket name, an object key, an HTTP method (PUT for uploading objects), and an expiration date and time. The pre-signed URLs are valid only for the specified duration.

A user can either READ the object or WRITE an object or update existing one, A pre-signed URL uses three parameters to limit the access to the user:

  • Bucket: The bucket that the object is in (or will be in)

  • Key: The name of the object

  • Expires: The amount of time that the URL is valid

Why pre-signed URL ?

The presigned URLs are useful if you want your user/customer to be able to upload a specific object to your bucket, but you don't require them to have AWS security credentials or permissions.

In this article, i will provide a very pure example of generating AWS S3 presigned URL with Python.

How to generate presigned URL for uploading an object?

    s3_client = boto3.client('s3',config=Config(signature_version='s3v4'))

def generate_upload_object_presignedurl(bucket, keypath, ExpiresIn=3600):
        presignedurl=s3_client.generate_presigned_url('put_object', Params={ 
                                              'Bucket': bucket, 'Key': keypath,},
                                                   ExpiresIn=ExpiresIn, HttpMethod='PUT')
    return presignedurl

How to upload using the presigned URL that we generated previously ?

How to get back the object using the presingurl GetObject?

def generate_presigned_url(bucket,key_path,ExpiresIn=3600):
    s3_client.generate_presigned_url(ClientMethod='get_object',
                                                             Params={'Bucket': bucket,
                                                                     'Key': key_path},
                                                             ExpiresIn=ExpiresIn)
        return presignedurl

Further Reading