From 2187920d58412999a87e30a6abf1480d8b7e497b Mon Sep 17 00:00:00 2001 From: Nathan LaFreniere Date: Fri, 4 Dec 2015 11:24:34 -0800 Subject: [PATCH] add support for website redirects --- DOCS.md | 18 ++++++++++++++++++ aws.go | 18 ++++++++++++++++++ main.go | 8 ++++++++ types.go | 21 +++++++++++---------- 4 files changed, 55 insertions(+), 10 deletions(-) diff --git a/DOCS.md b/DOCS.md index 00b379d..d56cbf8 100644 --- a/DOCS.md +++ b/DOCS.md @@ -10,6 +10,7 @@ Use the S3 sync plugin to synchronize files and folders with an Amazon S3 bucket * `delete` - deletes files in the target not found in the source * `content_type` - override default mime-types to use this value * `metadata` - set custom metadata +* `redirects` - targets that should redirect elsewhere The following is a sample S3 configuration in your .drone.yml file: @@ -87,3 +88,20 @@ publish: "*.html": Cache-Control: "max-age: 1000" ``` + +Additionally, you can specify redirect targets for files that don't exist by using the `redirects` key: + +```yaml +publish: + s3_sync: + acl: public-read + region: "us-east-1" + bucket: "my-bucket.s3-website-us-east-1.amazonaws.com" + access_key: "970d28f4dd477bc184fbd10b376de753" + secret_key: "9c5785d3ece6a9cdefa42eb99b58986f9095ff1c" + source: folder/to/archive + target: /target/location + delete: true + redirects: + some/missing/file: /somewhere/that/actually/exists +``` diff --git a/aws.go b/aws.go index 71a0084..aa45b27 100644 --- a/aws.go +++ b/aws.go @@ -231,6 +231,24 @@ func (a *AWS) visit(path string, info os.FileInfo, err error) error { return err } +func (a *AWS) AddRedirects(redirects map[string]string) error { + for path, location := range redirects { + fmt.Printf("Adding redirect from \"%s\" to \"%s\"", path, location) + a.local = append(a.local, strings.TrimPrefix(path, "/")) + _, err := a.client.PutObject(&s3.PutObjectInput{ + Bucket: aws.String(a.vargs.Bucket), + Key: aws.String(path), + WebsiteRedirectLocation: aws.String(location), + }) + + if err != nil { + return err + } + } + + return nil +} + func (a *AWS) List(path string) error { resp, err := a.client.ListObjects(&s3.ListObjectsInput{ Bucket: aws.String(a.vargs.Bucket), diff --git a/main.go b/main.go index 1978d73..9b57fa0 100644 --- a/main.go +++ b/main.go @@ -51,6 +51,14 @@ func main() { os.Exit(1) } + if len(vargs.Redirects) > 0 { + err = client.AddRedirects(vargs.Redirects) + if err != nil { + fmt.Println(err) + os.Exit(1) + } + } + if vargs.Delete { err = client.Cleanup() if err != nil { diff --git a/types.go b/types.go index e45c7d2..1e302eb 100644 --- a/types.go +++ b/types.go @@ -3,16 +3,17 @@ 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"` + 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"` } type DeepStringMap struct {