Are you aware of Pre-Signed URLs in AWS S3?

Today we want to tell you a story: recently we recently met with a friend and between many discussions started with “remember when”, we had also discussed technical problems. He detailed us an interesting solution done in order to give temporary access to some files he has stored somewhere.

We know and we admit we are a little biased with AWS in general, but at that moment a simple question had arised: why did you do that instead of using pre-signed URLs from Amazon S3?

We hope our friend is the only one person who did not know about this feature, but we think there could be other that can benefit from this story.

With this feature, you can do 2 things:

  • Generate an object marker in S3 and let others to add content to that object for a given amount of time – example here
// Set the URL to expire after 10 minutes.
Date expiration = new Date();
long expTimeMillis = expiration.getTime();
expTimeMillis += 1000 * 60 * 10;
expiration.setTime(expTimeMillis);

// Generate the pre-signed URL.
GeneratePresignedUrlRequest generatePresignedUrlRequest = new GeneratePresignedUrlRequest(bucketName, objectKey)
           .withMethod(HttpMethod.PUT)
           .withExpiration(expiration);
URL url = s3Client.generatePresignedUrl(generatePresignedUrlRequest);
            
// Create the connection and use it to upload the new object using the pre-signed URL.
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setDoOutput(true);
connection.setRequestMethod("PUT");
OutputStreamWriter out = new OutputStreamWriter(connection.getOutputStream());
out.write("Object content uploaded as an object using a presigned URL.");
out.close();
  • Generate an URL that gives access to an object stored in S3 for a defined period of time – example here
// Set the pre-signed URL to expire after 10 minutes.
Date expiration = new Date();
long expTimeMillis = expiration.getTime();
expTimeMillis += 1000 * 60 * 10;
expiration.setTime(expTimeMillis);

// Generate the presigned URL.
GeneratePresignedUrlRequest generatePresignedUrlRequest = new GeneratePresignedUrlRequest(bucketName, objectKey)
          .withMethod(HttpMethod.GET)
          .withExpiration(expiration);
URL url = s3Client.generatePresignedUrl(generatePresignedUrlRequest);
    
System.out.println("URL (valid for 10 minutes): " + url.toString());

After that, you can share that URL with anyone and he/she can access data without needing any credential.

Keep in mind one aspect: anyone can create an URL, but the permissions to access that object are verified only when the object is actually used. More concrete: you can create an URL for an object that doesn’t exist or from a bucket that doesn’t exist. Or if you delete the IAM user that created that URL, then the object cannot be accessed.

As always, we invite you to check the documentation to learn more about that. Also, if you know anyone that could use this info, please share this article.

Happy cloud computing!

9 thoughts on “Are you aware of Pre-Signed URLs in AWS S3?

  1. It’s a shame you don’t have a donate button! I’d certainly donate to this excellent blog! I guess for now i’ll settle for bookmarking and adding your RSS feed to my Google account. I look forward to fresh updates and will share this blog with my Facebook group. Talk soon!

  2. Thank you so much for providing individuals with remarkably memorable opportunity to read articles and blog posts from this website. It really is very pleasant plus jam-packed with fun for me personally and my office peers to search your web site a minimum of thrice weekly to learn the new things you have got. Not to mention, I’m also usually astounded with all the beautiful tricks you give. Certain 2 tips on this page are rather the most efficient I’ve had.

  3. I am only writing to make you know of the notable discovery my wife’s girl went through reading through yuor web blog. She came to understand a lot of pieces, most notably what it is like to possess an ideal giving spirit to make the mediocre ones easily master various complicated issues. You really surpassed readers’ expected results. I appreciate you for offering such powerful, healthy, revealing and also fun guidance on that topic to Ethel.

  4. Thanks a lot for giving everyone a very special opportunity to read articles and blog posts from this site. It really is very terrific plus jam-packed with amusement for me personally and my office peers to search your site the equivalent of three times in 7 days to find out the latest issues you will have. Not to mention, I am just certainly impressed with all the awesome tactics served by you. Selected 2 tips in this article are honestly the simplest we have ever had.

Comments are closed.