diff --git a/client/sdk.go b/client/sdk.go index 3e6dd9d4..112139cf 100644 --- a/client/sdk.go +++ b/client/sdk.go @@ -86,6 +86,9 @@ func NewAwsS3ClientWithApiOptions( } // Apply custom middlewares if provided o.APIOptions = append(o.APIOptions, apiOptions...) + if c.Debug { + o.ClientLogMode = aws.LogResponse | aws.LogRequest + } }) return s3Client, nil diff --git a/config/config.go b/config/config.go index 143cc1bb..1f96138c 100644 --- a/config/config.go +++ b/config/config.go @@ -1,10 +1,13 @@ package config import ( + "bytes" "encoding/json" "errors" "fmt" "io" + "os" + "strconv" "strings" ) @@ -27,6 +30,7 @@ type S3Cli struct { HostStyle bool `json:"host_style"` SwiftAuthAccount string `json:"swift_auth_account"` SwiftTempURLKey string `json:"swift_temp_url_key"` + Debug bool `json:"debug"` requestChecksumCalculationEnabled bool uploaderRequestChecksumCalculationEnabled bool } @@ -58,6 +62,41 @@ func newStaticCredentialsPresentError(desiredSource string) error { return errorStaticCredentialsPresent{credentialsSource: desiredSource} } +// ConfigFromEnv pulls in config from environment variables instead of config.json. +// It returns a io.Reader to enable NewFromReader() to do regular validation as done for files. +func ConfigFromEnv() (io.Reader, error) { + port := 443 + if altPort, isset := os.LookupEnv("S3_PORT"); isset { + var err error + port, err = strconv.Atoi(altPort) + if err != nil { + return nil, err + } + } + c := S3Cli{ + AccessKeyID: os.Getenv("S3_ACCESS_KEY_ID"), + BucketName: os.Getenv("S3_BUCKET"), + // Fixate CredentialSource to StaticCredentialsSource, making S3_ACCESS_KEY_ID & S3_SECRET_ACCESS_KEY required + CredentialsSource: StaticCredentialsSource, + Host: os.Getenv("S3_HOST"), + Port: port, + Region: os.Getenv("S3_REGION"), + SecretAccessKey: os.Getenv("S3_SECRET_ACCESS_KEY"), + SSLVerifyPeer: !(os.Getenv("S3_INSECURE_SSL") != ""), + // Use SSL/TLS/https:// unless S3_DISABLE_SSL is set + UseSSL: !(os.Getenv("S3_DISABLE_SSL") != ""), + // Use PathStyle=true by default (endpoint/bucket instead of DNS bucket.endpoint), if S3_USE_HOSTSTYLE unset. See client/sdk.go + HostStyle: os.Getenv("S3_USE_HOSTSTYLE") != "", + // Enable HTTP(S) request debugging if S3_DEBUG has non-empty value + Debug: os.Getenv("S3_DEBUG") != "", + } + json, err := json.Marshal(c) + if err != nil { + return nil, err + } + return bytes.NewReader(json), nil +} + // NewFromReader returns a new s3cli configuration struct from the contents of reader. // reader.Read() is expected to return valid JSON func NewFromReader(reader io.Reader) (S3Cli, error) { diff --git a/main.go b/main.go index ea1c9ba8..51aee30a 100644 --- a/main.go +++ b/main.go @@ -3,6 +3,7 @@ package main import ( "flag" "fmt" + "io" "log" "os" "time" @@ -28,7 +29,13 @@ func main() { log.Fatalf("Expected at least two arguments got %d\n", len(nonFlagArgs)) } - configFile, err := os.Open(*configPath) + var configFile io.Reader + var err error + if *configPath == "" { + configFile, err = config.ConfigFromEnv() + } else { + configFile, err = os.Open(*configPath) + } if err != nil { log.Fatalln(err) }