Skip to content

How to create permanent links for files stored in S3 using JavaScript v3 SDK?

0

I would like to add functionality to create permanent public links for specific files from my app, so other apps could use those links to display content. I don't want to make the whole bucket public.

Currently, I use pre-signed URLs for downloads. But pre-signed URLs are temporary. Is there a way to create permanent pre-signed URL? If not, then is there any other way to generate public access link to the specific file? The generated link should not expose bucket structure.

asked 2 years ago761 views

1 Answer
0

Creating permanent public links for specific files in S3 without making the entire bucket public is possible, but it's important to note that pre-signed URLs are inherently temporary and cannot be made permanent. However, there are alternative approaches you can consider:

  1. Object ACLs (Access Control Lists): You can set individual object ACLs to make specific files publicly accessible. This allows you to create permanent public links without exposing the entire bucket. However, keep in mind that AWS recommends using bucket policies over ACLs when possible.

  2. Bucket Policy with Conditions: You can create a bucket policy that allows public access to specific objects or paths within your bucket. This method gives you more fine-grained control over which objects are accessible.

  3. CloudFront Distribution: You can set up an Amazon CloudFront distribution in front of your S3 bucket. This allows you to create public URLs for specific objects while keeping the bucket itself private. CloudFront can also help with performance and security.

To implement one of these solutions using the AWS SDK for JavaScript v3, you would typically:

  1. Use the S3Client to set object ACLs or update bucket policies.
  2. For CloudFront, you would use the CloudFrontClient to create and manage distributions.

Here's a general approach using object ACLs:

  1. Create an S3Client instance.
  2. Use the PutObjectAclCommand to set the ACL of a specific object to public-read.
  3. Construct the public URL for the object using the bucket name and object key.

Remember that making objects public means anyone with the URL can access them. Ensure you're not exposing sensitive information and that this aligns with your security requirements.

If you need more control or security, consider using CloudFront with signed URLs or cookies, which allows you to create time-limited or IP-restricted access to your content while keeping your S3 bucket private.

Regardless of the method you choose, make sure to carefully consider the security implications and use the principle of least privilege when granting access to your S3 objects.
Sources
Amazon S3 examples using SDK for JavaScript (v3) - AWS SDK for JavaScript
Amazon S3 examples using SDK for JavaScript (v3) - AWS SDK Code Examples
Amazon S3 considerations - AWS SDK for JavaScript
AWS SDK for JavaScript v3

answered 2 years ago

  • The putting ACL for the specific file seems the most convenient approach, however the public link exposes object key and people can try to manipulate the key name to potentially discover other publicly open files from other users. I would like to see the links that are hard to guess.

You are not logged in. Log in to post an answer.

A good answer clearly answers the question and provides constructive feedback and encourages professional growth in the question asker.