如何为Node.JS配置AWS S3 SDK以使用FakeS3?

Wir*_*rie 2 amazon-s3 amazon-web-services node.js

我正在尝试使用fakes3作为我编写的一些简单S3代码的端点.我不能超越连接阶段.

目前的错误是:NetworkingError: getaddrinfo ENOTFOUND.

我有配置设置:

"aws": {                                
    "accessKeyId": "123",               
    "secretAccessKey": "abc",           
    "region": "",                       
    "endpoint": "http://localhost:8081",
    "sslEnabled": false                 
}               

var AWS = require('aws-sdk');
// loaded the config into an object called `config`:

AWS.config.update(config.aws);
s3 = new AWS.S3();
// also tried creating an `EndPoint`:
s3.endpoint = new AWS.Endpoint(config.aws.endpoint);  
Run Code Online (Sandbox Code Playgroud)

当我尝试简单的代码时:

s3.putObject({ Bucket: 'logging', Key: "logging123", Body: "started" }, 
   function(err, data) {
   if (err) {
       console.log(err);
   }
});
Run Code Online (Sandbox Code Playgroud)

发生上述错误.当我遗漏直接设置时endPoint,请求发送到东AWS区域(并忽略endpoint我通过配置传递的值).

而且,我正在fakes3使用命令行运行:

fakes3 -r c:\temp\_fakes3 -p 8081
Loading FakeS3 with c:/temp/_fakes3 on port 8081 with hostname s3.amazonaws.com
[2013-11-30 14:20:22] INFO  WEBrick 1.3.1
[2013-11-30 14:20:22] INFO  ruby 2.0.0 (2013-06-27) [x64-mingw32]
[2013-11-30 14:20:22] INFO  WEBrick::HTTPServer#start: pid=11800 port=8081
Run Code Online (Sandbox Code Playgroud)

Flu*_*key 20

在AWS SDK 文档中,有以下选项:

s3ForcePathStyle(boolean)

返回是否强制S3对象的路径样式URL

我测试过它适用于这个配置:

var config = {
  accessKeyId: "123",
  secretAccessKey: "abc",
  endpoint: "localhost:3000",
  sslEnabled: false,
  s3ForcePathStyle: true
};
AWS.config.update(config);
s3Client = new AWS.S3();
Run Code Online (Sandbox Code Playgroud)

  • 有人会澄清"强制路径样式URL"的含义吗? (2认同)
  • 亚当·贝克(Adam-beck),我认为是要强制使用诸如http:// {host} / {bucket} / {key}之类的网址,而不是http:// {bucket}。{host} / {key} (2认同)

Mel*_*hia 5

如果您使用的是 aws sdk 版本 3。配置结构已更改,s3ForcePathStyle现已重命名为forcePathStyle

const { S3 } = require("@aws-sdk/client-s3");

const config = {
          "credentials": {
            "accessKeyId": "minioadmin",
            "secretAccessKey": "minioadmin"
          },
          "endpoint": "http://127.0.0.1:9099",
          "sslEnabled": false,
          "forcePathStyle": true
    };
const s3Client = new S3(config);

// example of usage
const response = await s3Client.listObjectsV2({
      Bucket: 'bucketname',
      Prefix: 'prefix'
    });

Run Code Online (Sandbox Code Playgroud)