AWS Builder Center
Your S3 Bucket Doesn't Need to Be Public to Share Files

Your S3 Bucket Doesn't Need to Be Public to Share Files

A practical guide to keeping files private in Amazon S3 while still allowing users to download them. Learn how presigned URLs work, how to generate one using the AWS SDK for JavaScript, and what security mistakes to avoid.

Imagine you are building a web application where users can upload profile pictures, invoices, or documents.
You decide to store those files in Amazon S3. The application works, but when you try to display a file in the browser, you get an  AccessDenied  error.
The first solution that might come to mind is making the S3 bucket public.
It seems simple. The file becomes accessible, and the application starts working.
But there is a problem: if the bucket contains private documents, making it public can expose more data than intended.
There is a better approach.
In this article, we'll look at how Amazon S3 presigned URLs allow applications to share private files temporarily without opening the bucket to everyone.

First, understand how S3 access works

Amazon S3 is an object storage service. Files are stored as objects inside buckets.
By default, S3 objects are private. Access depends on the permissions associated with the requesting identity and the bucket or object policies.
For a typical application, there are two different requirements:
  • Some files should be publicly accessible, such as website images or public assets.
  • Other files should only be accessible to authorized users, such as invoices, identity documents, or private reports.
These requirements should not be handled in the same way.
Making an entire bucket public just to display one private file is usually unnecessary.

What is a presigned URL?

A presigned URL is a URL that contains a cryptographic signature authorizing a specific operation on an S3 object for a limited period.
For example, an application can generate a URL that allows a user to download a particular PDF file for five minutes.
The user does not need AWS credentials.
The URL is generated using credentials that already have the required permissions. Amazon S3 verifies the signature when the request is made.
A presigned URL can be used for operations such as:
  • Downloading an object using  GET 
  • Uploading an object using  PUT 
The permissions of the URL are limited by the permissions of the identity that generated it.
One important thing to remember: anyone who possesses a valid presigned URL can use it for the authorized operation until it expires. Treat it like a temporary bearer token.

How the workflow works

Let's consider a simple application where a logged-in user wants to download a private invoice.
The workflow looks like this:
  1. The user logs in to the application.
  1. The user requests access to an invoice.
  1. The backend verifies that the user is authorized to access that invoice.
  1. The backend generates a presigned URL for the specific S3 object.
  1. The backend returns the URL to the frontend.
  1. The browser uses the URL to download the file directly from S3.
The backend does not need to proxy the entire file through itself.
This can be useful when dealing with larger files because S3 handles the file transfer.

Generating a presigned URL using JavaScript

Let's look at a practical example using the AWS SDK for JavaScript v3.
First, install the required packages:
npm install @aws-sdk/client-s3 @aws-sdk/s3-request-presigner
Now create a function to generate a temporary download URL.
import { S3Client, GetObjectCommand } from "@aws-sdk/client-s3";
import { getSignedUrl } from "@aws-sdk/s3-request-presigner";

const s3 = new S3Client({
region: process.env.AWS_REGION,
});

export async function generateDownloadUrl() {
const command = new GetObjectCommand({
Bucket: process.env.S3_BUCKET_NAME,
Key: "private/invoices/invoice-123.pdf",
});

const url = await getSignedUrl(s3, command, {
expiresIn: 300,
});

return url;
}
Here is what the important parts do:
Bucket: Identifies the S3 bucket where the file is stored.
Key: Identifies the exact object. In this example, it is a PDF invoice.
GetObjectCommand: Specifies that the URL is intended for downloading an object.
expiresIn: 300: Sets the requested expiration to 300 seconds, or five minutes.
The SDK uses the configured AWS credentials to generate the signature.
For a production application, credentials should come from an appropriate AWS identity, such as an IAM role attached to the compute environment. Avoid hardcoding access keys into your application.
Also, this function should be called only after your backend has verified that the requesting user is allowed to access the invoice.
The code above generates the URL. It does not implement authentication or authorization.

Why this is better than making the bucket public

Consider the following comparison:
Public S3 object
Presigned URL
Anyone can access the object if public access is allowed
Access is time-limited
No temporary signature is needed for public access
URL contains a signature
Suitable for public website assets
Suitable for private files
Access is not restricted to a temporary link
Link expires based on its validity
This does not mean presigned URLs are automatically secure.
If your backend generates a URL for the wrong user, the file can still be exposed. The application must verify ownership and access permissions before generating the URL.

A few security details worth remembering

There are some things that are easy to overlook when implementing presigned URLs.

1. Keep Block Public Access enabled

Amazon S3 provides Block Public Access settings at the bucket and account levels.
For private application data, keeping public access blocked is a sensible starting point.
These settings help prevent accidental public exposure through bucket policies or access control lists.

2. Use short expiration times

A URL that remains valid for a long time has a larger window for misuse if it is shared accidentally.
For sensitive files, use a short expiration time appropriate to the use case.
A five-minute URL is a reasonable example for a user who is downloading a file immediately.

3. Never put AWS credentials in frontend code

Your React application should not contain an AWS secret access key.
The backend should generate the presigned URL and return only that URL to the authorized user.

4. Be careful where URLs are logged

Presigned URLs contain signature information in their query parameters.
Avoid logging complete URLs unnecessarily or exposing them through analytics, error reports, or application logs.

5. Remember that expiration is not one-time access

A presigned URL can generally be reused until it expires.
It is not automatically invalidated after the first download.
Also, the URL cannot remain valid beyond the lifetime of the credentials used to generate it.

When should you use this approach?

Presigned URLs are useful in applications that need to share private files temporarily.
Some examples include:
  • Downloading invoices from a billing application
  • Sharing private documents with authenticated users
  • Uploading files directly from a browser to S3
  • Downloading reports generated by a backend
  • Providing temporary access to user-uploaded content
For public assets such as a public website's logo, a public URL or a CDN may be more appropriate.
The right solution depends on whether the data is intended to be public, private, or temporarily accessible.

Final thoughts

When working with Amazon S3, making a file accessible does not necessarily mean making the bucket public.
Presigned URLs provide a practical way to share private objects without giving users AWS credentials or changing the bucket's public access configuration.
The most important part is not just generating the URL. It is verifying who is requesting it, what object they should access, and how long that access should remain valid.
This is a small implementation detail that can make a meaningful difference in the security of a web application.
If you are building an application that stores user files in S3, it is worth understanding this approach before making your bucket public.

References

Any opinions in this article are those of the individual author and may not reflect the opinions of AWS.
Enjoyed reading this content? Let the author know!

Your likes, comments, shares, and saves help creators reach more builders.

Loading recommendations

Loading article