diff --git a/aws.go b/aws.go index 1f0cff8..56ddd41 100644 --- a/aws.go +++ b/aws.go @@ -7,20 +7,23 @@ import ( "mime" "os" "path/filepath" + "time" "github.com/aws/aws-sdk-go/aws" "github.com/aws/aws-sdk-go/aws/awserr" "github.com/aws/aws-sdk-go/aws/credentials" "github.com/aws/aws-sdk-go/aws/session" + "github.com/aws/aws-sdk-go/service/cloudFront" "github.com/aws/aws-sdk-go/service/s3" "github.com/ryanuber/go-glob" ) type AWS struct { - client *s3.S3 - remote []string - local []string - vargs PluginArgs + client *s3.S3 + cfClient *cloudfront.CloudFront + remote []string + local []string + vargs PluginArgs } func NewAWS(vargs PluginArgs) AWS { @@ -29,10 +32,11 @@ func NewAWS(vargs PluginArgs) AWS { Region: aws.String(vargs.Region), }) c := s3.New(sess) + cf := cloudfront.New(sess) r := make([]string, 1, 1) l := make([]string, 1, 1) - return AWS{c, r, l, vargs} + return AWS{c, cf, r, l, vargs} } func (a *AWS) Upload(local, remote string) error { @@ -270,3 +274,20 @@ func (a *AWS) List(path string) ([]string, error) { return remote, nil } + +func (a *AWS) Invalidate(invalidatePath string) error { + debug("Invalidating \"%s\"", invalidatePath) + _, err := a.cfClient.CreateInvalidation(&cloudfront.CreateInvalidationInput{ + DistributionId: aws.String(a.vargs.CloudFrontDistribution), + InvalidationBatch: &cloudfront.InvalidationBatch{ + CallerReference: aws.String(time.Now().Format(time.RFC3339Nano)), + Paths: &cloudfront.Paths{ + Quantity: aws.Int64(1), + Items: []*string{ + aws.String(invalidatePath), + }, + }, + }, + }) + return err +} diff --git a/main.go b/main.go index b9469c0..6173976 100644 --- a/main.go +++ b/main.go @@ -120,6 +120,14 @@ func main() { } } + if len(vargs.CloudFrontDistribution) > 0 { + jobs = append(jobs, job{ + local: "", + remote: filepath.Join("/", vargs.Target, "*"), + action: "invalidateCloudFront", + }) + } + jobChan := make(chan struct{}, maxConcurrent) results := make(chan *result, len(jobs)) @@ -133,6 +141,8 @@ func main() { err = client.Redirect(j.local, j.remote) } else if j.action == "delete" && vargs.Delete { err = client.Delete(j.remote) + } else if j.action == "invalidateCloudFront" { + client.Invalidate(j.remote) } else { err = nil } diff --git a/types.go b/types.go index 1e302eb..dde55dc 100644 --- a/types.go +++ b/types.go @@ -3,17 +3,18 @@ package main import "encoding/json" type PluginArgs struct { - Key string `json:"access_key"` - Secret string `json:"secret_key"` - Bucket string `json:"bucket"` - Region string `json:"region"` - Source string `json:"source"` - Target string `json:"target"` - Delete bool `json:"delete"` - Access StringMap `json:"acl"` - ContentType StringMap `json:"content_type"` - Metadata DeepStringMap `json:"metadata"` - Redirects map[string]string `json:"redirects"` + Key string `json:"access_key"` + Secret string `json:"secret_key"` + Bucket string `json:"bucket"` + Region string `json:"region"` + Source string `json:"source"` + Target string `json:"target"` + Delete bool `json:"delete"` + Access StringMap `json:"acl"` + ContentType StringMap `json:"content_type"` + Metadata DeepStringMap `json:"metadata"` + Redirects map[string]string `json:"redirects"` + CloudFrontDistribution string `json:"cloudfront_distribution_id"` } type DeepStringMap struct {