AWS S3-错误:无法获取本地发行者证书

时间:2020-02-20 21:13:05

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

在我完成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)

1 个答案:

答案 0 :(得分:0)

我成功了。我需要在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,
      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);
  }
});