Skip to content
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

Move backends to seperate packages #25

Merged
merged 1 commit into from
Oct 29, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 16 additions & 4 deletions straw_gcs.go → gcs/straw_gcs.go
Original file line number Diff line number Diff line change
@@ -1,21 +1,33 @@
package straw
package gcs

import (
"context"
"fmt"
"io"
"net/url"
"os"
"path/filepath"
"sort"
"strings"
"time"

"cloud.google.com/go/storage"
"github.com/uw-labs/straw"
"google.golang.org/api/iterator"
"google.golang.org/api/option"
)

var _ StreamStore = &GCSStreamStore{}
var _ straw.StreamStore = &GCSStreamStore{}

func init() {
straw.Register("gs", func(u *url.URL) (straw.StreamStore, error) {
creds := u.Query().Get("credentialsfile")
if creds == "" {
return nil, fmt.Errorf("gs URLs must provide a `credentialsfile` parameter")
}
return NewGCSStreamStore(creds, u.Host)
})
}

func NewGCSStreamStore(credentialsFile string, bucket string) (*GCSStreamStore, error) {
ctx := context.Background()
Expand Down Expand Up @@ -131,7 +143,7 @@ func (sr *gcsStatResult) Sys() interface{} {
return nil
}

func (fs *GCSStreamStore) OpenReadCloser(name string) (StrawReader, error) {
func (fs *GCSStreamStore) OpenReadCloser(name string) (straw.StrawReader, error) {
fi, err := fs.Stat(name)
if err != nil {
return nil, err
Expand Down Expand Up @@ -233,7 +245,7 @@ func (fs *GCSStreamStore) Remove(name string) error {
return fs.client.Bucket(fs.bucket).Object(name).Delete(context.Background())
}

func (fs *GCSStreamStore) CreateWriteCloser(name string) (StrawWriter, error) {
func (fs *GCSStreamStore) CreateWriteCloser(name string) (straw.StrawWriter, error) {
name = fs.noSlashPrefix(name)

if err := fs.checkParentDir(name); err != nil {
Expand Down
25 changes: 21 additions & 4 deletions straw_s3.go → s3/straw_s3.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
package straw
package s3

import (
"fmt"
"io"
"io/ioutil"
"log"
"net/url"
"os"
"path/filepath"
"sort"
Expand All @@ -16,6 +17,7 @@ import (
"github.com/aws/aws-sdk-go/aws/session"
"github.com/aws/aws-sdk-go/service/s3"
"github.com/aws/aws-sdk-go/service/s3/s3manager"
"github.com/uw-labs/straw"
)

type ServerSideEncryptionType string
Expand All @@ -25,7 +27,22 @@ const (
ServerSideEncryptionTypeAES256 ServerSideEncryptionType = "AES256"
)

var _ StreamStore = &S3StreamStore{}
var _ straw.StreamStore = &S3StreamStore{}

func init() {
straw.Register("s3", func(u *url.URL) (straw.StreamStore, error) {
sse := u.Query().Get("sse")
var opts []S3Option
switch sse {
case "":
case "AES256":
opts = append(opts, S3ServerSideEncoding(ServerSideEncryptionTypeAES256))
default:
return nil, fmt.Errorf("unknown server side encryption type '%s'", sse)
}
return NewS3StreamStore(u.Host, opts...)
})
}

func NewS3StreamStore(bucket string, options ...S3Option) (*S3StreamStore, error) {
sess, err := session.NewSessionWithOptions(
Expand Down Expand Up @@ -159,7 +176,7 @@ func (sr *s3StatResult) Sys() interface{} {
return nil
}

func (fs *S3StreamStore) OpenReadCloser(name string) (StrawReader, error) {
func (fs *S3StreamStore) OpenReadCloser(name string) (straw.StrawReader, error) {
fi, err := fs.Stat(name)
if err != nil {
return nil, err
Expand Down Expand Up @@ -299,7 +316,7 @@ func (fs *S3StreamStore) Remove(name string) error {
return err
}

func (fs *S3StreamStore) CreateWriteCloser(name string) (StrawWriter, error) {
func (fs *S3StreamStore) CreateWriteCloser(name string) (straw.StrawWriter, error) {
name = fs.noSlashPrefix(name)

if err := fs.checkParentDir(name); err != nil {
Expand Down
2 changes: 1 addition & 1 deletion straw_s3_options.go → s3/straw_s3_options.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package straw
package s3

// S3Option is an option to the s3 backend
type S3Option interface {
Expand Down
15 changes: 11 additions & 4 deletions straw_sftp.go → sftp/straw_sftp.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package straw
package sftp

import (
"errors"
Expand All @@ -12,10 +12,17 @@ import (
"sync"

"github.com/pkg/sftp"
"github.com/uw-labs/straw"
"golang.org/x/crypto/ssh"
)

var _ StreamStore = &SFTPStreamStore{}
var _ straw.StreamStore = &SFTPStreamStore{}

func init() {
straw.Register("sftp", func(u *url.URL) (straw.StreamStore, error) {
return NewSFTPStreamStore(u.String())
})
}

type SFTPStreamStore struct {
sshClient *ssh.Client
Expand Down Expand Up @@ -74,7 +81,7 @@ func (s *SFTPStreamStore) Mkdir(path string, mode os.FileMode) error {
return err
}

func (s *SFTPStreamStore) OpenReadCloser(name string) (StrawReader, error) {
func (s *SFTPStreamStore) OpenReadCloser(name string) (straw.StrawReader, error) {
sr, err := s.sftpClient.Open(name)
if err != nil {
return nil, err
Expand All @@ -99,7 +106,7 @@ func (s *SFTPStreamStore) Remove(name string) error {
return err
}

func (s *SFTPStreamStore) CreateWriteCloser(name string) (StrawWriter, error) {
func (s *SFTPStreamStore) CreateWriteCloser(name string) (straw.StrawWriter, error) {
fi, err := s.Stat(name)
if err == nil && fi.IsDir() {
return nil, fmt.Errorf("%s is a directory", name)
Expand Down
23 changes: 1 addition & 22 deletions strawurl.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,29 +22,8 @@ func Open(u string) (StreamStore, error) {
}

func init() {
// the only "built in" backend is "file"
Register("file", func(u *url.URL) (StreamStore, error) {
return &OsStreamStore{}, nil
})
Register("gs", func(u *url.URL) (StreamStore, error) {
creds := u.Query().Get("credentialsfile")
if creds == "" {
return nil, fmt.Errorf("gs URLs must provide a `credentialsfile` parameter")
}
return NewGCSStreamStore(creds, u.Host)
})
Register("s3", func(u *url.URL) (StreamStore, error) {
sse := u.Query().Get("sse")
var opts []S3Option
switch sse {
case "":
case "AES256":
opts = append(opts, S3ServerSideEncoding(ServerSideEncryptionTypeAES256))
default:
return nil, fmt.Errorf("unknown server side encryption type '%s'", sse)
}
return NewS3StreamStore(u.Host, opts...)
})
Register("sftp", func(u *url.URL) (StreamStore, error) {
return NewSFTPStreamStore(u.String())
})
}