Replies: 5 comments 2 replies
-
how is there nothing happening on this topic? |
Beta Was this translation helpful? Give feedback.
-
Hi @PWadeDev @idwalkill, Can you try this? import { getSignedCookies } from "@aws-sdk/cloudfront-signer"; // ESM
// const { getSignedCookies } = require("@aws-sdk/cloudfront-signer"); // CJS
const cloudfrontDistributionDomain = "https://d111111abcdef8.cloudfront.net";
const s3ObjectKey = "private-content/private.jpeg";
const url = `${cloudfrontDistributionDomain}/${s3ObjectKey}`;
const privateKey = "CONTENTS-OF-PRIVATE-KEY";
const keyPairId = "PUBLIC-KEY-ID-OF-CLOUDFRONT-KEY-PAIR";
const dateLessThan = "2022-01-01";
+const policy = {
+ 'Statement': [{
+ 'Resource': cloudfrontDistributionDomain + '/*',
+ 'Condition': {
+ 'DateLessThan': {'AWS:EpochTime': expiry}
+ }
+ }]
+};
const cookies = getSignedCookies({
url,
keyPairId,
dateLessThan,
privateKey,
+ policy
});
|
Beta Was this translation helpful? Give feedback.
-
Apologies if the comment didn't make sense, I'm just going off of documentation and general understanding of the package. Since replicating this behavior requires setting up a distribution and resource it's not so straight forward to debug. I'll need some more time to do a deep dive to what seems to be the issue. Thanks, |
Beta Was this translation helpful? Give feedback.
-
@idwalkill This is working for me. I'm on import { getSignedCookies } from "@aws-sdk/cloudfront-signer";
import axios from "axios";
const imagePath = "https://<distribution-domain-name>/f59fa716-6fd4-45c0-9b15-3eb9a27905c9/*"
const reqImagePath = "https://<distribution-domain-name>/f59fa716-6fd4-45c0-9b15-3eb9a27905c9/assets/ef7841d9-8099-483c-be9c-df04a0703178/images/0/header.jpg"
const shouldFailImagePath = "https://<distribution-domain-name>/0fa13f3a-137b-4ab9-94f0-d514c23da606/assets/549987c6-a86a-4534-b827-ffb76df78b98/images/0/convertedOriginal.jpg"
const privateKey = process.env.CDN_PRIVATE_KEY || ''
const cdnPubKeyId = process.env.CDN_PUB_KEY_ID || ''
const expiration = Math.round(Number(new Date(Date.now() + 1000 * 60 * 60))/1000)
const policy = {
"Statement": [
{
"Resource": imagePath,
"Condition": {
"DateLessThan": {
"AWS:EpochTime": expiration
}
}
}
]
}
const cookies = getSignedCookies(
{
url: imagePath,
privateKey,
keyPairId: cdnPubKeyId,
policy: JSON.stringify(policy)
}
)
const cookiesClause = Object.keys(cookies).map(key => `${key}=${cookies[key]}`).join(';')
axios.get(reqImagePath, {
headers: {
'Cookie': cookiesClause
}
}).then(response => {
console.log('first request:', response.status)
axios.get(shouldFailImagePath, {
headers: {
'Cookie': cookiesClause
}
}).then(response => {
console.log('second request:', response.status)
}).catch(error => {
console.log('second request:', error.response.status)
})
}).catch(error => {
console.log('first request: failed', error.response.status)
}) output: first request: 200
second request: 403 |
Beta Was this translation helpful? Give feedback.
-
To make it works, follow the v3 documentation :
My working exemple: async function getSignedCloudfrontCookies(url, ttl = 300) {
const policy = {
Statement: [{
Resource: url,
Condition: {
DateLessThan: {
"AWS:EpochTime": Math.round(new Date().getTime() / 1000 + ttl), // time in seconds
},
},
}]
};
return getSignedCookies({
keyPairId: CLOUDFRONT_PUBLIC_KEY_ID,
privateKey: CLOUDFRONT_PRIVATE_KEY,
policy: JSON.stringify(policy)
});
}
const cfCookies = await getSignedCloudfrontCookies('https://xxxx.cloudfront.net/private/*.zip');
// Set Cookies.... |
Beta Was this translation helpful? Give feedback.
-
Hello ,
I'm trying to move to v3 and I don't know how to create a cloudfront wildcard url.
It's working correctly in v2.
The documentation is only shows the non wildcard solution.
Can anyone help me migrate please if possible
Beta Was this translation helpful? Give feedback.
All reactions