1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46
|
package s3
import (
"github.com/viant/toolbox/cred"
"os"
"path"
"path/filepath"
"strings"
"github.com/viant/toolbox/storage"
"github.com/viant/toolbox/url"
)
const ProviderScheme = "s3"
func init() {
SetDefaultProvider()
}
func serviceProvider(credentialFile string) (storage.Service, error) {
s3config := &cred.Config{}
if credentialFile != "" {
if !strings.HasPrefix(credentialFile, "/") {
dir, _ := filepath.Abs(filepath.Dir(os.Args[0]))
credentialFile = path.Join(dir, credentialFile)
}
resource := url.NewResource(credentialFile)
err := resource.Decode(s3config)
if err != nil {
return nil, err
}
}
return NewService(s3config), nil
}
//SetProvider set s3 provider with dynamic credentials
func SetDefaultProvider() {
storage.Registry().Registry[ProviderScheme] = serviceProvider
}
//SetProvider set s3 provider with supplied config
func SetProvider(config *cred.Config) {
storage.Registry().Registry[ProviderScheme] = func(string) (storage.Service, error) {
return NewService(config), nil
}
}
|