mirror of
https://github.com/thegeeklab/wp-s3-action.git
synced 2024-11-22 01:00:40 +00:00
switch to aws-sdk-go over goamz/s3
This commit is contained in:
parent
36a0e84f33
commit
55060d67eb
83
main.go
83
main.go
@ -10,13 +10,16 @@ import (
|
|||||||
|
|
||||||
"github.com/drone/drone-go/drone"
|
"github.com/drone/drone-go/drone"
|
||||||
"github.com/drone/drone-go/plugin"
|
"github.com/drone/drone-go/plugin"
|
||||||
"launchpad.net/goamz/aws"
|
"github.com/aws/aws-sdk-go/aws"
|
||||||
"launchpad.net/goamz/s3"
|
"github.com/aws/aws-sdk-go/aws/credentials"
|
||||||
|
"github.com/aws/aws-sdk-go/aws/session"
|
||||||
|
"github.com/aws/aws-sdk-go/service/s3"
|
||||||
|
"github.com/aws/aws-sdk-go/service/s3/s3manager"
|
||||||
)
|
)
|
||||||
|
|
||||||
type AWS struct {
|
type AWS struct {
|
||||||
client *s3.S3
|
client *s3.S3
|
||||||
bucket *s3.Bucket
|
uploader *s3manager.Uploader
|
||||||
remote []string
|
remote []string
|
||||||
local []string
|
local []string
|
||||||
vargs PluginArgs
|
vargs PluginArgs
|
||||||
@ -90,18 +93,20 @@ type PluginArgs struct {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func NewClient(vargs PluginArgs) AWS {
|
func NewClient(vargs PluginArgs) AWS {
|
||||||
auth := aws.Auth{AccessKey: vargs.Key, SecretKey: vargs.Secret}
|
sess := session.New(&aws.Config{
|
||||||
region := aws.Regions[vargs.Region]
|
Credentials: credentials.NewStaticCredentials(vargs.Key, vargs.Secret, ""),
|
||||||
client := s3.New(auth, region)
|
Region: aws.String(vargs.Region),
|
||||||
bucket := client.Bucket(vargs.Bucket)
|
})
|
||||||
|
client := s3.New(sess)
|
||||||
|
uploader := s3manager.NewUploader(sess)
|
||||||
remote := make([]string, 1, 1)
|
remote := make([]string, 1, 1)
|
||||||
local := make([]string, 1, 1)
|
local := make([]string, 1, 1)
|
||||||
|
|
||||||
aws := AWS{client, bucket, remote, local, vargs}
|
a := AWS{client, uploader, remote, local, vargs}
|
||||||
return aws
|
return a
|
||||||
}
|
}
|
||||||
|
|
||||||
func (aws *AWS) visit(path string, info os.FileInfo, err error) error {
|
func (a *AWS) visit(path string, info os.FileInfo, err error) error {
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
@ -114,12 +119,12 @@ func (aws *AWS) visit(path string, info os.FileInfo, err error) error {
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
localPath := strings.TrimPrefix(path, aws.vargs.Source)
|
localPath := strings.TrimPrefix(path, a.vargs.Source)
|
||||||
if strings.HasPrefix(localPath, "/") {
|
if strings.HasPrefix(localPath, "/") {
|
||||||
localPath = localPath[1:]
|
localPath = localPath[1:]
|
||||||
}
|
}
|
||||||
|
|
||||||
aws.local = append(aws.local, localPath)
|
a.local = append(a.local, localPath)
|
||||||
file, err := os.Open(path)
|
file, err := os.Open(path)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
@ -127,29 +132,29 @@ func (aws *AWS) visit(path string, info os.FileInfo, err error) error {
|
|||||||
|
|
||||||
defer file.Close()
|
defer file.Close()
|
||||||
|
|
||||||
var access s3.ACL
|
access := ""
|
||||||
if aws.vargs.Access.IsString() {
|
if a.vargs.Access.IsString() {
|
||||||
access = s3.ACL(aws.vargs.Access.String())
|
access = a.vargs.Access.String()
|
||||||
} else if !aws.vargs.Access.IsEmpty() {
|
} else if !a.vargs.Access.IsEmpty() {
|
||||||
accessMap := aws.vargs.Access.Map()
|
accessMap := a.vargs.Access.Map()
|
||||||
for pattern := range accessMap {
|
for pattern := range accessMap {
|
||||||
if match, _ := filepath.Match(pattern, localPath); match == true {
|
if match, _ := filepath.Match(pattern, localPath); match == true {
|
||||||
access = s3.ACL(accessMap[pattern])
|
access = accessMap[pattern]
|
||||||
break
|
break
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if access == "" {
|
if access == "" {
|
||||||
access = s3.ACL("private")
|
access = "private"
|
||||||
}
|
}
|
||||||
|
|
||||||
fileExt := filepath.Ext(localPath)
|
fileExt := filepath.Ext(localPath)
|
||||||
var contentType string
|
var contentType string
|
||||||
if aws.vargs.ContentType.IsString() {
|
if a.vargs.ContentType.IsString() {
|
||||||
contentType = aws.vargs.ContentType.String()
|
contentType = a.vargs.ContentType.String()
|
||||||
} else if !aws.vargs.ContentType.IsEmpty() {
|
} else if !a.vargs.ContentType.IsEmpty() {
|
||||||
contentMap := aws.vargs.ContentType.Map()
|
contentMap := a.vargs.ContentType.Map()
|
||||||
for patternExt := range contentMap {
|
for patternExt := range contentMap {
|
||||||
if patternExt == fileExt {
|
if patternExt == fileExt {
|
||||||
contentType = contentMap[patternExt]
|
contentType = contentMap[patternExt]
|
||||||
@ -162,8 +167,14 @@ func (aws *AWS) visit(path string, info os.FileInfo, err error) error {
|
|||||||
contentType = mime.TypeByExtension(fileExt)
|
contentType = mime.TypeByExtension(fileExt)
|
||||||
}
|
}
|
||||||
|
|
||||||
fmt.Printf("Uploading %s with Content-Type %s and permissions %s\n", localPath, contentType, access)
|
fmt.Printf("Uploading \"%s\" with Content-Type \"%s\" and permissions \"%s\"\n", localPath, contentType, access)
|
||||||
err = aws.bucket.PutReader(filepath.Join(aws.vargs.Target, localPath), file, info.Size(), contentType, access)
|
_, err = a.uploader.Upload(&s3manager.UploadInput{
|
||||||
|
Bucket: aws.String(a.vargs.Bucket),
|
||||||
|
Key: aws.String(filepath.Join(a.vargs.Target, localPath)),
|
||||||
|
Body: file,
|
||||||
|
ContentType: aws.String(contentType),
|
||||||
|
ACL: aws.String(access),
|
||||||
|
})
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
@ -171,14 +182,17 @@ func (aws *AWS) visit(path string, info os.FileInfo, err error) error {
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (aws *AWS) List(path string) (*s3.ListResp, error) {
|
func (a *AWS) List(path string) (*s3.ListObjectsOutput, error) {
|
||||||
return aws.bucket.List(path, "", "", 10000)
|
return a.client.ListObjects(&s3.ListObjectsInput{
|
||||||
|
Bucket: aws.String(a.vargs.Bucket),
|
||||||
|
Prefix: aws.String(path),
|
||||||
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
func (aws *AWS) Cleanup() error {
|
func (a *AWS) Cleanup() error {
|
||||||
for _, remote := range aws.remote {
|
for _, remote := range a.remote {
|
||||||
found := false
|
found := false
|
||||||
for _, local := range aws.local {
|
for _, local := range a.local {
|
||||||
if local == remote {
|
if local == remote {
|
||||||
found = true
|
found = true
|
||||||
break
|
break
|
||||||
@ -186,8 +200,11 @@ func (aws *AWS) Cleanup() error {
|
|||||||
}
|
}
|
||||||
|
|
||||||
if !found {
|
if !found {
|
||||||
fmt.Println("Removing remote file ", remote)
|
fmt.Printf("Removing remote file \"%s\"\n", remote)
|
||||||
err := aws.bucket.Del(remote)
|
_, err := a.client.DeleteObject(&s3.DeleteObjectInput{
|
||||||
|
Bucket: aws.String(a.vargs.Bucket),
|
||||||
|
Key: aws.String(remote),
|
||||||
|
})
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
@ -234,7 +251,7 @@ func main() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
for _, item := range resp.Contents {
|
for _, item := range resp.Contents {
|
||||||
client.remote = append(client.remote, item.Key)
|
client.remote = append(client.remote, *item.Key)
|
||||||
}
|
}
|
||||||
|
|
||||||
err = filepath.Walk(vargs.Source, client.visit)
|
err = filepath.Walk(vargs.Source, client.visit)
|
||||||
|
Loading…
Reference in New Issue
Block a user