11package config
22
33import (
4+ "bytes"
45 "encoding/json"
56 "errors"
67 "fmt"
78 "io"
9+ "os"
10+ "strconv"
811 "strings"
912)
1013
@@ -27,6 +30,7 @@ type S3Cli struct {
2730 HostStyle bool `json:"host_style"`
2831 SwiftAuthAccount string `json:"swift_auth_account"`
2932 SwiftTempURLKey string `json:"swift_temp_url_key"`
33+ Debug bool `json:"debug"`
3034 RequestChecksumCalculationEnabled bool
3135 ResponseChecksumCalculationEnabled bool
3236 UploaderRequestChecksumCalculationEnabled bool
@@ -74,6 +78,44 @@ func newStaticCredentialsPresentError(desiredSource string) error {
7478 return errorStaticCredentialsPresent {credentialsSource : desiredSource }
7579}
7680
81+ // NewReader provides an io.Reader on given configFile, using environment as fall-back.
82+ func NewReader (configFile * os.File ) (io.Reader , error ) {
83+ if configFile != nil {
84+ return configFile , nil
85+ }
86+ // Try reading config from env if no config file handle was provided
87+ port := 443
88+ if altPort , isset := os .LookupEnv ("S3_PORT" ); isset {
89+ var err error
90+ port , err = strconv .Atoi (altPort )
91+ if err != nil {
92+ return nil , err
93+ }
94+ }
95+ c := S3Cli {
96+ AccessKeyID : os .Getenv ("S3_ACCESS_KEY_ID" ),
97+ BucketName : os .Getenv ("S3_BUCKET_NAME" ),
98+ // Fixate CredentialSource to StaticCredentialsSource, making S3_ACCESS_KEY_ID & S3_SECRET_ACCESS_KEY required
99+ CredentialsSource : StaticCredentialsSource ,
100+ Host : os .Getenv ("S3_HOST" ),
101+ Port : port ,
102+ Region : os .Getenv ("S3_REGION" ),
103+ SecretAccessKey : os .Getenv ("S3_SECRET_ACCESS_KEY" ),
104+ SSLVerifyPeer : ! (os .Getenv ("S3_INSECURE_SSL" ) != "" ),
105+ // Use SSL/TLS/https:// unless S3_DISABLE_SSL is set
106+ UseSSL : ! (os .Getenv ("S3_DISABLE_SSL" ) != "" ),
107+ // Use PathStyle=true by default (endpoint/bucket instead of DNS bucket.endpoint), if S3_USE_HOSTSTYLE unset. See client/sdk.go
108+ HostStyle : os .Getenv ("S3_USE_HOSTSTYLE" ) != "" ,
109+ // Enable HTTP(S) request debugging if S3_DEBUG has non-empty value
110+ Debug : os .Getenv ("S3_DEBUG" ) != "" ,
111+ }
112+ json , err := json .Marshal (c )
113+ if err != nil {
114+ return nil , err
115+ }
116+ return bytes .NewReader (json ), nil
117+ }
118+
77119// NewFromReader returns a new s3cli configuration struct from the contents of reader.
78120// reader.Read() is expected to return valid JSON
79121func NewFromReader (reader io.Reader ) (S3Cli , error ) {
0 commit comments