• Stars
    star
    360
  • Rank 117,859 (Top 3 %)
  • Language
    Go
  • License
    MIT License
  • Created over 8 years ago
  • Updated 13 days ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

A simple fake AWS S3 object storage (used for local test-runs against AWS S3 APIs)

CircleCI Codecov

Logo

AWS (GOFAKE)S3

AWS S3 fake server and testing library for extensive S3 test integrations. Either by running a test-server, e.g. for testing of AWS Lambda functions accessing S3. Or, to have a simple and convencience S3 mock- and test-server.

What to use it for?

We're using it for the local development of S3 dependent Lambda functions, to test AWS S3 golang implementations and access, and to test browser based direct uploads to S3 locally.

What not to use it for?

Please don't use gofakes3 as a production service. The intended use case for gofakes3 is currently to facilitate testing. It's not meant to be used for safe, persistent access to production data at the moment.

There's no reason we couldn't set that as a stretch goal at a later date, but it's a long way down the road, especially while we have so much of the API left to implement; breaking changes are inevitable.

In the meantime, there are more battle-hardened solutions for production workloads out there, some of which are listed in the "Similar Notable Projects" section below.

How to use it?

Example (aws-sdk-go version 1)

// fake s3
backend := s3mem.New()
faker := gofakes3.New(backend)
ts := httptest.NewServer(faker.Server())
defer ts.Close()

// configure S3 client
s3Config := &aws.Config{
	Credentials:      credentials.NewStaticCredentials("YOUR-ACCESSKEYID", "YOUR-SECRETACCESSKEY", ""),
	Endpoint:         aws.String(ts.URL),
	Region:           aws.String("eu-central-1"),
	DisableSSL:       aws.Bool(true),
	S3ForcePathStyle: aws.Bool(true),
}
newSession := session.New(s3Config)

s3Client := s3.New(newSession)
cparams := &s3.CreateBucketInput{
	Bucket: aws.String("newbucket"),
}

// Create a new bucket using the CreateBucket call.
_, err := s3Client.CreateBucket(cparams)
if err != nil {
	// Message from an error.
	fmt.Println(err.Error())
	return
}

// Upload a new object "testobject" with the string "Hello World!" to our "newbucket".
_, err = s3Client.PutObject(&s3.PutObjectInput{
	Body:   strings.NewReader(`{"configuration": {"main_color": "#333"}, "screens": []}`),
	Bucket: aws.String("newbucket"),
	Key:    aws.String("test.txt"),
})

// ... accessing of test.txt through any S3 client would now be possible

Example for V2 (aws-sdk-go-v2)

backend := s3mem.New()
faker := gofakes3.New(backend)
ts := httptest.NewServer(faker.Server())
defer ts.Close()

// Difference in configuring the client

// Setup a new config
cfg, _ := config.LoadDefaultConfig(
	context.TODO(),
    config.WithCredentialsProvider(credentials.NewStaticCredentialsProvider("KEY", "SECRET", "SESSION")),
    config.WithHTTPClient(&http.Client{
        Transport: &http.Transport{
            TLSClientConfig: &tls.Config{InsecureSkipVerify: true},
        },
    }),
    config.WithEndpointResolverWithOptions(
        aws.EndpointResolverWithOptionsFunc(func(_, _ string, _ ...interface{}) (aws.Endpoint, error) {
            return aws.Endpoint{URL: ts.URL}, nil
        }),
    ),
)

// Create an Amazon S3 v2 client, important to use o.UsePathStyle
// alternatively change local DNS settings, e.g., in /etc/hosts
// to support requests to http://<bucketname>.127.0.0.1:32947/...
client := s3.NewFromConfig(cfg, func(o *s3.Options) {
	o.UsePathStyle = true
})

Please feel free to check it out and to provide useful feedback (using github issues), but be aware, this software is used internally and for the local development only. Thus, it has no demand for correctness, performance or security.

There are two ways to run locally: using DNS, or using S3 path mode.

S3 path mode is the most flexible and least restrictive, but it does require that you are able to modify your client code.In Go, the modification would look like so:

config := aws.Config{}
config.WithS3ForcePathStyle(true)

S3 path mode works over the network by default for all bucket names.

If you are unable to modify the code, DNS mode can be used, but it comes with further restrictions and requires you to be able to modify your local DNS resolution.

If using localhost as your endpoint, you will need to add the following to /etc/hosts for every bucket you want to fake:

127.0.0.1 <bucket-name>.localhost

It is trickier if you want other machines to be able to use your fake S3 server as you need to be able to modify their DNS resolution as well.

Exemplary usage

Lambda Example

var AWS   = require('aws-sdk')

var ep = new AWS.Endpoint('http://localhost:9000');
var s3 = new AWS.S3({endpoint: ep});

exports.handle = function (e, ctx) {
  s3.createBucket({
    Bucket: '<bucket-name>',
  }, function(err, data) {
    if (err) return console.log(err, err.stack);
    ctx.succeed(data)
  });
}

Upload Example

<html>
  <head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
  </head>
  <body>

  <form action="http://localhost:9000/<bucket-name>/" method="post" enctype="multipart/form-data">
    Key to upload:
    <input type="input"  name="key" value="user/user1/test/<filename>" /><br />
    <input type="hidden" name="acl" value="public-read" />
    <input type="hidden" name="x-amz-meta-uuid" value="14365123651274" />
    <input type="hidden" name="x-amz-server-side-encryption" value="AES256" />
    <input type="text"   name="X-Amz-Credential" value="AKIAIOSFODNN7EXAMPLE/20151229/us-east-1/s3/aws4_request" />
    <input type="text"   name="X-Amz-Algorithm" value="AWS4-HMAC-SHA256" />
    <input type="text"   name="X-Amz-Date" value="20151229T000000Z" />

    Tags for File:
    <input type="input"  name="x-amz-meta-tag" value="" /><br />
    <input type="hidden" name="Policy" value='<Base64-encoded policy string>' />
    <input type="hidden" name="X-Amz-Signature" value="<signature-value>" />
    File:
    <input type="file"   name="file" /> <br />
    <!-- The elements after this will be ignored -->
    <input type="submit" name="submit" value="Upload to Amazon S3" />
  </form>
</html>

Similar notable projects

Contributors

A big thank you to all the contributors, especially Blake @shabbyrobe who pushed this little project to the next level!

Help wanted

More Repositories

1

node-stl

parse stl files with Node.js and get volume, and weight
JavaScript
90
star
2

gomon

Simple monitor script for use during development of a golang app.
JavaScript
67
star
3

node-dxf

Parse DXF files to get polygons and text elements.
JavaScript
16
star
4

nodejs-quicksort-power

node.js' Power! Running a quick sort algorithm with an unordered list.
JavaScript
14
star
5

simpleTileServer

How to write your own simple tile server, using Node.js + node-canvas + tileJson
JavaScript
14
star
6

godockersample

A Go, Docker (sample) workflow
Makefile
9
star
7

node-dxf-to-png

Transform a DXF into a PNG
JavaScript
8
star
8

boltbackup

A simple BoltDB Backup to Amazon S3 package
Go
8
star
9

beacontriangulation

Triangulation Algorithm Beacons (or any other point to point to point + radii stuff)
JavaScript
5
star
10

echojson

Echo back json from URL
JavaScript
5
star
11

sse

Simple Server-Side Events
Go
3
star
12

bucker-receiver

A simple logging service for the simple "bucker" logging module.
JavaScript
2
star
13

ShowMe.js

ShowMe.js is a little JavaScript util for showing Apple-Addressbook like "Large Type" notifications.
JavaScript
2
star
14

js-array-iterator

Simple JavaScript Array Iterator, using next() and Events.
JavaScript
2
star
15

javascript-testing

Testing JavaScript code (a how to & best practice)
JavaScript
2
star
16

try_git

1
star
17

ajt

minimal ajax json template lib
JavaScript
1
star
18

json-combinator

Combine JSON files (or simple) JavaScript Objects.
1
star
19

libvncserver

Security Advancement for multi VNC Server usage
C
1
star
20

mor-aws-lambda-serverless-architecture

AWS Lambda 100% scaleable, serverless web applications
HTML
1
star
21

gettheshitdone

A simple website creating tool, package, framework whatever.
JavaScript
1
star
22

browserify-marionette-express

A browserify, marionette, express.js, middleware for simple development
JavaScript
1
star
23

dotfiles

Vim Script
1
star
24

smalljwtproxy

A small JWT token validator proxy
Go
1
star
25

gofreegeoipclient

Simple Golang Client for the Freegeoip.net (JSON) API
Go
1
star
26

mapboxGeoImage

Simple Mapbox Geocoder and Image Generator
Go
1
star
27

mor-gopresentation

My presentation samples for the Monster on Rails Meetup
1
star