使用自定义策略的 AWS 云前端 getSignedCookies 上的策略无效或参数类型无效

问题描述 投票:0回答:1

我正在尝试为 getSignedCookies 创建自定义策略,但我不清楚该策略应采用什么格式。

这是我的代码:

  import { getSignedCookies } from "@aws-sdk/cloudfront-signer";


  let originalString = process.env.S3_PRIVATEKEY_CLOUDFRONT;
  let privateKey = originalString.replace(/\\n/g, "\n");
  const keyPairId = "XXXXXXXXXX";
  var currentDate = new Date();
  var oneHourFromNow = new Date(currentDate.getTime() + 60 * 60 * 1000);
  const dateLessThan = oneHourFromNow;

  const policy = {
    "Statement": [
      {
        "Resource": "https://xxxxxxxxxx.cloudfront.net/harry/*",
        "Condition": {
          "DateLessThan": {
            "AWS:EpochTime": dateLessThan.getTime(),
          },
        },
      },
    ],
  };

  const signedCookie = getSignedCookies({
    keyPairId,
    policy,
    privateKey,
  });
  return signedCookie;

这返回了一个错误:

error - TypeError [ERR_INVALID_ARG_TYPE]: The "data" argument must be of type string or an instance of Buffer, TypedArray, or DataView. Received an instance of Object
getSignedCookies 函数。

为了解决这个问题,我尝试将策略常量解析为字符串:

...
  const stringPoly = JSON.stringify(policy);
  const signedCookie = getSignedCookies({
    keyPairId,
    stringPoly,
    privateKey,
  });
...

但是我收到了

error - Error: Invalid policy

我的代码有什么问题?

javascript reactjs amazon-web-services next.js amazon-cloudfront
1个回答
0
投票

您的政策似乎是正确的。您没有向 getSignedCookies 函数提供 URL 和 dateLessThan 参数。

return getSignedCookies({url:url,privateKey:privateKey,keyPairId:keyPairId,dateLessThan:expiry,policy:policyString});
© www.soinside.com 2019 - 2024. All rights reserved.