Skip to content

Commit

Permalink
pass req obj to backend methods
Browse files Browse the repository at this point in the history
  • Loading branch information
saw-jan authored and ncw committed Apr 12, 2024
1 parent bf4feae commit beb07a1
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 62 deletions.
27 changes: 14 additions & 13 deletions backend.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package gofakes3

import (
"io"
"net/http"

"github.com/aws/aws-sdk-go/aws/awserr"
)
Expand Down Expand Up @@ -134,7 +135,7 @@ type Backend interface {
// ListBuckets returns a list of all buckets owned by the authenticated
// sender of the request.
// https://docs.aws.amazon.com/AmazonS3/latest/API/RESTServiceGET.html
ListBuckets() ([]BucketInfo, error)
ListBuckets(r *http.Request) ([]BucketInfo, error)

// ListBucket returns the contents of a bucket. Backends should use the
// supplied prefix to limit the contents of the bucket and to sort the
Expand All @@ -155,18 +156,18 @@ type Backend interface {
// work fine if you ignore the pagination request, but this may not suit
// your application. Not all backends bundled with gofakes3 correctly
// support this pagination yet, but that will change.
ListBucket(name string, prefix *Prefix, page ListBucketPage) (*ObjectList, error)
ListBucket(r *http.Request, name string, prefix *Prefix, page ListBucketPage) (*ObjectList, error)

// CreateBucket creates the bucket if it does not already exist. The name
// should be assumed to be a valid name.
//
// If the bucket already exists, a gofakes3.ResourceError with
// gofakes3.ErrBucketAlreadyExists MUST be returned.
CreateBucket(name string) error
CreateBucket(r *http.Request, name string) error

// BucketExists should return a boolean indicating the bucket existence, or
// an error if the backend was unable to determine existence.
BucketExists(name string) (exists bool, err error)
BucketExists(r *http.Request, name string) (exists bool, err error)

// DeleteBucket deletes a bucket if and only if it is empty.
//
Expand All @@ -176,7 +177,7 @@ type Backend interface {
// If the bucket does not exist, gofakes3.ErrNoSuchBucket MUST be returned.
//
// AWS does not validate the bucket's name for anything other than existence.
DeleteBucket(name string) error
DeleteBucket(r *http.Request, name string) error

// GetObject must return a gofakes3.ErrNoSuchKey error if the object does
// not exist. See gofakes3.KeyNotFound() for a convenient way to create
Expand All @@ -191,7 +192,7 @@ type Backend interface {
// implementers MUST return ErrNotImplemented.
//
// If the backend is a VersionedBackend, GetObject retrieves the latest version.
GetObject(bucketName, objectName string, rangeRequest *ObjectRangeRequest) (*Object, error)
GetObject(r *http.Request, bucketName, objectName string, rangeRequest *ObjectRangeRequest) (*Object, error)

// HeadObject fetches the Object from the backend, but reading the Contents
// will return io.EOF immediately.
Expand All @@ -202,7 +203,7 @@ type Backend interface {
//
// HeadObject should return a NotFound() error if the object does not
// exist.
HeadObject(bucketName, objectName string) (*Object, error)
HeadObject(r *http.Request, bucketName, objectName string) (*Object, error)

// DeleteObject deletes an object from the bucket.
//
Expand All @@ -220,18 +221,18 @@ type Backend interface {
// delete marker, which becomes the latest version of the object. If there
// isn't a null version, Amazon S3 does not remove any objects.
//
DeleteObject(bucketName, objectName string) (ObjectDeleteResult, error)
DeleteObject(r *http.Request, bucketName, objectName string) (ObjectDeleteResult, error)

// PutObject should assume that the key is valid. The map containing meta
// may be nil.
//
// The size can be used if the backend needs to read the whole reader; use
// gofakes3.ReadAll() for this job rather than ioutil.ReadAll().
PutObject(bucketName, key string, meta map[string]string, input io.Reader, size int64) (PutObjectResult, error)
PutObject(r *http.Request, bucketName, key string, meta map[string]string, input io.Reader, size int64) (PutObjectResult, error)

DeleteMulti(bucketName string, objects ...string) (MultiDeleteResult, error)
DeleteMulti(r *http.Request, bucketName string, objects ...string) (MultiDeleteResult, error)

CopyObject(srcBucket, srcKey, dstBucket, dstKey string, meta map[string]string) (CopyObjectResult, error)
CopyObject(r *http.Request, srcBucket, srcKey, dstBucket, dstKey string, meta map[string]string) (CopyObjectResult, error)
}

// VersionedBackend may be optionally implemented by a Backend in order to support
Expand Down Expand Up @@ -308,9 +309,9 @@ type VersionedBackend interface {
ListBucketVersions(bucketName string, prefix *Prefix, page *ListBucketVersionsPage) (*ListBucketVersionsResult, error)
}

func MergeMetadata(db Backend, bucketName string, objectName string, meta map[string]string) error {
func MergeMetadata(r *http.Request, db Backend, bucketName string, objectName string, meta map[string]string) error {
// get potential existing object to potentially carry metadata over
existingObj, err := db.GetObject(bucketName, objectName, nil)
existingObj, err := db.GetObject(r, bucketName, objectName, nil)
if err != nil {
if awsErr, ok := err.(awserr.Error); ok && awsErr.Code() != string(ErrNoSuchKey) {
return err
Expand Down
70 changes: 35 additions & 35 deletions gofakes3.go
Original file line number Diff line number Diff line change
Expand Up @@ -184,7 +184,7 @@ func (g *GoFakeS3) httpError(w http.ResponseWriter, r *http.Request, err error)
}

func (g *GoFakeS3) listBuckets(w http.ResponseWriter, r *http.Request) error {
buckets, err := g.storage.ListBuckets()
buckets, err := g.storage.ListBuckets(r)
if err != nil {
return err
}
Expand Down Expand Up @@ -215,7 +215,7 @@ func (g *GoFakeS3) listBuckets(w http.ResponseWriter, r *http.Request) error {
func (g *GoFakeS3) listBucket(bucketName string, w http.ResponseWriter, r *http.Request) error {
g.log.Print(LogInfo, "LIST BUCKET")

if err := g.ensureBucketExists(bucketName); err != nil {
if err := g.ensureBucketExists(r, bucketName); err != nil {
return err
}

Expand All @@ -230,15 +230,15 @@ func (g *GoFakeS3) listBucket(bucketName string, w http.ResponseWriter, r *http.

g.log.Print(LogInfo, "bucketname:", bucketName, "prefix:", prefix, "page:", fmt.Sprintf("%+v", page))

objects, err := g.storage.ListBucket(bucketName, &prefix, page)
objects, err := g.storage.ListBucket(r, bucketName, &prefix, page)
if err != nil {
if err == ErrInternalPageNotImplemented && !g.failOnUnimplementedPage {
// We have observed (though not yet confirmed) that simple clients
// tend to work fine if you simply ignore pagination, so the
// default if this is not implemented is to retry without it. If
// you care about this performance impact for some weird reason,
// you'll need to handle it yourself.
objects, err = g.storage.ListBucket(bucketName, &prefix, ListBucketPage{})
objects, err = g.storage.ListBucket(r, bucketName, &prefix, ListBucketPage{})
if err != nil {
return err
}
Expand Down Expand Up @@ -314,7 +314,7 @@ func (g *GoFakeS3) listBucket(bucketName string, w http.ResponseWriter, r *http.
func (g *GoFakeS3) getBucketLocation(bucketName string, w http.ResponseWriter, r *http.Request) error {
g.log.Print(LogInfo, "GET BUCKET LOCATION")

if err := g.ensureBucketExists(bucketName); err != nil { // S300006
if err := g.ensureBucketExists(r, bucketName); err != nil { // S300006
return err
}

Expand All @@ -331,7 +331,7 @@ func (g *GoFakeS3) listBucketVersions(bucketName string, w http.ResponseWriter,
return ErrNotImplemented
}

if err := g.ensureBucketExists(bucketName); err != nil {
if err := g.ensureBucketExists(r, bucketName); err != nil {
return err
}

Expand Down Expand Up @@ -382,7 +382,7 @@ func (g *GoFakeS3) createBucket(bucket string, w http.ResponseWriter, r *http.Re
if err := ValidateBucketName(bucket); err != nil {
return err
}
if err := g.storage.CreateBucket(bucket); err != nil {
if err := g.storage.CreateBucket(r, bucket); err != nil {
return err
}

Expand All @@ -396,10 +396,10 @@ func (g *GoFakeS3) createBucket(bucket string, w http.ResponseWriter, r *http.Re
func (g *GoFakeS3) deleteBucket(bucket string, w http.ResponseWriter, r *http.Request) error {
g.log.Print(LogInfo, "DELETE BUCKET:", bucket)

if err := g.ensureBucketExists(bucket); err != nil {
if err := g.ensureBucketExists(r, bucket); err != nil {
return err
}
if err := g.storage.DeleteBucket(bucket); err != nil {
if err := g.storage.DeleteBucket(r, bucket); err != nil {
return err
}

Expand All @@ -412,7 +412,7 @@ func (g *GoFakeS3) headBucket(bucket string, w http.ResponseWriter, r *http.Requ
g.log.Print(LogInfo, "HEAD BUCKET", bucket)
g.log.Print(LogInfo, "bucketname:", bucket)

if err := g.ensureBucketExists(bucket); err != nil {
if err := g.ensureBucketExists(r, bucket); err != nil {
return err
}

Expand All @@ -430,7 +430,7 @@ func (g *GoFakeS3) getObject(

g.log.Print(LogInfo, "GET OBJECT", "Bucket:", bucket, "Object:", object)

if err := g.ensureBucketExists(bucket); err != nil {
if err := g.ensureBucketExists(r, bucket); err != nil {
return err
}

Expand All @@ -443,7 +443,7 @@ func (g *GoFakeS3) getObject(

{ // get object from backend
if versionID == "" {
obj, err = g.storage.GetObject(bucket, object, rnge)
obj, err = g.storage.GetObject(r, bucket, object, rnge)
if err != nil {
return err
}
Expand Down Expand Up @@ -520,11 +520,11 @@ func (g *GoFakeS3) headObject(

g.log.Print(LogInfo, "HEAD OBJECT", bucket, object)

if err := g.ensureBucketExists(bucket); err != nil {
if err := g.ensureBucketExists(r, bucket); err != nil {
return err
}

obj, err := g.storage.HeadObject(bucket, object)
obj, err := g.storage.HeadObject(r, bucket, object)
if err != nil {
return err
}
Expand All @@ -548,7 +548,7 @@ func (g *GoFakeS3) headObject(
func (g *GoFakeS3) createObjectBrowserUpload(bucket string, w http.ResponseWriter, r *http.Request) error {
g.log.Print(LogInfo, "CREATE OBJECT THROUGH BROWSER UPLOAD")

if err := g.ensureBucketExists(bucket); err != nil {
if err := g.ensureBucketExists(r, bucket); err != nil {
return err
}

Expand Down Expand Up @@ -593,7 +593,7 @@ func (g *GoFakeS3) createObjectBrowserUpload(bucket string, w http.ResponseWrite
return err
}

result, err := g.storage.PutObject(bucket, key, meta, rdr, fileHeader.Size)
result, err := g.storage.PutObject(r, bucket, key, meta, rdr, fileHeader.Size)
if err != nil {
return err
}
Expand All @@ -609,7 +609,7 @@ func (g *GoFakeS3) createObjectBrowserUpload(bucket string, w http.ResponseWrite
func (g *GoFakeS3) createObject(bucket, object string, w http.ResponseWriter, r *http.Request) (err error) {
g.log.Print(LogInfo, "CREATE OBJECT:", bucket, object)

if err := g.ensureBucketExists(bucket); err != nil {
if err := g.ensureBucketExists(r, bucket); err != nil {
return err
}

Expand Down Expand Up @@ -667,7 +667,7 @@ func (g *GoFakeS3) createObject(bucket, object string, w http.ResponseWriter, r
return err
}

result, err := g.storage.PutObject(bucket, object, meta, rdr, size)
result, err := g.storage.PutObject(r, bucket, object, meta, rdr, size)
if err != nil {
return err
}
Expand All @@ -683,7 +683,7 @@ func (g *GoFakeS3) createObject(bucket, object string, w http.ResponseWriter, r

// CopyObject copies an existing S3 object
func (g *GoFakeS3) copyObject(bucket, object string, meta map[string]string, w http.ResponseWriter, r *http.Request) (err error) {
if err := g.ensureBucketExists(bucket); err != nil {
if err := g.ensureBucketExists(r, bucket); err != nil {
return err
}

Expand All @@ -703,7 +703,7 @@ func (g *GoFakeS3) copyObject(bucket, object string, meta map[string]string, w h
if err != nil {
return err
}
srcObj, err := g.storage.HeadObject(srcBucket, srcKey)
srcObj, err := g.storage.HeadObject(r, srcBucket, srcKey)
if err != nil {
return err
}
Expand All @@ -726,7 +726,7 @@ func (g *GoFakeS3) copyObject(bucket, object string, meta map[string]string, w h
// }
delete(meta, "X-Amz-Acl")

result, err := g.storage.CopyObject(srcBucket, srcKey, bucket, object, meta)
result, err := g.storage.CopyObject(r, srcBucket, srcKey, bucket, object, meta)
if err != nil {
return err
}
Expand All @@ -745,11 +745,11 @@ func (g *GoFakeS3) copyObject(bucket, object string, meta map[string]string, w h

func (g *GoFakeS3) deleteObject(bucket, object string, w http.ResponseWriter, r *http.Request) error {
g.log.Print(LogInfo, "DELETE:", bucket, object)
if err := g.ensureBucketExists(bucket); err != nil {
if err := g.ensureBucketExists(r, bucket); err != nil {
return err
}

result, err := g.storage.DeleteObject(bucket, object)
result, err := g.storage.DeleteObject(r, bucket, object)
if err != nil {
return err
}
Expand All @@ -774,7 +774,7 @@ func (g *GoFakeS3) deleteObjectVersion(bucket, object string, version VersionID,
}

g.log.Print(LogInfo, "DELETE VERSION:", bucket, object, version)
if err := g.ensureBucketExists(bucket); err != nil {
if err := g.ensureBucketExists(r, bucket); err != nil {
return err
}

Expand Down Expand Up @@ -803,7 +803,7 @@ func (g *GoFakeS3) deleteObjectVersion(bucket, object string, version VersionID,
func (g *GoFakeS3) deleteMulti(bucket string, w http.ResponseWriter, r *http.Request) error {
g.log.Print(LogInfo, "delete multi", bucket)

if err := g.ensureBucketExists(bucket); err != nil {
if err := g.ensureBucketExists(r, bucket); err != nil {
return err
}

Expand All @@ -820,7 +820,7 @@ func (g *GoFakeS3) deleteMulti(bucket string, w http.ResponseWriter, r *http.Req
keys[i] = o.Key
}

out, err := g.storage.DeleteMulti(bucket, keys...)
out, err := g.storage.DeleteMulti(r, bucket, keys...)
if err != nil {
return err
}
Expand All @@ -839,7 +839,7 @@ func (g *GoFakeS3) initiateMultipartUpload(bucket, object string, w http.Respons
if err != nil {
return err
}
if err := g.ensureBucketExists(bucket); err != nil {
if err := g.ensureBucketExists(r, bucket); err != nil {
return err
}

Expand Down Expand Up @@ -960,7 +960,7 @@ func (g *GoFakeS3) completeMultipartUpload(bucket, object string, uploadID Uploa
return err
}

result, err := g.storage.PutObject(bucket, object, upload.Meta, bytes.NewReader(fileBody), int64(len(fileBody)))
result, err := g.storage.PutObject(r, bucket, object, upload.Meta, bytes.NewReader(fileBody), int64(len(fileBody)))
if err != nil {
return err
}
Expand All @@ -976,7 +976,7 @@ func (g *GoFakeS3) completeMultipartUpload(bucket, object string, uploadID Uploa
}

func (g *GoFakeS3) listMultipartUploads(bucket string, w http.ResponseWriter, r *http.Request) error {
if err := g.ensureBucketExists(bucket); err != nil {
if err := g.ensureBucketExists(r, bucket); err != nil {
return err
}

Expand All @@ -1001,7 +1001,7 @@ func (g *GoFakeS3) listMultipartUploads(bucket string, w http.ResponseWriter, r
}

func (g *GoFakeS3) listMultipartUploadParts(bucket, object string, uploadID UploadID, w http.ResponseWriter, r *http.Request) error {
if err := g.ensureBucketExists(bucket); err != nil {
if err := g.ensureBucketExists(r, bucket); err != nil {
return err
}

Expand All @@ -1026,7 +1026,7 @@ func (g *GoFakeS3) listMultipartUploadParts(bucket, object string, uploadID Uplo
}

func (g *GoFakeS3) getBucketVersioning(bucket string, w http.ResponseWriter, r *http.Request) error {
if err := g.ensureBucketExists(bucket); err != nil { // S300007
if err := g.ensureBucketExists(r, bucket); err != nil { // S300007
return err
}

Expand All @@ -1044,7 +1044,7 @@ func (g *GoFakeS3) getBucketVersioning(bucket string, w http.ResponseWriter, r *
}

func (g *GoFakeS3) putBucketVersioning(bucket string, w http.ResponseWriter, r *http.Request) error {
if err := g.ensureBucketExists(bucket); err != nil { // S300007
if err := g.ensureBucketExists(r, bucket); err != nil { // S300007
return err
}

Expand All @@ -1069,13 +1069,13 @@ func (g *GoFakeS3) putBucketVersioning(bucket string, w http.ResponseWriter, r *
return g.versioned.SetVersioningConfiguration(bucket, in)
}

func (g *GoFakeS3) ensureBucketExists(bucket string) error {
exists, err := g.storage.BucketExists(bucket)
func (g *GoFakeS3) ensureBucketExists(r *http.Request, bucket string) error {
exists, err := g.storage.BucketExists(r, bucket)
if err != nil {
return err
}
if !exists && g.autoBucket {
if err := g.storage.CreateBucket(bucket); err != nil {
if err := g.storage.CreateBucket(r, bucket); err != nil {
g.log.Print(LogErr, "autobucket create failed:", err)
return ResourceError(ErrNoSuchBucket, bucket)
}
Expand Down
Loading

0 comments on commit beb07a1

Please sign in to comment.