diff --git a/go.mod b/go.mod index 12b3a049..64c05651 100644 --- a/go.mod +++ b/go.mod @@ -55,6 +55,7 @@ require ( github.com/minio/md5-simd v1.1.2 // indirect github.com/minio/sha256-simd v1.0.1 // indirect github.com/mitchellh/copystructure v1.2.0 // indirect + github.com/mitchellh/go-homedir v1.1.0 // indirect github.com/mitchellh/go-testing-interface v1.14.1 // indirect github.com/mitchellh/go-wordwrap v1.0.1 // indirect github.com/mitchellh/mapstructure v1.5.0 // indirect diff --git a/go.sum b/go.sum index 68c7d630..f6dcfae5 100644 --- a/go.sum +++ b/go.sum @@ -136,6 +136,8 @@ github.com/minio/sha256-simd v1.0.1 h1:6kaan5IFmwTNynnKKpDHe6FWHohJOHhCPchzK49dz github.com/minio/sha256-simd v1.0.1/go.mod h1:Pz6AKMiUdngCLpeTL/RJY1M9rUuPMYujV5xJjtbRSN8= github.com/mitchellh/copystructure v1.2.0 h1:vpKXTN4ewci03Vljg/q9QvCGUDttBOGBIa15WveJJGw= github.com/mitchellh/copystructure v1.2.0/go.mod h1:qLl+cE2AmVv+CoeAwDPye/v+N2HKCj9FbZEVFJRxO9s= +github.com/mitchellh/go-homedir v1.1.0 h1:lukF9ziXFxDFPkA1vsr5zpc1XuPDn/wFntq5mG+4E0Y= +github.com/mitchellh/go-homedir v1.1.0/go.mod h1:SfyaCUpYCn1Vlf4IUYiD9fPX4A5wJrkLzIz1N1q0pr0= github.com/mitchellh/go-testing-interface v1.14.1 h1:jrgshOhYAUVNMAJiKbEu7EqAwgJJ2JqpQmpLJOu07cU= github.com/mitchellh/go-testing-interface v1.14.1/go.mod h1:gfgS7OtZj6MA4U1UrDRp04twqAjfvlZyCfX3sDjEym8= github.com/mitchellh/go-wordwrap v1.0.1 h1:TLuKupo69TCn6TQSyGxwI1EblZZEsQ0vMlAFQflz0v0= diff --git a/minio/resource_minio_s3_object.go b/minio/resource_minio_s3_object.go index 2a3e3539..75238aa3 100644 --- a/minio/resource_minio_s3_object.go +++ b/minio/resource_minio_s3_object.go @@ -5,11 +5,17 @@ import ( "context" "encoding/base64" "errors" + "fmt" + "io" + "log" + "os" + "path/filepath" + "github.com/hashicorp/terraform-plugin-sdk/v2/diag" "github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" "github.com/hashicorp/terraform-plugin-sdk/v2/helper/validation" "github.com/minio/minio-go/v7" - "io" + "github.com/mitchellh/go-homedir" ) func resourceMinioObject() *schema.Resource { @@ -78,7 +84,26 @@ func minioPutObject(ctx context.Context, d *schema.ResourceData, meta interface{ var body io.ReadSeeker - if v, ok := d.GetOk("content"); ok { + if v, ok := d.GetOk("source"); ok { + source := v.(string) + path, err := homedir.Expand(source) + if err != nil { + return NewResourceError(fmt.Sprintf("expanding homedir in source (%s)", source), d.Id(), err) + } + path = filepath.Clean(path) + file, err := os.Open(path) + if err != nil { + return NewResourceError(fmt.Sprintf("opening S3 object source (%s)", path), d.Id(), err) + } + + body = file + defer func() { + err := file.Close() + if err != nil { + log.Printf("[WARN] Error closing S3 object source (%s): %s", path, err) + } + }() + } else if v, ok := d.GetOk("content"); ok { content := v.(string) body = bytes.NewReader([]byte(content)) } else if v, ok := d.GetOk("content_base64"); ok { @@ -88,8 +113,6 @@ func minioPutObject(ctx context.Context, d *schema.ResourceData, meta interface{ return NewResourceError("error decoding content_base64", d.Id(), err) } body = bytes.NewReader(contentRaw) - } else if _, ok := d.GetOk("content_base64"); ok { - return NewResourceError("putting object failed", d.Id(), errors.New("sorry, unsupported yet")) } else { return NewResourceError("putting object failed", d.Id(), errors.New("one of source / content / content_base64 is not set")) }