Unlock Seamless File Uploads with Pre-Signed S3 URLs: A Scalable and Secure Alternative to API…

SaqibSaqib
5 min read

Handling file uploads in applications can quickly become a bottleneck, especially when dealing with large files. While AWS API Gateway is a great tool for routing and processing HTTP requests, it has a strict 10 MB payload limit for REST APIs.

For businesses dealing with images, videos, or documents that exceed this size, API Gateway can feel restrictive. So, how do you efficiently and securely handle large file uploads?

The answer lies in Pre-Signed S3 URLs, a lightweight, scalable, and secure solution for direct file uploads to Amazon S3. Let’s explore how pre-signed URLs can simplify your architecture, reduce costs, and boost security — all while keeping the process developer-friendly.

Why Use Pre-Signed S3 URLs Instead of API Gateway?

Imagine you’re building a file-sharing platform or a system for uploading high-resolution images. With API Gateway:

  • File data flows through your backend, increasing latency and server load.
  • File size is capped at 10 MB, requiring you to split larger files or implement additional workarounds.

Pre-Signed S3 URLs allow your users to upload and download files directly to/from Amazon S3, completely bypassing your backend. This not only removes the size limitations but also makes the system faster, more cost-efficient, and easier to scale.

What Are Pre-Signed S3 URLs?

A pre-signed S3 URL is a temporary, secure link that allows users to upload or download files directly to/from an S3 bucket. This URL includes:

  1. The S3 object key (file identifier).
  2. Permissions for the specific operation (e.g., upload or download).
  3. An expiration timestamp to limit how long the URL is valid.

Benefits of Pre-Signed S3 URLs

1. Eliminate File Size Restrictions

Pre-signed URLs bypass API Gateway entirely, allowing files of any size (up to 5 TB, the S3 object limit) to be uploaded or downloaded. No more splitting files or dealing with upload size errors.

2. Boost Performance and Scalability

By enabling direct transfers between the client and S3, pre-signed URLs:

  • Eliminate the need for your backend to proxy file data.
  • Reduce latency for users.
  • Free up backend resources, making your application more scalable.

3. Enhance Security

Pre-signed URLs are inherently secure because:

  • They expire automatically after a set duration, reducing the risk of misuse.
  • Access is restricted to a specific file and operation (e.g., upload or download).
  • You can enforce server-side encryption on all uploaded files, ensuring data remains protected at rest.

4. Cost Efficiency

Processing large file uploads through your backend consumes compute and networking resources. With pre-signed URLs, you offload this to S3, significantly reducing AWS infrastructure costs.

5. Simple Implementation

No new infrastructure is needed — just generate a pre-signed URL in your backend and let the client handle the rest. It’s a lightweight solution with minimal setup.

How It Works: Uploading and Downloading with Pre-Signed URLs

File Upload Workflow

  1. Generate the Pre-Signed PUT URL

  2. Your backend generates a pre-signed URL using the S3 SDK, specifying the object key, permissions, and expiration time.

  3. This URL is sent to the client.

2. Client Uploads File

  • The client uploads the file directly to S3 using the URL.
  • Since the URL includes all necessary permissions, no additional authentication is needed.

3. Validation and Post-Processing (Optional)

  • If needed, the backend can validate the uploaded file (e.g., file type, size) or process it further.

File Download Workflow

  1. Generate the Pre-Signed GET URL

  2. Your backend generates a pre-signed URL for the requested file and sends it to the client.

2. Client Downloads File

  • The client uses the URL to download the file directly from S3.

Security Best Practices for Pre-Signed URLs

While pre-signed URLs simplify file uploads, security is paramount. Here’s how you can ensure they remain secure:

  1. Set Expiration Times

  2. Always set a short expiration time (e.g., 15 minutes). The shorter the lifespan of the URL, the lower the risk of unauthorized use.

2. Use HTTPS

  • Always use HTTPS to protect pre-signed URLs during transmission.

3. Enforce Server-Side Encryption

  • Configure your S3 bucket to require server-side encryption (e.g., AES-256) for all uploaded files.

4. Restrict Bucket Permissions

  • Ensure your S3 bucket’s policy only allows uploads/downloads via pre-signed URLs.

5. Scan Uploaded Files

  • For added security, upload files to a temporary bucket, scan them for viruses or malware, and move validated files to the final destination bucket.

How to Generate Pre-Signed URLs

Here’s a simple example in Java to generate a pre-signed PUT URL for file uploads:

import software.amazon.awssdk.services.s3.S3Client;
import software.amazon.awssdk.services.s3.model.GeneratePresignedUrlRequest;
import java.time.Duration;

public class PreSignedUrlExample {

public String generatePresignedUrl(String bucketName, String objectKey) {
S3Client s3 \= S3Client.create();

GeneratePresignedUrlRequest request \= GeneratePresignedUrlRequest.builder()
.bucket(bucketName)
.key(objectKey)
.method(HttpMethod.PUT)
.expiration(Duration.ofMinutes(15)) // URL valid for 15 minutes
.build();

return s3.utilities().getPresignedUrl(request).toString();
}
}

Challenges and How to Overcome Them

  1. File Validation — Pre-signed URLs don’t allow server-side validation during upload. Mitigate this by validating files after they are uploaded or by using a temporary S3 bucket.
  2. Encryption Management — If custom encryption is needed, implement client-side encryption before uploading files or rely on S3’s built-in server-side encryption.
  3. Virus Scanning — Use AWS Lambda to automatically scan files after upload, ensuring they’re safe before making them available for download.

When Should You Use Pre-Signed URLs?

  • Require large file uploads/downloads.
  • Need to reduce backend load and improve latency.
  • Require temporary access control for files.
  • Want a lightweight, cost-effective solution without additional infrastructure overhead.

Conclusion

Pre-signed S3 URLs are a powerful tool for handling file uploads and downloads efficiently and securely. By allowing clients to interact directly with S3, you can eliminate size restrictions, reduce latency, and cut infrastructure costs — all while keeping your application scalable and secure.

If your application handles large files or needs temporary file access, pre-signed URLs are a practical and robust solution worth considering. Empower your architecture with the scalability of S3 and simplify your workflow today!

#AWS #S3 #PreSignedURLs

0
Subscribe to my newsletter

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

Written by

Saqib
Saqib