Skip to content

Commit

Permalink
1. Add swift object storage support, current only support "v1" auth m…
Browse files Browse the repository at this point in the history
…ethod (#115)

2. The bucket url (--bucket option) format is:   http://BucketName.1.2.3.4[:port]
  • Loading branch information
morrishe committed Jan 25, 2021
1 parent 103cd4e commit 8fcee47
Show file tree
Hide file tree
Showing 3 changed files with 107 additions and 0 deletions.
1 change: 1 addition & 0 deletions go.mod
Expand Up @@ -12,6 +12,7 @@ require (
github.com/hungys/go-lz4 v0.0.0-20170805124057-19ff7f07f099
github.com/juicedata/godaemon v0.0.0-20210118074000-659b6681b236
github.com/juicedata/juicesync v0.6.3-0.20210124085431-dc40ea723a9e
github.com/ncw/swift v1.0.53
github.com/sirupsen/logrus v1.7.0
github.com/urfave/cli/v2 v2.3.0
golang.org/x/sys v0.0.0-20200930185726-fdedc70b468f
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Expand Up @@ -141,6 +141,8 @@ github.com/mattn/go-isatty v0.0.4 h1:bnP0vzxcAdeI1zdubAl5PjU6zsERjGZb7raWodagDYs
github.com/mattn/go-isatty v0.0.4/go.mod h1:M+lRXTBqGeGNdLjl/ufCoiOlB5xdOkqRJdNxMWT7Zi4=
github.com/mozillazg/go-httpheader v0.2.1 h1:geV7TrjbL8KXSyvghnFm+NyTux/hxwueTSrwhe88TQQ=
github.com/mozillazg/go-httpheader v0.2.1/go.mod h1:jJ8xECTlalr6ValeXYdOF8fFUISeBAdw6E61aqQma60=
github.com/ncw/swift v1.0.53 h1:luHjjTNtekIEvHg5KdAFIBaH7bWfNkefwFnpDffSIks=
github.com/ncw/swift v1.0.53/go.mod h1:23YIA4yWVnGwv2dQlN4bB7egfYX6YLn0Yo/S6zZO/ZM=
github.com/nxadm/tail v1.4.4/go.mod h1:kenIhsEOeOJmVchQTgglprH7qJGnHDVpk1VPCcaMI8A=
github.com/onsi/ginkgo v1.6.0/go.mod h1:lLunBs/Ym6LB5Z9jYTR76FiuTmxDTDusOGeTQH+WWjE=
github.com/onsi/ginkgo v1.12.1/go.mod h1:zj2OWP4+oCPe1qIXoGWkgMRwljMUYCdkwsT2108oapk=
Expand Down
104 changes: 104 additions & 0 deletions pkg/object/swift.go
@@ -0,0 +1,104 @@
package object

import (
"bytes"
"fmt"
"io"
"io/ioutil"
"net/url"
"strings"

"github.com/ncw/swift"
)

type swiftOSS struct {
conn *swift.Connection
region string
storageUrl string
container string
}

func (s *swiftOSS) String() string {
return fmt.Sprintf("swift://%s", s.container)
}

func (s *swiftOSS) Create() error {
/* Connection.ContainerCreate():
* No error is returned if it already exists but the metadata if any will be updated.
*/
err := s.conn.ContainerCreate(s.container, nil)
return err
}

func (s *swiftOSS) Get(key string, off, limit int64) (io.ReadCloser, error) {
objOpenFile, _, err := s.conn.ObjectOpen(s.container, key, false, nil)
if err != nil {
return nil, err
}
if off > 0 {
_, err := objOpenFile.Seek(off, 0)
if err != nil {
objOpenFile.Close()
return nil, err
}
}
if limit > 0 {
defer objOpenFile.Close()
buf := make([]byte, limit)
if n, err := objOpenFile.Read(buf); err != nil {
return nil, err
} else {
return ioutil.NopCloser(bytes.NewBuffer(buf[:n])), nil
}
}
return objOpenFile, err
}

func (s *swiftOSS) Put(key string, in io.Reader) error {
_, err := s.conn.ObjectPut(s.container, key, in, false, "", "", nil)
return err
}

func (s *swiftOSS) Delete(key string) error {
err := s.conn.ObjectDelete(s.container, key)
return err
}

func newSwiftOSS(endpoint, accessKey, secretKey string) (ObjectStorage, error) {
uri, err := url.ParseRequestURI(endpoint)
if err != nil {
return nil, fmt.Errorf("Invalid endpoint %s: %s", endpoint, err)
}
//use 'http' or 'https"
if uri.Scheme != "http" && uri.Scheme != "https" {
return nil, fmt.Errorf("Invalid uri.Scheme: %s", uri.Scheme)
}

hostSlice := strings.SplitN(uri.Host, ".", 2)
if len(hostSlice) != 2 {
return nil, fmt.Errorf("Invalid uri.host: %s", uri.Host)
}
container := hostSlice[0]
host := hostSlice[1]

// current only support V1 authentication
auth_url := uri.Scheme + "://" + host + "/auth/v1.0"

//fmt.Printf("endpoint: %s\n", endpoint)
//fmt.Printf("connect to: %s, container: %s, auth_key: %s, ApiKey: *removed*\n", auth_url, container, accessKey)
conn := swift.Connection{
UserName: accessKey,
ApiKey: secretKey,
AuthUrl: auth_url,
}
// Authenticate
err = conn.Authenticate()
if err != nil {
return nil, fmt.Errorf("Auth failed: %s", err)
}
return &swiftOSS{&conn, conn.Region, conn.StorageUrl, container}, nil
}

func init() {
register("swift", newSwiftOSS)
}

0 comments on commit 8fcee47

Please sign in to comment.