NewClient creates a new S3 client. Credentials can be set in the options, but it's recommended to either use the shared credentials file (Linux: "~/.aws/credentials", Windows: "%UserProfile%\.aws\credentials") or environment variables (AWS_ACCESS_KEY_ID and AWS_SECRET_ACCESS_KEY). See https://githu
(options Options)
| 172 | // or environment variables (AWS_ACCESS_KEY_ID and AWS_SECRET_ACCESS_KEY). |
| 173 | // See https://github.com/awsdocs/aws-go-developer-guide/blob/0ae5712d120d43867cf81de875cb7505f62f2d71/doc_source/configuring-sdk.rst#specifying-credentials. |
| 174 | func NewClient(options Options) (Client, error) { |
| 175 | result := Client{} |
| 176 | |
| 177 | // Precondition check |
| 178 | if options.BucketName == "" { |
| 179 | return result, errors.New("the BucketName in the options must not be empty") |
| 180 | } |
| 181 | |
| 182 | // Set default values |
| 183 | if options.Codec == nil { |
| 184 | options.Codec = DefaultOptions.Codec |
| 185 | } |
| 186 | |
| 187 | // Set credentials only if set in the options. |
| 188 | // If not set, the SDK uses the shared credentials file or environment variables, which is the preferred way. |
| 189 | // Return an error if only one of the values is set. |
| 190 | var creds *credentials.Credentials |
| 191 | if (options.AWSaccessKeyID != "" && options.AWSsecretAccessKey == "") || (options.AWSaccessKeyID == "" && options.AWSsecretAccessKey != "") { |
| 192 | return result, errors.New("when passing credentials via options, you need to set BOTH AWSaccessKeyID AND AWSsecretAccessKey") |
| 193 | } else if options.AWSaccessKeyID != "" { |
| 194 | // Due to the previous check we can be sure that in this case AWSsecretAccessKey is not empty as well. |
| 195 | creds = credentials.NewStaticCredentials(options.AWSaccessKeyID, options.AWSsecretAccessKey, "") |
| 196 | } |
| 197 | |
| 198 | config := aws.NewConfig() |
| 199 | if options.Region != "" { |
| 200 | config = config.WithRegion(options.Region) |
| 201 | } |
| 202 | if creds != nil { |
| 203 | config = config.WithCredentials(creds) |
| 204 | } |
| 205 | if options.CustomEndpoint != "" { |
| 206 | config = config.WithEndpoint(options.CustomEndpoint) |
| 207 | } |
| 208 | if options.UsePathStyleAddressing { |
| 209 | config = config.WithS3ForcePathStyle(true) |
| 210 | } |
| 211 | // Use shared config file... |
| 212 | sessionOpts := session.Options{ |
| 213 | SharedConfigState: session.SharedConfigEnable, |
| 214 | } |
| 215 | // ...but allow overwrite of region and credentials if they are set in the options. |
| 216 | sessionOpts.Config.MergeIn(config) |
| 217 | session, err := session.NewSessionWithOptions(sessionOpts) |
| 218 | if err != nil { |
| 219 | return result, err |
| 220 | } |
| 221 | svc := awss3.New(session) |
| 222 | |
| 223 | // Create the bucket if it doesn't exist yet. |
| 224 | createBucketInput := awss3.CreateBucketInput{ |
| 225 | Bucket: aws.String(options.BucketName), |
| 226 | } |
| 227 | origS3 := options.CustomEndpoint == "" |
| 228 | err = createBucket(origS3, svc, createBucketInput, options.BucketName) |
| 229 | if err != nil { |
| 230 | return result, err |
| 231 | } |