github.com/viant/toolbox@v0.34.5/storage/gs/provider.go (about) 1 package gs 2 3 import ( 4 "context" 5 "encoding/json" 6 "github.com/viant/toolbox/cred" 7 "github.com/viant/toolbox/secret" 8 "github.com/viant/toolbox/storage" 9 "golang.org/x/oauth2/google" 10 "google.golang.org/api/option" 11 "os" 12 ) 13 14 const ProviderScheme = "gs" 15 const userAgent = "gcloud-golang-storage/20151204" 16 const DevstorageFullControlScope = "https://www.googleapis.com/auth/devstorage.full_control" 17 const googleStorageProjectKey = "GOOGLE_STORAGE_PROJECT" 18 19 func init() { 20 storage.Registry().Registry[ProviderScheme] = serviceProvider 21 } 22 23 func serviceProvider(credentials string) (storage.Service, error) { 24 var credentialOptions = make([]option.ClientOption, 0) 25 var projectID = os.Getenv("GOOGLE_CLOUD_PROJECT") 26 if credentials == "" { 27 credentialOptions = append([]option.ClientOption{}, 28 option.WithScopes(DevstorageFullControlScope), 29 option.WithUserAgent(userAgent)) 30 } else { 31 if json.Valid([]byte(credentials)) { 32 credentialOptions = append(credentialOptions, option.WithCredentialsJSON([]byte(credentials))) 33 } else { 34 credentialOptions = append(credentialOptions, option.WithCredentialsFile(credentials)) 35 } 36 secretService := secret.New("", false) 37 config, err := secretService.GetCredentials(credentials) 38 if err != nil { 39 return nil, err 40 } 41 projectID = config.ProjectID 42 } 43 44 if customProjectID := os.Getenv(googleStorageProjectKey); customProjectID != "" { 45 projectID = customProjectID 46 } 47 if projectID == "" { 48 if credentials, err := google.FindDefaultCredentials(context.Background(), DevstorageFullControlScope); err == nil { 49 projectID = credentials.ProjectID 50 } 51 } 52 return NewService(projectID, credentialOptions...), nil 53 } 54 55 func credServiceProvider(config *cred.Config) (storage.Service, error) { 56 var credentialOptions = make([]option.ClientOption, 0) 57 projectID := config.ProjectID 58 59 if config.Data == "" { 60 if data, err := json.Marshal(config); err == nil { 61 config.Data = string(data) 62 } 63 } 64 credentialOptions = append(credentialOptions, option.WithCredentialsJSON([]byte(config.Data))) 65 return NewService(projectID, credentialOptions...), nil 66 } 67 68 //SetProvider set gs provider with supplied config 69 func SetProvider(config *cred.Config) { 70 storage.Registry().Registry[ProviderScheme] = func(string) (storage.Service, error) { 71 return credServiceProvider(config) 72 } 73 }