|
1 | 1 | package awsconfig
|
2 | 2 |
|
3 | 3 | import (
|
| 4 | + "context" |
4 | 5 | "flag"
|
5 | 6 |
|
6 |
| - "github.com/aws/aws-sdk-go/aws" |
7 |
| - "github.com/aws/aws-sdk-go/aws/credentials" |
8 |
| - "github.com/aws/aws-sdk-go/aws/session" |
| 7 | + "github.com/aws/aws-sdk-go-v2/aws" |
| 8 | + "github.com/aws/aws-sdk-go-v2/config" |
| 9 | + "github.com/aws/aws-sdk-go-v2/credentials" |
| 10 | + "github.com/aws/aws-sdk-go-v2/service/s3" |
9 | 11 | "github.com/cshum/imagor"
|
10 | 12 | "github.com/cshum/imagor/storage/s3storage"
|
11 | 13 | "go.uber.org/zap"
|
@@ -100,95 +102,138 @@ func WithAWS(fs *flag.FlagSet, cb func() (*zap.Logger, bool)) imagor.Option {
|
100 | 102 | if *s3StorageBucket == "" && *s3LoaderBucket == "" && *s3ResultStorageBucket == "" {
|
101 | 103 | return
|
102 | 104 | }
|
103 |
| - var loaderSess, storageSess, resultStorageSess *session.Session |
104 |
| - var cred = credentials.NewStaticCredentials( |
105 |
| - *awsAccessKeyID, *awsSecretAccessKey, *awsSessionToken) |
106 |
| - var options = session.Options{ |
107 |
| - SharedConfigState: session.SharedConfigEnable, |
| 105 | + |
| 106 | + ctx := context.Background() |
| 107 | + |
| 108 | + // Helper function to create S3 client with custom endpoint |
| 109 | + createS3Client := func(cfg aws.Config, endpoint string) *s3.Client { |
| 110 | + var options []func(*s3.Options) |
| 111 | + |
| 112 | + if endpoint != "" { |
| 113 | + options = append(options, func(o *s3.Options) { |
| 114 | + o.BaseEndpoint = aws.String(endpoint) |
| 115 | + }) |
| 116 | + } |
| 117 | + |
| 118 | + if *s3ForcePathStyle { |
| 119 | + options = append(options, func(o *s3.Options) { |
| 120 | + o.UsePathStyle = true |
| 121 | + }) |
| 122 | + } |
| 123 | + |
| 124 | + return s3.NewFromConfig(cfg, options...) |
108 | 125 | }
|
109 |
| - if _, err := cred.Get(); err != nil { |
110 |
| - cred = credentials.NewSharedCredentials("", "") |
| 126 | + |
| 127 | + // Create base configuration |
| 128 | + var loaderCfg, storageCfg, resultStorageCfg aws.Config |
| 129 | + var err error |
| 130 | + |
| 131 | + // Default configuration |
| 132 | + defaultCfg, err := config.LoadDefaultConfig(ctx) |
| 133 | + if err != nil { |
| 134 | + panic(err) |
111 | 135 | }
|
112 |
| - if _, err := cred.Get(); err == nil { |
113 |
| - options.Config = aws.Config{ |
114 |
| - Endpoint: s3Endpoint, |
115 |
| - Region: awsRegion, |
116 |
| - Credentials: cred, |
117 |
| - S3ForcePathStyle: s3ForcePathStyle, |
118 |
| - } |
| 136 | + |
| 137 | + // Override with explicit credentials if provided |
| 138 | + if *awsAccessKeyID != "" && *awsSecretAccessKey != "" { |
| 139 | + defaultCfg.Credentials = credentials.NewStaticCredentialsProvider( |
| 140 | + *awsAccessKeyID, *awsSecretAccessKey, *awsSessionToken) |
119 | 141 | }
|
120 |
| - var sess = session.Must(session.NewSessionWithOptions(options)) |
121 |
| - loaderSess = sess |
122 |
| - storageSess = sess |
123 |
| - resultStorageSess = sess |
| 142 | + if *awsRegion != "" { |
| 143 | + defaultCfg.Region = *awsRegion |
| 144 | + } |
| 145 | + |
| 146 | + // Set default configurations |
| 147 | + loaderCfg = defaultCfg |
| 148 | + storageCfg = defaultCfg |
| 149 | + resultStorageCfg = defaultCfg |
| 150 | + |
| 151 | + // Override loader config if specific credentials provided |
124 | 152 | if *awsLoaderRegion != "" && *awsLoaderAccessKeyID != "" && *awsLoaderSecretAccessKey != "" {
|
125 |
| - cfg := &aws.Config{ |
126 |
| - Endpoint: s3LoaderEndpoint, |
127 |
| - Region: awsLoaderRegion, |
128 |
| - Credentials: credentials.NewStaticCredentials( |
| 153 | + loaderCfg = aws.Config{ |
| 154 | + Region: *awsLoaderRegion, |
| 155 | + Credentials: credentials.NewStaticCredentialsProvider( |
129 | 156 | *awsLoaderAccessKeyID, *awsLoaderSecretAccessKey, *awsLoaderSessionToken),
|
130 |
| - S3ForcePathStyle: s3ForcePathStyle, |
131 | 157 | }
|
132 |
| - // activate AWS Session only if credentials present |
133 |
| - loaderSess = session.Must(session.NewSession(cfg)) |
134 | 158 | }
|
| 159 | + |
| 160 | + // Override storage config if specific credentials provided |
135 | 161 | if *awsStorageRegion != "" && *awsStorageAccessKeyID != "" && *awsStorageSecretAccessKey != "" {
|
136 |
| - cfg := &aws.Config{ |
137 |
| - Endpoint: s3StorageEndpoint, |
138 |
| - Region: awsStorageRegion, |
139 |
| - Credentials: credentials.NewStaticCredentials( |
| 162 | + storageCfg = aws.Config{ |
| 163 | + Region: *awsStorageRegion, |
| 164 | + Credentials: credentials.NewStaticCredentialsProvider( |
140 | 165 | *awsStorageAccessKeyID, *awsStorageSecretAccessKey, *awsStorageSessionToken),
|
141 |
| - S3ForcePathStyle: s3ForcePathStyle, |
142 | 166 | }
|
143 |
| - // activate AWS Session only if credentials present |
144 |
| - storageSess = session.Must(session.NewSession(cfg)) |
145 | 167 | }
|
| 168 | + |
| 169 | + // Override result storage config if specific credentials provided |
146 | 170 | if *awsResultStorageRegion != "" && *awsResultStorageAccessKeyID != "" && *awsResultStorageSecretAccessKey != "" {
|
147 |
| - cfg := &aws.Config{ |
148 |
| - Endpoint: s3ResultStorageEndpoint, |
149 |
| - Region: awsResultStorageRegion, |
150 |
| - Credentials: credentials.NewStaticCredentials( |
| 171 | + resultStorageCfg = aws.Config{ |
| 172 | + Region: *awsResultStorageRegion, |
| 173 | + Credentials: credentials.NewStaticCredentialsProvider( |
151 | 174 | *awsResultStorageAccessKeyID, *awsResultStorageSecretAccessKey, *awsResultStorageSessionToken),
|
152 |
| - S3ForcePathStyle: s3ForcePathStyle, |
153 | 175 | }
|
154 |
| - // activate AWS Session only if credentials present |
155 |
| - resultStorageSess = session.Must(session.NewSession(cfg)) |
156 | 176 | }
|
157 |
| - if storageSess != nil && *s3StorageBucket != "" { |
158 |
| - // activate S3 Storage only if bucket config presents |
159 |
| - app.Storages = append(app.Storages, |
160 |
| - s3storage.New(storageSess, *s3StorageBucket, |
161 |
| - s3storage.WithPathPrefix(*s3StoragePathPrefix), |
162 |
| - s3storage.WithBaseDir(*s3StorageBaseDir), |
163 |
| - s3storage.WithACL(*s3StorageACL), |
164 |
| - s3storage.WithSafeChars(*s3SafeChars), |
165 |
| - s3storage.WithExpiration(*s3StorageExpiration), |
166 |
| - s3storage.WithStorageClass(*s3StorageClass), |
167 |
| - ), |
| 177 | + |
| 178 | + // Create S3 Storage instances |
| 179 | + if *s3StorageBucket != "" { |
| 180 | + // Determine endpoint: service-specific takes priority over global |
| 181 | + endpoint := *s3StorageEndpoint |
| 182 | + if endpoint == "" { |
| 183 | + endpoint = *s3Endpoint |
| 184 | + } |
| 185 | + |
| 186 | + storage := s3storage.New(storageCfg, *s3StorageBucket, |
| 187 | + s3storage.WithPathPrefix(*s3StoragePathPrefix), |
| 188 | + s3storage.WithBaseDir(*s3StorageBaseDir), |
| 189 | + s3storage.WithACL(*s3StorageACL), |
| 190 | + s3storage.WithSafeChars(*s3SafeChars), |
| 191 | + s3storage.WithExpiration(*s3StorageExpiration), |
| 192 | + s3storage.WithStorageClass(*s3StorageClass), |
168 | 193 | )
|
| 194 | + // Override client with custom endpoint if needed |
| 195 | + storage.Client = createS3Client(storageCfg, endpoint) |
| 196 | + |
| 197 | + app.Storages = append(app.Storages, storage) |
169 | 198 | }
|
170 |
| - if loaderSess != nil && *s3LoaderBucket != "" { |
171 |
| - // activate S3 Loader only if bucket config presents |
172 |
| - app.Loaders = append(app.Loaders, |
173 |
| - s3storage.New(loaderSess, *s3LoaderBucket, |
174 |
| - s3storage.WithPathPrefix(*s3LoaderPathPrefix), |
175 |
| - s3storage.WithBaseDir(*s3LoaderBaseDir), |
176 |
| - s3storage.WithSafeChars(*s3SafeChars), |
177 |
| - ), |
| 199 | + |
| 200 | + if *s3LoaderBucket != "" { |
| 201 | + // Determine endpoint: service-specific takes priority over global |
| 202 | + endpoint := *s3LoaderEndpoint |
| 203 | + if endpoint == "" { |
| 204 | + endpoint = *s3Endpoint |
| 205 | + } |
| 206 | + |
| 207 | + loader := s3storage.New(loaderCfg, *s3LoaderBucket, |
| 208 | + s3storage.WithPathPrefix(*s3LoaderPathPrefix), |
| 209 | + s3storage.WithBaseDir(*s3LoaderBaseDir), |
| 210 | + s3storage.WithSafeChars(*s3SafeChars), |
178 | 211 | )
|
| 212 | + // Override client with custom endpoint if needed |
| 213 | + loader.Client = createS3Client(loaderCfg, endpoint) |
| 214 | + |
| 215 | + app.Loaders = append(app.Loaders, loader) |
179 | 216 | }
|
180 |
| - if resultStorageSess != nil && *s3ResultStorageBucket != "" { |
181 |
| - // activate S3 ResultStorage only if bucket config presents |
182 |
| - app.ResultStorages = append(app.ResultStorages, |
183 |
| - s3storage.New(resultStorageSess, *s3ResultStorageBucket, |
184 |
| - s3storage.WithPathPrefix(*s3ResultStoragePathPrefix), |
185 |
| - s3storage.WithBaseDir(*s3ResultStorageBaseDir), |
186 |
| - s3storage.WithACL(*s3ResultStorageACL), |
187 |
| - s3storage.WithSafeChars(*s3SafeChars), |
188 |
| - s3storage.WithExpiration(*s3ResultStorageExpiration), |
189 |
| - s3storage.WithStorageClass(*s3StorageClass), |
190 |
| - ), |
| 217 | + |
| 218 | + if *s3ResultStorageBucket != "" { |
| 219 | + // Determine endpoint: service-specific takes priority over global |
| 220 | + endpoint := *s3ResultStorageEndpoint |
| 221 | + if endpoint == "" { |
| 222 | + endpoint = *s3Endpoint |
| 223 | + } |
| 224 | + |
| 225 | + resultStorage := s3storage.New(resultStorageCfg, *s3ResultStorageBucket, |
| 226 | + s3storage.WithPathPrefix(*s3ResultStoragePathPrefix), |
| 227 | + s3storage.WithBaseDir(*s3ResultStorageBaseDir), |
| 228 | + s3storage.WithACL(*s3ResultStorageACL), |
| 229 | + s3storage.WithSafeChars(*s3SafeChars), |
| 230 | + s3storage.WithExpiration(*s3ResultStorageExpiration), |
| 231 | + s3storage.WithStorageClass(*s3StorageClass), |
191 | 232 | )
|
| 233 | + // Override client with custom endpoint if needed |
| 234 | + resultStorage.Client = createS3Client(resultStorageCfg, endpoint) |
| 235 | + |
| 236 | + app.ResultStorages = append(app.ResultStorages, resultStorage) |
192 | 237 | }
|
193 | 238 | }
|
194 | 239 | }
|
0 commit comments