Amazon S3 – Pre-Signed URLs: My Experience Making File Sharing Easier

Gedion DanielGedion Daniel
5 min read

When I first started working with Amazon S3, I knew it was a great way to store and manage files in the cloud. But I quickly ran into a challenge: how could I share a file with someone securely without opening up my entire bucket? After some searching, I found out about Pre-Signed URLs, and they’ve made my life so much easier.

Here’s a bit about what they are, how they work, and how I’ve used them in my projects. Hopefully, this will help you get started with them too!


What Are Pre-Signed URLs?

A Pre-Signed URL is a unique, temporary link that gives access to a specific file (or “object”) in an S3 bucket. When you create a Pre-Signed URL, you’re essentially giving permission to access that file, but only for a set time. This has been incredibly useful in my projects, where I often need to share files or let users upload documents without permanently making everything public.

In simple terms, think of it like a temporary key that opens the door to a specific file, but the key automatically disappears after a set period.


How I First Used Pre-Signed URLs

My first experience with Pre-Signed URLs was in a project where users needed to download PDFs. I wanted to ensure these files weren’t accessible forever or by just anyone. This is where Pre-Signed URLs saved me a lot of hassle.

Instead of setting up complicated access controls for every user, I could create a link that worked for only a few hours. This was perfect for securely sharing files without over-complicating permissions.


How Pre-Signed URLs Work in Practice

Setting up a Pre-Signed URL is simple once you get the hang of it. Here’s the process I followed:

  1. Generate the URL: I specified which file in my S3 bucket I wanted to share. For me, it was often reports or images.

  2. Set the Expiration: I usually set my URLs to expire after 24 hours, but you can choose any time limit.

  3. Use the URL in My Application: I could pass the link directly to users, who could click it to access the file. After the time was up, the link no longer worked.

The best part? I could generate these links programmatically with code, which made it easy to integrate them directly into my app. Here’s a basic example in Python, using the boto3 library:

pythonCopy codeimport boto3

s3_client = boto3.client('s3')

bucket_name = 'my-bucket'
object_name = 'my-file.pdf'
expiration = 86400  # 24 hours

url = s3_client.generate_presigned_url('get_object',
                                        Params={'Bucket': bucket_name,
                                                'Key': object_name},
                                        ExpiresIn=expiration)

print("Pre-Signed URL:", url)

With this code, I could print out a secure link to a file in my S3 bucket that would last only 24 hours. The person receiving the link could download the file within that time, and I didn’t have to worry about ongoing access.


Real-Life Use Cases I’ve Worked On

  1. Secure Document Sharing: I worked on a project where users needed to download confidential reports. Using Pre-Signed URLs, I could control access so each report was available only for a limited time. This worked perfectly, as it allowed users to download the report when they needed it without risking it being permanently accessible.

  2. Photo Uploads in an App: In another project, users uploaded photos directly to an S3 bucket. Instead of giving public access, I generated Pre-Signed URLs for each upload. Users could upload files through the URL without needing AWS credentials, keeping things secure.

  3. Automatic Expiry for Sensitive Files: In a recent project, I had files that were only relevant for a few hours, like temporary contracts. With Pre-Signed URLs, I could automatically expire access, which meant I didn’t have to worry about deleting files or updating permissions manually.


Why I Recommend Pre-Signed URLs

If you’re working with files in S3, Pre-Signed URLs are incredibly useful. They offer several benefits:

  • Security without Complexity: You don’t have to set up complex user permissions; just generate a URL with a time limit, and the file access is controlled.

  • Flexibility: You can set different expiration times based on the use case—anything from a few minutes to several days.

  • Access Control: Since the URL expires, you don’t have to worry about old links sticking around and causing security issues.

One thing I found helpful is testing the URLs with different expiration times to see what works best for the project. For longer-lasting files, I might set a 24-hour limit, but for others, 1–2 hours might be enough.


Tips from My Experience

  1. Set Clear Expiry Times: Start with shorter expiration times until you find the best fit for your users. Too long, and you might have lingering links; too short, and users might find the links expire before they even get to use them.

  2. Automate Link Generation: If your project allows, automate the Pre-Signed URL generation so that links are created and shared instantly without extra effort.

  3. Experiment with Permissions: You can set URLs to allow upload or download. I found it helpful to test different permissions in my projects to avoid unexpected access issues.


Final Thoughts

Working with Pre-Signed URLs in Amazon S3 has been a game-changer for me. They’re simple to create, flexible, and secure, and they’ve solved the challenge of controlled file sharing in my applications. Whether you need to share files temporarily or allow uploads securely, Pre-Signed URLs are a powerful tool to have in your AWS toolkit.

0
Subscribe to my newsletter

Read articles from Gedion Daniel directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Gedion Daniel
Gedion Daniel

I am a Software Developer from Italy.