Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to configure AWS S3 SDK for Node.JS to be used with localhost?

I'm trying to use fakes3 as an endpoint for some simple S3 code I've written. I can't get beyond the connection stage though.

The current error is: NetworkingError: getaddrinfo ENOTFOUND.

I've got configuration setup:

"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);  

When I try simple code like:

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

The error mentioned above occurs. When I leave out directly setting the endPoint, the request is made to the East AWS region (and ignores the endpoint value I've passed in via configuration).

And, I'm running fakes3 using the command line:

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
like image 876
WiredPrairie Avatar asked Nov 30 '13 21:11

WiredPrairie


People also ask

How do I use AWS S3 bucket in NodeJS?

Go to “Services/Storage/S3” and then click in “Create Bucket”. Then, fill the information required in the form. The bucket name must be unique, so be creative :) Click next, next, next and you will have the bucket created.

Can I run NodeJS on S3?

html is the root and files with paths such as js/ css/ images/ taken from the root folder. Note: Its important to understand that you cannot run NodeJS in S3 and instead you will be using the internal web hosting from S3 to serve the static content.

What is AWS SDK NodeJS?

The AWS SDK for JavaScript simplifies use of AWS Services by providing a set of libraries that are consistent and familiar for JavaScript developers. It provides support for API lifecycle consideration such as credential management, retries, data marshaling, serialization, and deserialization.

How do I integrate the latest version of the AWS SDK for JavaScript into my NodeJS lambda function using layers?

To integrate the latest version of an AWS SDK into your Lambda function's deployment package, create a Lambda layer, and then add it to your function. You can use either the AWS Command Line Interface (AWS CLI) or the Lambda console to create a Lambda layer and add it to your function.


2 Answers

In the AWS SDK docs there is the following option:

s3ForcePathStyle (boolean)

Returns whether to force path style URLs for S3 objects

I've tested it works with this config:

var config = {
  accessKeyId: "123",
  secretAccessKey: "abc",
  endpoint: "localhost:3000",
  sslEnabled: false,
  s3ForcePathStyle: true
};
AWS.config.update(config);
s3Client = new AWS.S3();
like image 63
Flukey Avatar answered Oct 21 '22 06:10

Flukey


If you're using the aws sdk version 3. The config structure has changed and s3ForcePathStyle is now renamed to 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'
    });

like image 21
Melchia Avatar answered Oct 21 '22 05:10

Melchia