Skill 2.1.4

Use presigned URLs to provide secure, time-limited access (for example, by using Amazon S3 and Amazon CloudFront).

Key Concepts

An S3 presigned URL is created in code, for example with boto3's generate_presigned_url or aws s3 presign, for an operation such as GetObject or PutObject. The URL carries a Signature Version 4 signature made with the signer's credentials, so it grants only what the signer is allowed to do, and S3 checks the signer's permissions when the URL is used. The expiry can be up to seven days for credentials of an IAM user, but a URL signed with temporary credentials, such as a Lambda execution role's, stops working when those credentials expire, even if its own expiry time is later.

A typical upload flow has the browser ask the application for a presigned PUT URL, then upload the file directly to S3, so the file never passes through the application's servers or API Gateway's payload limit. A presigned POST adds policy conditions, such as a maximum content length or a required key prefix, which suits HTML form uploads. Browser uploads also need a CORS configuration on the bucket. Bucket policies can restrict the use of presigned URLs further, for example with the s3:signatureAge condition key.

CloudFront signed URLs and signed cookies protect content that is delivered through a CloudFront distribution. The application signs a policy with a private key whose public key belongs to a trusted key group on the cache behaviour. A canned policy sets only an expiry time; a custom policy can also set a start time, an IP address range and a wildcard resource path. Signed URLs suit a single file, while signed cookies suit access to many files, such as the segments of a streaming video or a members-only section of a site.

The two are combined with origin access control (OAC): the bucket policy allows only the CloudFront distribution to read the bucket, and CloudFront allows only viewers with a valid signature. Choose S3 presigned URLs for direct, short-lived uploads and downloads, and CloudFront signed URLs or cookies when content must also be cached at edge locations.