Last active
December 1, 2022 02:27
-
-
Save beingadityak/645fc9d8573480836d4e774b967d6632 to your computer and use it in GitHub Desktop.
Generate CloudFront Signed URLs for your private content
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env bash | |
openssl genrsa -out private_key.pem 2048 # generate an RSA private key of 2048 bits | |
openssl rsa -pubout -in private_key.pem -out public_key.pem # Generate the public key from the private key |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const AWS = require('aws-sdk'); | |
const fs = require('fs'); | |
const path = require('path'); | |
const distUrl = 'YOUR_DISTRIBUTION_HERE'; | |
const s3Key = 'YOUR_S3_OBJECT_KEY_HERE'; | |
const cfAccessKeyId = 'YOUR_PUBLIC_KEY_ID_HERE'; | |
let cfPrivateKey = fs.readFileSync(path.join(__dirname, 'private_key.pem')); | |
const signer = new AWS.CloudFront.Signer(cfAccessKeyId, cfPrivateKey) | |
const thirtySeconds = 30 * 1000; // 30 seconds | |
let cfObjectUrl = 'https://' + distUrl + '/' + s3Key; | |
const signedUrl = signer.getSignedUrl({ | |
url: cfObjectUrl, | |
expires: Math.floor((Date.now() + thirtySeconds)/1000) | |
}) | |
console.log(signedUrl) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
YOUR_PUBLIC_KEY_HERE
is wrong, it should sayYOUR_PUBLIC_KEY_ID_HERE
: