How to serve premium/private content on your site?

How to serve premium/private content on your site?

A lot of online companies (for example: Netflix, Udemy etc ) distribute contents over internet that is accessible only to the members of the site or who have subscribed or paid a premium. Today in this post we see how you can achieve this for your website using Amazon Web Services. We will see how you can securely serve private content to your users from AWS S3 bucket using S3 Presigned URLs

What are Presigned URLs?

A Presigned URL is a URL that provides limited permission and time to make a request. Anyone who receives the presigned URL can then access the object. For example, if you have a file in your bucket and both the bucket and the object are private, you can share the file with others by generating a presigned URL.

Some important points on Presigned URLs

  • The creator of Presigned URL should have access to object for URL to work, otherwise URL wont work.
  • You can create a presigned URL that's are not usable or doesn’t work.
  • It doesn’t cost anything to create Presigned URLs.
  • You can set expiration time on the URLs
  • If you created a presigned URL using a temporary token, then the URL expires when the token expires, even if the URL was created with a later expiration time
  • You can revoke the URL by removing the permissions to access the object from the user created the URL.

How to create Presigned URL?

When you create a presigned URL for your object, you must provide your security credentials, specify a bucket name, an object key, specify method and expiration time.

In Python, using Boto3, you can use generate_presigned_url method to generate the URL

response = s3_client.generate_presigned_url('get_object',
            Params={'Bucket': bucket_name,
            'Key': object_name},

The response object will contain the URL which will look similar to this

You can use this URL in the browser and access the object! Simple.. isn't it?

Lets see it in Action

We are going to implement this on top of previous two posts, in the first post, we implemented custom identity broker(signup/login), using AWS KMS to encrypt decryt password. In second post, we used AWS STS to AssumeRole to read bucket files names and now we will extend that to use presigned url. Download code for second post from and modify it. Here is how overall process looks like free cloud storage on s3, presigned URL diagram

Open file from the Lamdba folder and lets add function to generate presigned URL. Call this function from getFilesList function.

def getSignedUrl(key,s3_client):
    KeyUrl = {}

    response = s3_client.generate_presigned_url('get_object',
                        Params={'Bucket': os.environ['filesBucket'],
                        'Key': key},
    KeyUrl[key] = response
    return KeyUrl

# Returns list of files from bucket using STS    
def getFilesList():
    sts_client = boto3.client('sts')

    # Call the assume_role method of the STSConnection object and pass the role
    # ARN and a role session name.

    # From the response that contains the assumed role, get the temporary 
    # credentials that can be used to make subsequent API calls

    # Use the temporary credentials that AssumeRole returns to make a 
    # connection to Amazon S3  
    s3_client = boto3.client('s3',aws_access_key_id=credentials['AccessKeyId'],
    bucket = s3_resource.Bucket(os.environ['filesBucket'])
    for obj in bucket.objects.all():
    return files

We are using the temporary credentials obtained from AssumeRole to generate Presigned URL in getSignedUrl function. The getFilesList functions returns list of file names and presigned URL.

Now, modify showFiles.html, to iterate over response and create links for files

<link rel="stylesheet" href="" integrity="sha512-8bHTC73gkZ7rZ7vpqUQThUDhqcNFyYi2xgDgPDHc+GXVGHXq+xPjynxIopALmOPqzo9JZj0k6OqqewdGO3EsrQ==" crossorigin="anonymous" />

<script src=""></script>

<div class="ui raised very text container">
<h1 class="ui header">File Access System</h1>
<i class="folder open icon"></i></i><div class="ui label">Files</div>
<div id="files" >Loading..</div>

fetch("", {
  credentials: 'include'
  .then(response => response.text())
  .then((body) => {
    var files="";

    var obj = JSON.parse(body)
    for (i = 0; i < obj.length; i++) {
            var o = obj[i]

            for(x in o){
                files =  files+ "<i class='file alternate outline icon'><a href='"+o[x]+"' target='_blank'>&nbsp;&nbsp;"+x+"</a>"
    document.getElementById("files").innerHTML= files
  .catch(function(error) {


Do SAM build and Deploy. Free cloud storage Presigned URL

Modify login.html, signup.html and showFiles.html to update the api urls from cloudformation outputs. Upload these files to the bucket stsexamplebucket or the bucket you created. Keep these files Public

You can find the code here:
AWS S3 Presigned Url Card


private content serve using Presigned URL

Let me know if you have any questions or comments!