| 83 | func (s *Storage) ResetStorageGeneration() error { return errors.New("not supported") } |
| 84 | |
| 85 | func newFromConfig(_ blobserver.Loader, config jsonconfig.Obj) (blobserver.Storage, error) { |
| 86 | var ( |
| 87 | auth = config.RequiredObject("auth") |
| 88 | bucket = config.RequiredString("bucket") |
| 89 | cacheSize = config.OptionalInt64("cacheSize", 32<<20) |
| 90 | |
| 91 | clientID = auth.RequiredString("client_id") // or "auto" for service accounts |
| 92 | clientSecret = auth.OptionalString("client_secret", "") |
| 93 | refreshToken = auth.OptionalString("refresh_token", "") |
| 94 | ) |
| 95 | |
| 96 | if err := config.Validate(); err != nil { |
| 97 | return nil, err |
| 98 | } |
| 99 | if err := auth.Validate(); err != nil { |
| 100 | return nil, err |
| 101 | } |
| 102 | |
| 103 | var dirPrefix string |
| 104 | if parts := strings.SplitN(bucket, "/", 2); len(parts) > 1 { |
| 105 | dirPrefix = parts[1] |
| 106 | bucket = parts[0] |
| 107 | } |
| 108 | if dirPrefix != "" && !strings.HasSuffix(dirPrefix, "/") { |
| 109 | dirPrefix += "/" |
| 110 | } |
| 111 | gs := &Storage{ |
| 112 | bucket: bucket, |
| 113 | dirPrefix: dirPrefix, |
| 114 | } |
| 115 | |
| 116 | var ( |
| 117 | ctx = context.Background() |
| 118 | ts oauth2.TokenSource |
| 119 | cl *storage.Client |
| 120 | err error |
| 121 | ) |
| 122 | if clientID == "auto" { |
| 123 | ts, err = google.DefaultTokenSource(ctx, storage.ScopeReadWrite) |
| 124 | if err != nil { |
| 125 | return nil, err |
| 126 | } |
| 127 | cl, err = storage.NewClient(ctx) |
| 128 | if err != nil { |
| 129 | return nil, err |
| 130 | } |
| 131 | } else { |
| 132 | if clientSecret == "" { |
| 133 | return nil, errors.New("missing required parameter 'client_secret'") |
| 134 | } |
| 135 | if refreshToken == "" { |
| 136 | return nil, errors.New("missing required parameter 'refresh_token'") |
| 137 | } |
| 138 | ts = oauthutil.NewRefreshTokenSource(&oauth2.Config{ |
| 139 | Scopes: []string{storage.ScopeReadWrite}, |
| 140 | Endpoint: google.Endpoint, |
| 141 | ClientID: clientID, |
| 142 | ClientSecret: clientSecret, |