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

31
aws.go
View File

@ -7,20 +7,23 @@ import (
"mime" "mime"
"os" "os"
"path/filepath" "path/filepath"
"time"
"github.com/aws/aws-sdk-go/aws" "github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/awserr" "github.com/aws/aws-sdk-go/aws/awserr"
"github.com/aws/aws-sdk-go/aws/credentials" "github.com/aws/aws-sdk-go/aws/credentials"
"github.com/aws/aws-sdk-go/aws/session" "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/aws/aws-sdk-go/service/s3"
"github.com/ryanuber/go-glob" "github.com/ryanuber/go-glob"
) )
type AWS struct { type AWS struct {
client *s3.S3 client *s3.S3
remote []string cfClient *cloudfront.CloudFront
local []string remote []string
vargs PluginArgs local []string
vargs PluginArgs
} }
func NewAWS(vargs PluginArgs) AWS { func NewAWS(vargs PluginArgs) AWS {
@ -29,10 +32,11 @@ func NewAWS(vargs PluginArgs) AWS {
Region: aws.String(vargs.Region), Region: aws.String(vargs.Region),
}) })
c := s3.New(sess) c := s3.New(sess)
cf := cloudfront.New(sess)
r := make([]string, 1, 1) r := make([]string, 1, 1)
l := 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 { func (a *AWS) Upload(local, remote string) error {
@ -270,3 +274,20 @@ func (a *AWS) List(path string) ([]string, error) {
return remote, nil 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) jobChan := make(chan struct{}, maxConcurrent)
results := make(chan *result, len(jobs)) results := make(chan *result, len(jobs))
@ -133,6 +141,8 @@ func main() {
err = client.Redirect(j.local, j.remote) err = client.Redirect(j.local, j.remote)
} else if j.action == "delete" && vargs.Delete { } else if j.action == "delete" && vargs.Delete {
err = client.Delete(j.remote) err = client.Delete(j.remote)
} else if j.action == "invalidateCloudFront" {
client.Invalidate(j.remote)
} else { } else {
err = nil err = nil
} }

View File

@ -3,17 +3,18 @@ package main
import "encoding/json" import "encoding/json"
type PluginArgs struct { type PluginArgs struct {
Key string `json:"access_key"` Key string `json:"access_key"`
Secret string `json:"secret_key"` Secret string `json:"secret_key"`
Bucket string `json:"bucket"` Bucket string `json:"bucket"`
Region string `json:"region"` Region string `json:"region"`
Source string `json:"source"` Source string `json:"source"`
Target string `json:"target"` Target string `json:"target"`
Delete bool `json:"delete"` Delete bool `json:"delete"`
Access StringMap `json:"acl"` Access StringMap `json:"acl"`
ContentType StringMap `json:"content_type"` ContentType StringMap `json:"content_type"`
Metadata DeepStringMap `json:"metadata"` Metadata DeepStringMap `json:"metadata"`
Redirects map[string]string `json:"redirects"` Redirects map[string]string `json:"redirects"`
CloudFrontDistribution string `json:"cloudfront_distribution_id"`
} }
type DeepStringMap struct { type DeepStringMap struct {