-
Notifications
You must be signed in to change notification settings - Fork 83
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[ATEAM-13094]: Implement Alibaba OSS backend (#223)
* [ATEAM-13094]: Adding backend implementation for Alibaba OSS * [ATEAM-13094]: Doing some code cleanup * [ATEAM-13094]: Doing some code cleanup * [ATEAM-13094]: Doing some code cleanup * [ATEAM-13094]: Doing some code cleanup * [ATEAM-13094]: More code cleanup * [ATEAM-13094]: More code cleanup * [ATEAM-13094]: More code cleanup using linter * [ATEAM-13094]: More code cleanup using linter * [ATEAM-13094]: Updated code for Alibaba OSS implementation * [ATEAM-13094]: Updated code for Alibaba OSS implementation * [ATEAM-13094]: Updated go.mod and go.sum * [ATEAM-13094]: Updated go.mod and go.sum * [ATEAM-13094]: Code cleanup after rebase off master * [ATEAM-13094]: Updated README.md, CHAANGELOG.md
- Loading branch information
Showing
12 changed files
with
227 additions
and
18 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,114 @@ | ||
package alioss | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"io" | ||
|
||
"github.com/aliyun/aliyun-oss-go-sdk/oss" | ||
"github.com/go-kit/log" | ||
"github.com/go-kit/log/level" | ||
"github.com/pkg/errors" | ||
) | ||
|
||
// Backend implements storage.Backend for Alibaba OSS storage. | ||
type Backend struct { | ||
logger log.Logger | ||
|
||
bucket string | ||
acl string | ||
encryption string | ||
client *oss.Client | ||
} | ||
|
||
func newAlibabaOss(bucket string, conf *oss.Config, opts ...oss.ClientOption) (*Backend, error) { | ||
client, err := oss.New(conf.Endpoint, conf.AccessKeyID, conf.AccessKeySecret, opts...) | ||
if err != nil { | ||
return nil, errors.Wrap(err, "could not create alibabaOSS client") | ||
} | ||
|
||
return &Backend{ | ||
bucket: bucket, | ||
client: client, | ||
}, nil | ||
} | ||
|
||
func New(l log.Logger, c Config, debug bool) (*Backend, error) { | ||
ossConf := &oss.Config{} | ||
if c.Endpoint != "" { | ||
ossConf.Endpoint = c.Endpoint | ||
} | ||
|
||
if c.AccesKeyID != "" { | ||
ossConf.AccessKeyID = c.AccesKeyID | ||
} | ||
|
||
if c.AccesKeySecret != "" { | ||
ossConf.AccessKeySecret = c.AccesKeySecret | ||
} | ||
|
||
if debug { | ||
level.Debug(l).Log("msg", "oss storage backend", "config", fmt.Sprintf("%+v", c)) | ||
} | ||
|
||
return newAlibabaOss(c.Bucket, ossConf) | ||
} | ||
|
||
func (c Backend) Get(ctx context.Context, p string, w io.Writer) error { | ||
bucket, err := c.client.Bucket(c.bucket) | ||
if err != nil { | ||
return errors.Wrap(err, "couldn't get the object") | ||
} | ||
|
||
reader, err := bucket.GetObject(p) | ||
if err != nil { | ||
return errors.Wrap(err, "couldn't get the object") | ||
} | ||
|
||
if reader != nil { | ||
level.Info(c.logger).Log("Successfully read fileobject") | ||
} | ||
|
||
return nil | ||
} | ||
|
||
func (c Backend) Put(ctx context.Context, p string, src io.Reader) error { | ||
bucket, err := c.client.Bucket(c.bucket) | ||
if err != nil { | ||
return errors.Wrap(err, "couldn't put the object") | ||
} | ||
|
||
options := []oss.Option{} | ||
|
||
if c.encryption != "" { | ||
option := oss.ServerSideEncryption(c.encryption) | ||
options = append(options, option) | ||
} | ||
|
||
if c.acl != "" { | ||
option := oss.ObjectACL(oss.ACLType(c.acl)) | ||
options = append(options, option) | ||
} | ||
|
||
if err := bucket.PutObject(p, src, options...); err != nil { | ||
return errors.Wrap(err, "couldn't put the object") | ||
} | ||
|
||
return nil | ||
} | ||
|
||
func (c Backend) Exists(ctx context.Context, p string) (bool, error) { | ||
bucket, err := c.client.Bucket(c.bucket) | ||
if err != nil { | ||
return false, errors.Wrap(err, "couldn't get the bucket object") | ||
} | ||
|
||
options := []oss.Option{} | ||
|
||
result, err := bucket.IsObjectExist(p, options...) | ||
if err != nil { | ||
return false, errors.Wrap(err, "couldn't get the object") | ||
} | ||
|
||
return result, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
package alioss | ||
|
||
import "testing" | ||
|
||
func TestAlibabaOss(t *testing.T) { | ||
t.Skip("skipping backend package tests") | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
package alioss | ||
|
||
// Config is a structure to store AlibabaOss backend configuration. | ||
type Config struct { | ||
Bucket string | ||
Endpoint string | ||
AccesKeyID string | ||
AccesKeySecret string | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters