Skip to content

Instantly share code, notes, and snippets.

@apal21
Created February 5, 2019 09:14
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save apal21/bc845297cf47337e29f9aca6f726d014 to your computer and use it in GitHub Desktop.
Save apal21/bc845297cf47337e29f9aca6f726d014 to your computer and use it in GitHub Desktop.
Example to generate Signed URLs in CloudFront using Custom Policy.
const AWS = require('aws-sdk');
// Try to use process.env.PRIVATE_KEY instead of exposing your key
const privateKey = `-----BEGIN RSA PRIVATE KEY-----
XXXXXXXX
XXXXXXXX
XXXXXXXX
XXXXXXXX
-----END RSA PRIVATE KEY-----`
const cloudFront = new AWS.CloudFront.Signer('PUBLIC_ACCESS_KEY', privateKey);
const policy = JSON.stringify({
"Statement": [
{
"Resource": "https://abcdefghijklmn.cloudfront.net/test/a.txt",
"Condition": {
"DateLessThan": {
"AWS:EpochTime": Math.floor((new Date()).getTime() / 1000) + (60 * 60 * 1) // Current Time in UTC + time in seconds, (60 * 60 * 1 = 1 hour)
}
}
}
]
});
cloudFront.getSignedUrl({
policy,
}, (err, url) => {
if (err) throw err;
console.log(url);
});
@immma
Copy link

immma commented Mar 17, 2021

Hi bro @apal21,

Thanks for sharing the code. Anyway, I got error on getSignedUrl() and the error message is

TypeError: Cannot read property 'split' of undefined
    at determineScheme (C:\Users\ardi\Documents\Code\NodeJS\nodejs-for-testing\node_modules\aws-sdk\lib\cloudfront\signer.js:52:21)
    at getResource (C:\Users\ardi\Documents\Code\NodeJS\nodejs-for-testing\node_modules\aws-sdk\lib\cloudfront\signer.js:66:13)
    at Signer.getSignedUrl (C:\Users\ardi\Documents\Code\NodeJS\nodejs-for-testing\node_modules\aws-sdk\lib\cloudfront\signer.js:178:28)
    at Object.<anonymous> (C:\Users\ardi\Documents\Code\NodeJS\nodejs-for-testing\cloudfront-signed-url.js:59:8)

My code is pretty same with yours,

Thanks in advance.

@apal21
Copy link
Author

apal21 commented Mar 22, 2021

Hi bro @apal21,

Thanks for sharing the code. Anyway, I got error on getSignedUrl() and the error message is

TypeError: Cannot read property 'split' of undefined
    at determineScheme (C:\Users\ardi\Documents\Code\NodeJS\nodejs-for-testing\node_modules\aws-sdk\lib\cloudfront\signer.js:52:21)
    at getResource (C:\Users\ardi\Documents\Code\NodeJS\nodejs-for-testing\node_modules\aws-sdk\lib\cloudfront\signer.js:66:13)
    at Signer.getSignedUrl (C:\Users\ardi\Documents\Code\NodeJS\nodejs-for-testing\node_modules\aws-sdk\lib\cloudfront\signer.js:178:28)
    at Object.<anonymous> (C:\Users\ardi\Documents\Code\NodeJS\nodejs-for-testing\cloudfront-signed-url.js:59:8)

My code is pretty same with yours,

Thanks in advance.

Hey @immma,

Can't tell anything from the information you gave here. Create a gist or something of the code which is failing so that I can look into it.

@jordizle
Copy link

@immma You have to give a url param to prevent this error.

@jishudo
Copy link

jishudo commented May 11, 2023

same issue TypeError: Cannot read property 'split' of undefined

@anubhab-parallel-reality

It is caused by this line in the SDK. It seems it is necessary to provide the Resource URL as url property to the options argument.

//  ... same as gist
cloudFront.getSignedUrl({
  url: <resource_url_here_as_string>
  policy,
}, (err, url) => {
  if (err) throw err;
  console.log(url);
});

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment