【问题标题】:Self-signed certificate error with custom S3 server with AWS SDK Node.js带有 AWS 开发工具包 Node.js 的自定义 S3 服务器的自签名证书错误
【发布时间】:2018-12-08 05:40:53
【问题描述】:

我正在尝试连接到自定义 S3 服务器(不基于 AWS)并创建存储桶。

在 Python 中使用以下代码有效:

session = boto3.session.Session()
s3res = session.resource(service_name='s3',
  use_ssl=True,
  verify=False,
  aws_access_key_id='xxx',
  aws_secret_access_key='xxx',
  endpoint_url='https://xxx',
  config=botocore_client_config)

但是,Node.js 中的以下代码不会:

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

const S3 = new AWS.S3({
  accessKeyId: 'xxx',
  secretAccessKey: 'xxx',
  endpoint: 'https://xxx/',
  s3ForcePathStyle: true,
  httpOptions: {
    agent: new https.Agent({ rejectUnauthorized: false })
  }
});

其实在尝试创建bucket的时候会报如下错误:

{ NetworkingError: Protocol "http:" not supported. Expected "https:"
    at new ClientRequest (_http_client.js:109:11)
    at Object.request (http.js:41:10)
    at features.constructor.handleRequest (/home/ec2-user/s3-tests/node_modules/aws-sdk/lib/http/node.js:42:23)

如果我删除自定义 https 代理,我会收到以下错误:

{ Error: self signed certificate
    at TLSSocket.onConnectSecure (_tls_wrap.js:1048:34)

此外,设置process.env.NODE_TLS_REJECT_UNAUTHORIZED = "0"; 也无济于事。

【问题讨论】:

  • 根据这些docs,它表示 httpOptions.agent 选项: agent [http.Agent, https.Agent] — 执行 HTTP 请求的代理对象。用于连接池。对于非 SSL 连接,默认为全局代理 (http.globalAgent)。请注意,对于 SSL 连接,使用特殊的代理对象来启用对等证书验证。此功能仅在 Node.js 环境中可用。 也许有一些自定义代码验证证书?

标签: node.js ssl amazon-s3 aws-sdk boto3


【解决方案1】:

那是因为当你在httpOptions 中指定agent 时,你使用了https

const https = require('https');

如果您不想要 ssl 版本,请改用 http

const http = require('http');

const S3 = new AWS.S3({
  accessKeyId: 'xxx',
  secretAccessKey: 'xxx',
  endpoint: 'https://xxx/',
  s3ForcePathStyle: true,
  httpOptions: {
    agent: new http.Agent({ rejectUnauthorized: false })
  }
});

【讨论】:

    【解决方案2】:

    看起来节点版本缺少 S3 选项中的 sslEnabled: false...

    【讨论】:

      猜你喜欢
      • 2016-07-10
      • 1970-01-01
      • 1970-01-01
      • 2010-12-11
      • 2015-04-15
      • 2019-04-30
      • 2012-06-20
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多