Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions client/sdk.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
39 changes: 39 additions & 0 deletions config/config.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
package config

import (
"bytes"
"encoding/json"
"errors"
"fmt"
"io"
"os"
"strconv"
"strings"
)

Expand All @@ -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
}
Expand Down Expand Up @@ -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) {
Expand Down
9 changes: 8 additions & 1 deletion main.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package main
import (
"flag"
"fmt"
"io"
"log"
"os"
"time"
Expand All @@ -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)
}
Expand Down