【问题标题】:AWS S3 - Error: unable to get local issuer certificateAWS S3 - 错误:无法获取本地颁发者证书
【发布时间】:2021-10-14 19:20:01
【问题描述】:

在我的 Protractor 测试套件执行完成后,我正在尝试将我的 html 结果文件上传到 AWS S3。我在自动化中使用 JavaScript。请帮我解决这里的错误:

  static uploadtoS3() {
    const AWS = require('aws-sdk');
    var FILE_NAME_LOCAL;
    var crypt = require("crypto");

    fs.readdirSync("./reports/html/").forEach(file => {
      if (file.startsWith("execution_report")) {
        FILE_NAME_LOCAL = process.cwd() + "\\reports\\html\\" + file;
      }
    });
    console.log("File name: " + FILE_NAME_LOCAL);
    // Get file stream
    const fileStream = fs.createReadStream(FILE_NAME_LOCAL);

    var hash = crypt.createHash("md5")
      .update(new Buffer.from(FILE_NAME_LOCAL, 'binary'))
      .digest("base64");
    console.log("Hash: "+hash);
    // Call S3 to retrieve upload file to specified bucket
    const uploadParams = {
      Bucket: 'my.bucket',
      Key: 'automation_report.html',
      Body: fileStream,
      ContentType: "text/html",
      ContentMD5: hash,
      // CacheControl: "max-age=0,no-cache,no-store,must-revalidate",
      ACL: 'public-read',
    };

    const s3 = new AWS.S3({
      // TODO: use this `accessKeyId: <key>` annotation to indicate the presence of a key instead of placing the actual key here. 
      endpoint: "https://3site-abc-wip1.nam.nsroot.net",
      accessKeyId: <access_key_id>,
      secretAccessKey: <secret_access_key>,
      signatureVersion: 'v4',
      ca: fs.readFileSync('C:\\Users\\AB11111\\InternalCAChain_PROD.pem'),
      sslEnabled: true
    });
    // Create S3 service object and upload
    s3.upload(uploadParams, function (err, data) {
      console.log("Inside upload..");
      if (err) {
        throw err;
      } if (data) {
        console.log('Upload Success. File location:' + data.Location);
      }
    });
  }

错误:无法在以下位置获取本地颁发者证书 TLSSocket.emit 上的 TLSSocket.onConnectSecure (_tls_wrap.js:1049:34) (events.js:182:13) 在 TLSSocket.EventEmitter.emit (domain.js:442:20) 在 TLSSocket._finishInit (_tls_wrap.js:631:8)

【问题讨论】:

  • 您是否设置了正确的凭据?你错过了AWS.config.update({ accessKeyId, secretAccessKey });
  • 谢谢,但AWS.config.update({ accessKeyId, secretAccessKey }); 没有帮助。我确定凭据。
  • 我只是希望它们不是有效的凭据
  • 您不认为错误的凭据会产生错误吗?我没有看到任何错误。
  • 我已经用我最近遇到的错误更新了我的问题。我请求你再调查一下。

标签: javascript amazon-web-services amazon-s3 protractor


【解决方案1】:

我让它工作了。我需要在 AWS.Config 中添加证书。完整的工作代码如下。这可能会帮助某人。注意:以下凭据和网址仅用于表示目的,它们不是真实的:

const AWS = require('aws-sdk');
const https = require('https');
var FILE_NAME_LOCAL;

AWS.config.update({
  httpOptions: {
    agent: new https.Agent({
      // rejectUnauthorized: false, // Don't use this - this is insecure, just like  --no-verify-ssl in AWS cli
      ca: fs.readFileSync('./support/InternalCAChain_PROD.pem')
    })
  }
});

const s3 = new AWS.S3({
  s3BucketEndpoint: true,
  endpoint: "https://my.bucket.3site-abc.nam.nsroot.net/",
  accessKeyId: "abABcdCD",
  secretAccessKey: "kjlJLlklkLlUYt",
});

// Get file stream
fs.readdirSync("./reports/html/").forEach(file => {
  if (file.startsWith("execution_report")) {
    FILE_NAME_LOCAL = process.cwd() + "\\reports\\html\\" + file;
  }
});
const fileStream = fs.readFileSync(FILE_NAME_LOCAL);

// Call S3 to retrieve upload file to specified bucket
const uploadParams = {
  Bucket: 'my.bucket',
  Key: path.basename(FILE_NAME_LOCAL),
  Body: fileStream,
  ContentType: "text/html",
  ContentEncoding: 'UTF-8',
  ACL: 'public-read',
};

// Create S3 service object and upload
s3.upload(uploadParams, function (err, data) {
  console.log("Inside upload..");
  if (err) {
    throw err;
  } if (data) {
    s3FileLocation = data.Location;
    console.log('Upload Success. File location:' + data.Location);
  }
});

【讨论】:

    猜你喜欢
    • 2020-09-18
    • 2016-04-20
    • 2015-03-18
    • 2011-12-26
    • 1970-01-01
    • 2014-08-13
    • 2018-05-15
    • 2020-01-20
    • 2016-07-06
    相关资源
    最近更新 更多