-
Notifications
You must be signed in to change notification settings - Fork 5.8k
/
Copy paths3_set_bucket_website.go
82 lines (69 loc) · 2.04 KB
/
s3_set_bucket_website.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
// SPDX-License-Identifier: Apache-2.0
package main
import (
"fmt"
"os"
"path/filepath"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/session"
"github.com/aws/aws-sdk-go/service/s3"
)
// Sets the bucket's website configuration. Allows setting the index suffix,
// and an optional error page keys.
//
// If the bucket already has a website configured on it this will overwrite
// that configuration
//
// Usage:
//
// go run s3_set_bucket_website.go BUCKET_NAME INDEX_PAGE ERROR_PAGE
func main() {
if len(os.Args) != 4 {
exitErrorf("bucket name and index suffix page required\nUsage: %s bucket_name index_page [error_page]",
filepath.Base(os.Args[0]))
}
bucket := fromArgs(os.Args, 1)
indexSuffix := fromArgs(os.Args, 2)
errorPage := fromArgs(os.Args, 3)
// Initialize a session in us-west-2 that the SDK will use to load
// credentials from the shared credentials file ~/.aws/credentials.
sess, err := session.NewSession(&aws.Config{
Region: aws.String("us-west-2")},
)
// Create S3 service client
svc := s3.New(sess)
// Create SetBucketWebsite parameters based on CLI input
params := s3.PutBucketWebsiteInput{
Bucket: aws.String(bucket),
WebsiteConfiguration: &s3.WebsiteConfiguration{
IndexDocument: &s3.IndexDocument{
Suffix: aws.String(indexSuffix),
},
},
}
// Add the error page if set on CLI
if len(errorPage) > 0 {
params.WebsiteConfiguration.ErrorDocument = &s3.ErrorDocument{
Key: aws.String(errorPage),
}
}
// Set the website configuration on the bucket. Replacing any existing
// configuration.
_, err = svc.PutBucketWebsite(¶ms)
if err != nil {
exitErrorf("Unable to set bucket %q website configuration, %v",
bucket, err)
}
fmt.Printf("Successfully set bucket %q website configuration\n", bucket)
}
func exitErrorf(msg string, args ...interface{}) {
fmt.Fprintf(os.Stderr, msg+"\n", args...)
os.Exit(1)
}
func fromArgs(args []string, idx int) string {
if len(args) > idx {
return args[idx]
}
return ""
}