Ravindra BagaleCourses & study guides

14. Amazon S3: Buckets, Objects, Policies and Presigned URLs

14.6 Presigned URLs: Temporary Private Access

Bucket private aahe, pan user la ek video 10 minutes sathi baghu dyaycha aahe – kasa? Presigned URL. Server aaplya credentials ne ek URL "sign" karto; tya URL madhe expiry time aani signature asto. Expiry nantar URL band.

aws s3 presign s3://ravindra-demo-files-pune/site/index.html --expires-in 600   # 600 seconds

The output looks like this (shortened):

https://ravindra-demo-files-pune.s3.ap-south-1.amazonaws.com/site/index.html
  ?X-Amz-Algorithm=AWS4-HMAC-SHA256&X-Amz-Credential=ASIA.../ap-south-1/s3/aws4_request
  &X-Amz-Date=20260927T043000Z&X-Amz-Expires=600&X-Amz-Signature=5f2c...

In PHP (AWS SDK for PHP – we use exactly this in the project chapter):

<?php
require __DIR__ . '/vendor/autoload.php';
$s3  = new Aws\S3\S3Client(['region' => 'ap-south-1']);   // credentials come from the IAM role
$cmd = $s3->getCommand('GetObject', ['Bucket' => 'ravindra-reels-media-pune', 'Key' => 'videos/a1.mp4']);
$url = (string) $s3->createPresignedRequest($cmd, '+15 minutes')->getUri();
echo $url;
Presigned URL fact Why it matters
Works for GET (download) and PUT (upload) Browsers can upload directly to S3 without your credentials
Signed with the signer's permissions If the role cannot read the object, the URL fails too
Anyone holding the URL can use it until expiry Keep expiry short; don't log or share them
Role-based (temporary) credentials The URL also stops working when those credentials expire

Why this matters for security

Presigned URLs let you keep the bucket 100% private and still serve files to logged-in users – the right design for user uploads. But they are bearer tokens (वाहक टोकन): whoever has the link has access. Short expiry, HTTPS only, and never presigning keys that come straight from user input without checks (or a user could ask for someone else's file – an IDOR bug).

Ravindra Bagale's Tip

Many students give a presigned URL a 7-day expiry "for convenience", and the URL ends up circulating on WhatsApp! For a video feed, 10–20 minutes is enough – a new URL is created when the page reloads. And if you run aws s3 presign on your laptop and get a 403, check the region – sign in the bucket's own region.

Lab

Upload a private image. Confirm its plain object URL returns AccessDenied in the browser. Generate a presigned URL with --expires-in 60, open it immediately, then open it again after two minutes and observe the expiry error.