node.js - 使用 AWS SDK Node.js 的自定义 S3 服务器的自签名证书错误

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

我正在尝试连接到自定义 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 })
  }
});

实际上,它在尝试创建存储桶时会导致以下错误:

{ 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"; 也没有帮助。

最佳答案

那是因为当你在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 })
  }
});

关于node.js - 使用 AWS SDK Node.js 的自定义 S3 服务器的自签名证书错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51094754/

相关文章:

node.js - 用nodeJS打洞?

node.js - 在 express 中使用多个路由器

node.js - 提取.node文件

javascript - 测试 oAuth 登录流程

amazon-web-services - CloudFront 上基于设备的重定向从 S3 源服务

ssl - 由于未正确使用 ssl 证书,REST 请求未在 newman 中执行

php - 意外开启 phpmyadmin 的 SSL/HTTPS

ubuntu - 从 nginx 的源代码编译时,http_ssl_module 安装失败

ios - Aws iOS sdk Pre-singed url 给出 "SignatureDoesNotMatch"错误

amazon-s3 - 使用 AWS SDK V2 删除对象?