Skip to content

Commit

Permalink
support CTYun OOS (#87)
Browse files Browse the repository at this point in the history
* support CTYun OOS

* change loglevl

Co-authored-by: liyong <chnliyong@gmail.com>
  • Loading branch information
davies and chnliyong authored Dec 30, 2020
1 parent 8eb3aa0 commit eef502a
Show file tree
Hide file tree
Showing 3 changed files with 79 additions and 0 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,7 @@ SRC and DST must be an URI of the following object storage:
- b2: Backblaze B2
- space: Digital Ocean Space
- obs: Huawei Object Storage Service
- oos: CTYun OOS

SRC and DST should be in the following format:

Expand Down
9 changes: 9 additions & 0 deletions object/object_storage_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -362,3 +362,12 @@ func TestHDFS(t *testing.T) {
dfs := newHDFS(os.Getenv("HDFS_ADDR"), "", "")
testStorage(t, dfs)
}

func TestOOS(t *testing.T) {
if os.Getenv("OOS_ACCESS_KEY") == "" {
t.SkipNow()
}
b := newOOS(fmt.Sprintf("https://%s", os.Getenv("OOS_TEST_BUCKET")),
os.Getenv("OOS_ACCESS_KEY"), os.Getenv("OOS_SECRET_KEY"))
testStorage(t, b)
}
69 changes: 69 additions & 0 deletions object/oos.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
// Copyright (C) 2018-present Juicedata Inc.

package object

import (
"fmt"
"net/url"
"strings"

"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/credentials"
"github.com/aws/aws-sdk-go/aws/session"
"github.com/aws/aws-sdk-go/service/s3"
)

type oos struct {
s3client
}

func (s *oos) String() string {
return fmt.Sprintf("oos://%s", s.s3client.bucket)
}

func (s *oos) Create() error {
_, err := s.List("", "", 1)
if err != nil {
return fmt.Errorf("please create bucket %s manually", s.s3client.bucket)
}
return err
}

func (s *oos) List(prefix, marker string, limit int64) ([]*Object, error) {
if limit > 1000 {
limit = 1000
}
objs, err := s.s3client.List(prefix, marker, limit)
if marker != "" && len(objs) > 0 && objs[0].Key == marker {
objs = objs[1:]
}
return objs, err
}

func newOOS(endpoint, accessKey, secretKey string) ObjectStorage {
uri, err := url.ParseRequestURI(endpoint)
if err != nil {
logger.Fatalf("Invalid endpoint %s: %s", endpoint, err)
}
ssl := strings.ToLower(uri.Scheme) == "https"
hostParts := strings.Split(uri.Host, ".")
bucket := hostParts[0]
region := hostParts[1][4:]
endpoint = uri.Host[len(bucket)+1:]

awsConfig := &aws.Config{
Region: &region,
Endpoint: &endpoint,
DisableSSL: aws.Bool(!ssl),
S3ForcePathStyle: aws.Bool(true),
// HTTPClient: httpClient,
Credentials: credentials.NewStaticCredentials(accessKey, secretKey, ""),
}

ses := session.New(awsConfig)
return &oos{s3client{bucket, s3.New(ses), ses}}
}

func init() {
register("oos", newOOS)
}

0 comments on commit eef502a

Please sign in to comment.