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

suzh9iv8  于 2023-08-04  发布在  Node.js
关注(0)|答案(3)|浏览(115)

我正在尝试连接到自定义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";没有帮助。

fjnneemd

fjnneemd1#

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

hivapdat

hivapdat2#

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

r7s23pms

r7s23pms3#

试试这个:

import * as https from 'https';

import { S3Client } from '@aws-sdk/client-s3';
import { NodeHttpHandler } from '@aws-sdk/node-http-handler';

const client = new S3Client({
  // other options ...
  requestHandler: new NodeHttpHandler({
    httpsAgent: new https.Agent({
      rejectUnauthorized: false,
    }),
  }),
});

字符串
并与客户做你的要求

相关问题