-
Notifications
You must be signed in to change notification settings - Fork 1
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
S3 Backend MVP #75
base: master
Are you sure you want to change the base?
S3 Backend MVP #75
Changes from 1 commit
93a4bbb
4348fc9
35513db
edb247e
4b16319
1f8dd0a
3a7151d
7fb600b
12aac3d
1d7b8b3
0dfea32
71e0b75
9d5b76f
497a974
1655738
1dec868
16b3d89
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
package storage | ||
|
||
import ( | ||
"bytes" | ||
"io/ioutil" | ||
|
||
"github.com/aws/aws-sdk-go/aws" | ||
"github.com/aws/aws-sdk-go/aws/session" | ||
"github.com/aws/aws-sdk-go/service/s3" | ||
"golang.org/x/net/context" | ||
) | ||
|
||
const () | ||
|
||
// newS3Backend returns a new backend implementation backed by the supplied | ||
// S3 bucket name. Note that with the current interface, the AWS_REGION environment | ||
// variable must be specified to use this backend. | ||
func newS3Backend(bucketName string) (*s3Backend, error) { | ||
return &s3Backend{bucketName: aws.String(bucketName)}, nil | ||
} | ||
|
||
type s3Backend struct { | ||
bucketName *string | ||
} | ||
|
||
func (b *s3Backend) read(_ context.Context, id string) ([]byte, error) { | ||
sess := session.Must(session.NewSession()) | ||
|
||
s3Client := s3.New(sess) | ||
getOutput, err := s3Client.GetObject(&s3.GetObjectInput{ | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Seems like there's an easily accessible [1] https://github.com/aws/aws-sdk-go/blob/master/service/s3/api.go#L2969 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It's pretty great that they support this now 🎉 |
||
Bucket: b.bucketName, | ||
Key: aws.String(id), | ||
}) | ||
if err != nil { | ||
return []byte{}, err | ||
} | ||
|
||
return ioutil.ReadAll(getOutput.Body) | ||
} | ||
|
||
func (b *s3Backend) write(_ context.Context, id string, contents []byte) error { | ||
sess := session.Must(session.NewSession()) | ||
|
||
s3Client := s3.New(sess) | ||
_, err := s3Client.PutObject(&s3.PutObjectInput{ | ||
Bucket: b.bucketName, | ||
Key: aws.String(id), | ||
Body: bytes.NewReader(contents), | ||
}) | ||
|
||
return err | ||
} | ||
|
||
func (b *s3Backend) list(_ context.Context, prefix string) ([]string, error) { | ||
sess := session.Must(session.NewSession()) | ||
|
||
s3Client := s3.New(sess) | ||
listOutput, err := s3Client.ListObjects(&s3.ListObjectsInput{ | ||
Bucket: b.bucketName, | ||
Prefix: aws.String(prefix), | ||
}) | ||
if err != nil { | ||
return []string{}, err | ||
} | ||
|
||
var keys []string | ||
for _, obj := range listOutput.Contents { | ||
keys = append(keys, *obj.Key) | ||
} | ||
|
||
return keys, nil | ||
} | ||
|
||
func (b *s3Backend) delete(_ context.Context, id string) error { | ||
sess := session.Must(session.NewSession()) | ||
|
||
s3Client := s3.New(sess) | ||
_, err := s3Client.DeleteObject(&s3.DeleteObjectInput{ | ||
Bucket: b.bucketName, | ||
Key: aws.String(id), | ||
}) | ||
|
||
return err | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
From the docs [1] it seems that these sessions (or perhaps even the client?) are designed to be cached/reused. Should we perahps store one in the backend object rather than creating it on every call? Here and below.
[1] https://docs.aws.amazon.com/sdk-for-go/api/aws/session/
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It seems like storing the client makes sense for now. That may change, but it's a detail that doesn't impact the interface.