devlopment branch (#1736)
This commit is contained in:
17
pkg/simple/client/s3/interface.go
Normal file
17
pkg/simple/client/s3/interface.go
Normal file
@@ -0,0 +1,17 @@
|
||||
package s3
|
||||
|
||||
import (
|
||||
"io"
|
||||
"time"
|
||||
)
|
||||
|
||||
type Interface interface {
|
||||
// Upload uploads a object to storage and returns object location if succeeded
|
||||
Upload(key string, body io.Reader) (string, error)
|
||||
|
||||
// Get retrieves and object's downloadable location if succeeded
|
||||
Get(key string, fileName string, expire time.Duration) (string, error)
|
||||
|
||||
// Delete deletes an object by its key
|
||||
Delete(key string) error
|
||||
}
|
||||
69
pkg/simple/client/s3/options.go
Normal file
69
pkg/simple/client/s3/options.go
Normal file
@@ -0,0 +1,69 @@
|
||||
package s3
|
||||
|
||||
import (
|
||||
"github.com/spf13/pflag"
|
||||
"kubesphere.io/kubesphere/pkg/utils/reflectutils"
|
||||
)
|
||||
|
||||
// Options contains configuration to access a s3 service
|
||||
type Options struct {
|
||||
Endpoint string `json:"endpoint,omitempty" yaml:"endpoint"`
|
||||
Region string `json:"region,omitempty" yaml:"region"`
|
||||
DisableSSL bool `json:"disableSSL" yaml:"disableSSL"`
|
||||
ForcePathStyle bool `json:"forcePathStyle" yaml:"forcePathStyle"`
|
||||
AccessKeyID string `json:"accessKeyID,omitempty" yaml:"accessKeyID"`
|
||||
SecretAccessKey string `json:"secretAccessKey,omitempty" yaml:"secretAccessKey"`
|
||||
SessionToken string `json:"sessionToken,omitempty" yaml:"sessionToken"`
|
||||
Bucket string `json:"bucket,omitempty" yaml:"bucket"`
|
||||
}
|
||||
|
||||
// NewS3Options creates a default disabled Options(empty endpoint)
|
||||
func NewS3Options() *Options {
|
||||
return &Options{
|
||||
Endpoint: "",
|
||||
Region: "us-east-1",
|
||||
DisableSSL: true,
|
||||
ForcePathStyle: true,
|
||||
AccessKeyID: "AKIAIOSFODNN7EXAMPLE",
|
||||
SecretAccessKey: "wJalrXUtnFEMI/K7MDENG/bPxRfiCYEXAMPLEKEY",
|
||||
SessionToken: "",
|
||||
Bucket: "s2i-binaries",
|
||||
}
|
||||
}
|
||||
|
||||
// Validate check options values
|
||||
func (s *Options) Validate() []error {
|
||||
var errors []error
|
||||
|
||||
return errors
|
||||
}
|
||||
|
||||
// ApplyTo overrides options if it's valid, which endpoint is not empty
|
||||
func (s *Options) ApplyTo(options *Options) {
|
||||
if s.Endpoint != "" {
|
||||
reflectutils.Override(options, s)
|
||||
}
|
||||
}
|
||||
|
||||
// AddFlags add options flags to command line flags,
|
||||
// if s3-endpoint if left empty, following options will be ignored
|
||||
func (s *Options) AddFlags(fs *pflag.FlagSet, c *Options) {
|
||||
fs.StringVar(&s.Endpoint, "s3-endpoint", c.Endpoint, ""+
|
||||
"Endpoint to access to s3 object storage service, if left blank, the following options "+
|
||||
"will be ignored.")
|
||||
|
||||
fs.StringVar(&s.Region, "s3-region", c.Region, ""+
|
||||
"Region of s3 that will access to, like us-east-1.")
|
||||
|
||||
fs.StringVar(&s.AccessKeyID, "s3-access-key-id", c.AccessKeyID, "access key of s2i s3")
|
||||
|
||||
fs.StringVar(&s.SecretAccessKey, "s3-secret-access-key", c.SecretAccessKey, "secret access key of s2i s3")
|
||||
|
||||
fs.StringVar(&s.SessionToken, "s3-session-token", c.SessionToken, "session token of s2i s3")
|
||||
|
||||
fs.StringVar(&s.Bucket, "s3-bucket", c.Bucket, "bucket name of s2i s3")
|
||||
|
||||
fs.BoolVar(&s.DisableSSL, "s3-disable-SSL", c.DisableSSL, "disable ssl")
|
||||
|
||||
fs.BoolVar(&s.ForcePathStyle, "s3-force-path-style", c.ForcePathStyle, "force path style")
|
||||
}
|
||||
93
pkg/simple/client/s3/s3.go
Normal file
93
pkg/simple/client/s3/s3.go
Normal file
@@ -0,0 +1,93 @@
|
||||
package s3
|
||||
|
||||
import (
|
||||
"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"
|
||||
"io"
|
||||
"k8s.io/klog"
|
||||
"time"
|
||||
)
|
||||
|
||||
type Client struct {
|
||||
s3Client *s3.S3
|
||||
s3Session *session.Session
|
||||
bucket string
|
||||
}
|
||||
|
||||
func (s *Client) Upload(key string, body io.Reader) (string, error) {
|
||||
panic("implement me")
|
||||
}
|
||||
|
||||
func (s *Client) Get(key string, fileName string, expire time.Duration) (string, error) {
|
||||
panic("implement me")
|
||||
}
|
||||
|
||||
func (s *Client) Delete(key string) error {
|
||||
panic("implement me")
|
||||
}
|
||||
|
||||
func NewS3Client(options *Options) (Interface, error) {
|
||||
cred := credentials.NewStaticCredentials(options.AccessKeyID, options.SecretAccessKey, options.SessionToken)
|
||||
|
||||
config := aws.Config{
|
||||
Region: aws.String(options.Region),
|
||||
Endpoint: aws.String(options.Endpoint),
|
||||
DisableSSL: aws.Bool(options.DisableSSL),
|
||||
S3ForcePathStyle: aws.Bool(options.ForcePathStyle),
|
||||
Credentials: cred,
|
||||
}
|
||||
|
||||
s, err := session.NewSession(&config)
|
||||
if err != nil {
|
||||
klog.Error(err)
|
||||
return nil, err
|
||||
}
|
||||
|
||||
var c Client
|
||||
|
||||
c.s3Client = s3.New(s)
|
||||
c.s3Session = s
|
||||
c.bucket = options.Bucket
|
||||
|
||||
return &c, nil
|
||||
}
|
||||
|
||||
// NewS3ClientOrDie creates Client and panics if there is an error
|
||||
func NewS3ClientOrDie(options *Options) Interface {
|
||||
cred := credentials.NewStaticCredentials(options.AccessKeyID, options.SecretAccessKey, options.SessionToken)
|
||||
|
||||
config := aws.Config{
|
||||
Region: aws.String(options.Region),
|
||||
Endpoint: aws.String(options.Endpoint),
|
||||
DisableSSL: aws.Bool(options.DisableSSL),
|
||||
S3ForcePathStyle: aws.Bool(options.ForcePathStyle),
|
||||
Credentials: cred,
|
||||
}
|
||||
|
||||
s, err := session.NewSession(&config)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
client := s3.New(s)
|
||||
|
||||
return &Client{
|
||||
s3Client: client,
|
||||
s3Session: s,
|
||||
bucket: options.Bucket,
|
||||
}
|
||||
}
|
||||
|
||||
func (s *Client) Client() *s3.S3 {
|
||||
|
||||
return s.s3Client
|
||||
}
|
||||
func (s *Client) Session() *session.Session {
|
||||
return s.s3Session
|
||||
}
|
||||
|
||||
func (s *Client) Bucket() *string {
|
||||
return aws.String(s.bucket)
|
||||
}
|
||||
Reference in New Issue
Block a user