0
0
mirror of https://github.com/thegeeklab/wp-s3-action.git synced 2024-11-10 04:40:38 +00:00

Initial support for CloudFront

This commit is contained in:
Eric Anderson 2016-03-09 20:22:54 -05:00
parent f04be6f847
commit a64c25d742
3 changed files with 48 additions and 16 deletions

23
aws.go
View File

@ -7,17 +7,20 @@ 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
cfClient *cloudfront.CloudFront
remote []string
local []string
vargs PluginArgs
@ -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
}

10
main.go
View File

@ -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
}

View File

@ -14,6 +14,7 @@ type PluginArgs struct {
ContentType StringMap `json:"content_type"`
Metadata DeepStringMap `json:"metadata"`
Redirects map[string]string `json:"redirects"`
CloudFrontDistribution string `json:"cloudfront_distribution_id"`
}
type DeepStringMap struct {