0
0
mirror of https://github.com/thegeeklab/wp-s3-action.git synced 2024-11-21 14:50:39 +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"
"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
}

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

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