I am looking to utilize signed cookies for accessing private content stored on S3 using CloudFront for CDN.
I am struggling to identify the appropriate commands to generate signed cookies in aws-sdk-js-v3. According to the updated SDK documentation, it should resemble the second code snippet below, but I am unable to locate the specific npm package that contains the necessary commands. In the previous version (v2), it was "getSignedCookie" but I am uncertain of its updated equivalent.
Previous version (v2):
import AWS from "aws-sdk";
const CFSigner = new AWS.CloudFront.Signer(cfPublicKeyId, cfPrivateKey);
const policy = JSON.stringify({
Statement: [
{
Resource: `https://${cfDomain}/images/*`,
Condition: {
DateLessThan: {
"AWS:EpochTime": expireTime,
},
},
},
],
});
const myCookie = CFSigner.getSignedCookie({ policy });
Attempt using v3 SDK
import {
CloudFrontClient,
CloudFrontClientConfig,
} from "@aws-sdk/client-cloudfront";
import { ICantFindAnAppropriateCommandToSignCookies } from "ICantFindAnAppropriateCommandToSignCookies";
async function signMyCookies() {
const config: CloudFrontClientConfig = {
apiVersion: "2015-12-08",
credentials: {
accessKeyId: process.env.SC_ADMIN_ACCESS_KEY_ID,
secretAccessKey: process.env.SC_ADMIN_SECRET_ACCESS_KEY,
},
region: "us-east-01",
};
const cfClient = new CloudFrontClient(config);
const cfDomain = process.env.CLOUDFRONT_DOMAIN;
const twoDays = 2 * 24 * 60 * 60 * 1000;
const expireTime = Math.floor((Date.now() + twoDays) / 1000);
const params = {
policy: JSON.stringify({
Statement: [
{
Resource: `https://${cfDomain}/images/*`,
Condition: {
DateLessThan: {
"AWS:EpochTime": expireTime,
},
},
},
],
}),
};
const command = new ICantFindAnAppropriateCommandToSignCookies(params);
try {
const data = await cfClient.send(command);
console.log("SUCCESS!", data);
} catch (error) {
console.error("OH NO ERROR GETTING SIGNED COOKIE", error);
} finally {
console.log("PROCESS COMPLETE");
}
}