johannesboyne / gofakes3

A simple fake AWS S3 object storage (used for local test-runs against AWS S3 APIs)
MIT License
358 stars 81 forks source link

Support for both path mode and host mode simultaneously #82

Closed shabbyrobe closed 1 year ago

shabbyrobe commented 1 year ago

Hallo Johannes! Hope you're well.

I've got a situation here at work where I need to support host based bucket routing as well as path-based at the same time. This PR adds a new middleware which, if enabled, takes precedence over the existing "all or nothing" middleware, that will support host-based bucket routing if and only if the host is a subdomain of the configured hosts.

If we start gofakes3 with the example.com host (and autobucket turned on just to make things easier):

go run ./cmd/gofakes3/ -backend=mem -autobucket -hostbucketbase=example.com

Requests for example.com will presume the first path segment is the bucket name:

$ curl -H "Host: example.com" http://localhost:9000/test
<?xml version="1.0" encoding="UTF-8"?>
<ListBucketResult xmlns="http://s3.amazonaws.com/doc/2006-03-01/">
  <Name>test</Name>
  <IsTruncated>false</IsTruncated>
  <Prefix></Prefix>
  <MaxKeys>1000</MaxKeys>
  <Marker></Marker>
</ListBucketResult>

Whereas requests for bucket.example.com will presume the first host segment is the bucket name, and the entire path becomes the object key:

$ curl -H "Host: bucket.example.com" http://localhost:9000/test
<?xml version="1.0" encoding="UTF-8"?>
<Error>
  <Code>NoSuchKey</Code>
  <Resource>test</Resource>
</Error>
sonarcloud[bot] commented 1 year ago

Kudos, SonarCloud Quality Gate passed!    Quality Gate passed

Bug A 0 Bugs
Vulnerability A 0 Vulnerabilities
Security Hotspot A 0 Security Hotspots
Code Smell A 1 Code Smell

No Coverage information No Coverage information
0.0% 0.0% Duplication