|
package main |
|
|
|
import ( |
|
"fmt" |
|
"github.com/aws/aws-sdk-go/aws" |
|
"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" |
|
"gopkg.in/alecthomas/kingpin.v2" |
|
"log" |
|
"os" |
|
) |
|
|
|
func S3MultipartUpload(path, contentType, bucket, cacheControl string, partSize int, file *os.File) (err error) { |
|
var uploader *s3manager.Uploader |
|
if partSize != 0 { |
|
uploader = s3manager.NewUploaderWithClient(s3Cli, func(u *s3manager.Uploader) { |
|
u.PartSize = int64(partSize) |
|
}) |
|
} else { |
|
uploader = s3manager.NewUploaderWithClient(s3Cli) |
|
} |
|
|
|
uploadInput := &s3manager.UploadInput{ |
|
Bucket: aws.String(bucket), |
|
Key: aws.String(path), |
|
Body: file, |
|
} |
|
|
|
if contentType != "" { |
|
uploadInput.ContentType = aws.String(contentType) |
|
} |
|
|
|
if cacheControl != "" { |
|
uploadInput.CacheControl = aws.String(cacheControl) |
|
} |
|
|
|
_, err = uploader.Upload(uploadInput) |
|
|
|
return err |
|
} |
|
|
|
var ( |
|
file = kingpin.Flag("file", "upload file").Short('f').Required().File() |
|
bucket = kingpin.Flag("bucket", "bucket").Short('b').Required().String() |
|
region = kingpin.Flag("region", "region").Short('r').Required().String() |
|
path = kingpin.Flag("path", "path").String() |
|
contentType = kingpin.Flag("content-type", "content-type").String() |
|
cacheControl = kingpin.Flag("cache-control", "cache-control").String() |
|
partSize = kingpin.Flag("part-size", "part size (bytes)").Int() |
|
s3Cli *s3.S3 |
|
) |
|
|
|
func main() { |
|
kingpin.Version("0.1") |
|
kingpin.Parse() |
|
|
|
sess := session.New() |
|
s3Cli = s3.New(sess, aws.NewConfig().WithRegion(*region)) |
|
|
|
err := S3MultipartUpload(*path, *contentType, *bucket, *cacheControl, *partSize, *file) |
|
defer (*file).Close() |
|
|
|
if err != nil { |
|
log.Fatal(err) |
|
} |
|
|
|
fmt.Println(fmt.Sprintf("Uploaded s3://%s/%s", *bucket, *path)) |
|
} |