Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package s3mock
- import (
- "bytes"
- "errors"
- awsS3 "github.com/aws/aws-sdk-go/service/s3"
- "github.com/aws/aws-sdk-go/service/s3/s3iface"
- "io"
- "io/ioutil"
- "strings"
- )
- type byteReadCloser struct {
- *bytes.Reader
- }
- func (b *byteReadCloser) Close() error {
- return nil
- }
- type fakeS3 struct {
- s3iface.S3API
- store map[string][]byte
- }
- func newFakeS3() *fakeS3 {
- var fakeS3 fakeS3
- fakeS3.store = map[string][]byte{}
- return &fakeS3
- }
- func (f *fakeS3) PutObject(iput *awsS3.PutObjectInput) (*awsS3.PutObjectOutput, error) {
- _, err := iput.Body.Seek(0, io.SeekStart)
- if err != nil {
- return nil, err
- }
- b, err := ioutil.ReadAll(iput.Body)
- if err != nil {
- return nil, err
- }
- f.store[*iput.Key] = b
- return &awsS3.PutObjectOutput{}, nil
- }
- func (f *fakeS3) ListObjectsV2(iput *awsS3.ListObjectsV2Input) (*awsS3.ListObjectsV2Output, error) {
- var (
- objs []*awsS3.Object
- isTruncated bool
- )
- prefix := iput.Prefix
- num := 0
- for key, _ := range f.store {
- if strings.Index(key, *prefix) == 0 {
- objKey := key
- objs = append(objs, &awsS3.Object{
- Key: &objKey,
- })
- }
- num += 1
- if num >= 1000 {
- isTruncated = true
- break
- }
- }
- return &awsS3.ListObjectsV2Output{
- Contents: objs,
- IsTruncated: &isTruncated,
- NextContinuationToken: nil,
- ContinuationToken: nil,
- }, nil
- }
- func (f *fakeS3) GetObject(iput *awsS3.GetObjectInput) (*awsS3.GetObjectOutput, error) {
- if iput.Key == nil {
- return nil, errors.New("key not supplied")
- }
- key := *iput.Key
- b, ok := f.store[key]
- if !ok {
- return nil, errors.New("item not found")
- }
- return &awsS3.GetObjectOutput{
- Body: &byteReadCloser{bytes.NewReader(b)},
- }, nil
- }
- func getTestStore() s3.Store {
- var store s3.Store
- store.Bucket = "test-bucket"
- store.S3 = newFakeS3()
- return store
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement