gpt4 book ai didi

node.js - 如何在 PEM 文件中保留换行符,并使用 NodeJS 将其拉入我的应用程序?

转载 作者:太空宇宙 更新时间:2023-11-03 23:58:00 28 4
gpt4 key购买 nike

使用 NodeJS,我将 PEM 文件拉入我的应用程序中。该文件位于 AWS S3 和我的本地环境中。当我在本地运行该应用程序时,一切都很好。但是,当我在 EC2 上运行它时,出现以下错误:

Error: Invalid private key string, must include line breaks

我正在使用AWS CloudFront URL Signature Utility

这是我的功能:

    function createSignedCookie(domain){

// we need the domain
if (!domain) {
return false;
}

const cookieLifeSpan = 900000; // milliseconds, is equal to 15 mins
const expires = new Date().getTime() + cookieLifeSpan;
const keyPairId = config.accessKeyId;
const keyPath = config.pemFilePath;

const options = {expireTime: expires, keypairId: keyPairId, privateKeyPath: keyPath}

const signedCookies = cfsign.getSignedCookies(domain + '/*', options);

return signedCookies;
}

我尝试在 PEM 的每一行末尾添加\n,但这并没有解决问题。
我还尝试在 PEM 的每一行末尾添加\r,但这并没有解决问题。
我看了this但这没有帮助

这是getSignedCookies方法:

function getSignedCookies(cfUrl, params){
var privateKey = _getPrivateKey(params);
var policy = _createPolicy(
cfUrl, _getExpireTime(params), _getIpRange(params));
var signature = _createPolicySignature(policy, privateKey);
var policyStr = new Buffer(policy.toJSON()).toString('base64');

var cookies = {};
cookies['CloudFront-Policy'] = normalizeBase64(policyStr);
cookies['CloudFront-Signature'] = normalizeBase64(signature);
cookies['CloudFront-Key-Pair-Id'] = params.keypairId;

return cookies;
}

这是_privateKey方法

function _getPrivateKey(params) {
var privateKeyString = params.privateKeyString;
var pem;

if (params.privateKeyPath) {
pem = fs.readFileSync(params.privateKeyPath);

privateKeyString = pem.toString('ascii');
console.log(privateKeyString);
}

var newLinePattern = /\r|\n/;
var lineBreakExists = newLinePattern.test(privateKeyString);
if (!lineBreakExists) {
throw new Error('Invalid private key string, must include line breaks');
}

return privateKeyString;
}

上述两种方法均附带 AWS CloudFront URL Signature Utility

最佳答案

只需在使用该值之前替换\n:

var private_value = process.env.PRIVATE_KEY.replace(/\\n/g, '\n');
console.log(private_value);

关于node.js - 如何在 PEM 文件中保留换行符,并使用 NodeJS 将其拉入我的应用程序?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56123177/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com