Last active
August 29, 2015 14:06
-
-
Save linyows/1fb3c54455f3491ed113 to your computer and use it in GitHub Desktop.
S3のリソースのhttpヘッダーにcache系追加
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"bufio" | |
"fmt" | |
"os" | |
"runtime" | |
"sync" | |
"time" | |
"github.com/linyows/goamz/aws" | |
"github.com/linyows/goamz/s3" | |
) | |
func main() { | |
changeHeaders() | |
} | |
const ( | |
accesskey = "" | |
secretkey = "" | |
bucketName = "" | |
list = "" | |
) | |
func changeHeaders() { | |
start := time.Now() | |
cpus := runtime.NumCPU() | |
semaphore := make(chan int, cpus) | |
runtime.GOMAXPROCS(cpus) | |
var wg sync.WaitGroup | |
var mu sync.Mutex | |
lines, _ := readLines(list) | |
//lines := []string{"1", "2", "3", "4", "5", "6", "7", "8", "9"} | |
total := len(lines) | |
index := 0 | |
for _, key := range lines { | |
index++ | |
wg.Add(1) | |
go parallel(index, total, key, semaphore, &wg, &mu) | |
} | |
wg.Wait() | |
defer fmt.Println("Total:", total) | |
defer fmt.Println("End:", time.Now()) | |
defer fmt.Println("Start:", start) | |
} | |
func parallel(index int, total int, key string, s chan int, wg *sync.WaitGroup, mu *sync.Mutex) { | |
mu.Lock() | |
defer mu.Unlock() | |
defer wg.Done() | |
s <- 1 | |
//showHeader(bucketName, key) | |
replace(bucketName, key) | |
fmt.Println(index, total, key) | |
<-s | |
} | |
func readLines(path string) ([]string, error) { | |
f, err := os.Open(path) | |
if err != nil { | |
return nil, err | |
} | |
defer f.Close() | |
var lines []string | |
scanner := bufio.NewScanner(f) | |
for scanner.Scan() { | |
lines = append(lines, scanner.Text()) | |
} | |
return lines, scanner.Err() | |
} | |
func httpdate() string { | |
now := time.Now() | |
httpdate := "Mon, 02 Jan 2006 15:04:05 GMT" | |
return now.AddDate(10, 0, 0).UTC().Format(httpdate) | |
} | |
func replace(name string, key string) *s3.CopyObjectResult { | |
ops := s3.Options{ | |
CacheControl: "max-age=315576000", | |
Expires: httpdate(), | |
} | |
copyOps := s3.CopyOptions{ | |
MetadataDirective: "REPLACE", | |
ContentType: contentType(name, key), | |
Options: ops, | |
} | |
re, err := bucket(name).PutCopy(key, s3.PublicRead, copyOps, name+"/"+key) | |
if err != nil { | |
fmt.Println(err) | |
} | |
return re | |
} | |
func showHeader(name string, key string) { | |
re, err := bucket(name).GetResponse(key) | |
fmt.Println(name + "/" + key) | |
if err != nil { | |
fmt.Println(err) | |
return | |
} | |
for k, v := range re.Header { | |
fmt.Println(" "+k+":", v[0]) | |
} | |
} | |
func contentType(name string, key string) string { | |
re, _ := bucket(name).Head(key, nil) | |
return re.Header["Content-Type"][0] | |
} | |
func bucket(name string) *s3.Bucket { | |
auth := aws.Auth{ | |
AccessKey: accesskey, | |
SecretKey: secretkey, | |
} | |
c := s3.New(auth, aws.APNortheast) | |
return c.Bucket(name) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment