0
0
mirror of https://github.com/thegeeklab/wp-s3-action.git synced 2024-11-28 19:30:35 +00:00

add support for website redirects

This commit is contained in:
Nathan LaFreniere 2015-12-04 11:24:34 -08:00
parent 5644941fde
commit 2187920d58
4 changed files with 55 additions and 10 deletions

18
DOCS.md
View File

@ -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 * `delete` - deletes files in the target not found in the source
* `content_type` - override default mime-types to use this value * `content_type` - override default mime-types to use this value
* `metadata` - set custom metadata * `metadata` - set custom metadata
* `redirects` - targets that should redirect elsewhere
The following is a sample S3 configuration in your .drone.yml file: The following is a sample S3 configuration in your .drone.yml file:
@ -87,3 +88,20 @@ publish:
"*.html": "*.html":
Cache-Control: "max-age: 1000" 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
```

18
aws.go
View File

@ -231,6 +231,24 @@ func (a *AWS) visit(path string, info os.FileInfo, err error) error {
return err 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 { func (a *AWS) List(path string) error {
resp, err := a.client.ListObjects(&s3.ListObjectsInput{ resp, err := a.client.ListObjects(&s3.ListObjectsInput{
Bucket: aws.String(a.vargs.Bucket), Bucket: aws.String(a.vargs.Bucket),

View File

@ -51,6 +51,14 @@ func main() {
os.Exit(1) 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 { if vargs.Delete {
err = client.Cleanup() err = client.Cleanup()
if err != nil { if err != nil {

View File

@ -13,6 +13,7 @@ type PluginArgs struct {
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"`
} }
type DeepStringMap struct { type DeepStringMap struct {