Skip to content

Latest commit

 

History

History
41 lines (27 loc) · 1.02 KB

CloudFrontSignedURL.md

File metadata and controls

41 lines (27 loc) · 1.02 KB

CloudFront Signed URLs with Node.js

Generate RSA Key Pair

https://docs.aws.amazon.com/AmazonCloudFront/latest/DeveloperGuide/private-content-trusted-signers.html

Install OpenSSL on your machine and generate the keypairs

openssl genrsa -out private_key.pem 2048
openssl rsa -pubout -in private_key.pem -out public_key.pem

Sign URLs

https://docs.aws.amazon.com/AWSJavaScriptSDK/v3/latest/modules/_aws_sdk_cloudfront_signer.html#getsignedurl

Install and import the cloudfront signer

npm i @aws-sdk/cloudfront-signer
import { getSignedUrl } from "@aws-sdk/cloudfront-signer"

Sign the urls before sending them to the browser

const signedUrl = getSignedUrl({
  keyPairId: process.env.CLOUDFRONT_KEYPAIR_ID,
  privateKey: process.env.CLOUDFRONT_PRIVATE_KEY,
  url: url,
  dateLessThan: new Date( Date.now() + (1000 /*sec*/ * 60))
})