我想使用签名的 cookie 来查看存储在 S3 上的私有内容以及 CloudFront for CDN。
我不知道在 aws-sdk-js-v3 中使用哪些命令来获取签名的 cookie。根据新的 SDK 文档,它应该类似于下面的第二个代码示例,但我找不到包含我需要的命令的特定 npm 包。在 v2 中它是“getSignedCookie”,但我不知道它更新到了什么。
老派(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 });
尝试使用 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");
}
}