2014-10-24 23:37:25 +00:00
|
|
|
package s3
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
2014-10-29 01:15:40 +00:00
|
|
|
"fmt"
|
2014-10-24 23:37:25 +00:00
|
|
|
"io"
|
|
|
|
"net/http"
|
|
|
|
"strconv"
|
|
|
|
|
|
|
|
"github.com/crowdmob/goamz/aws"
|
|
|
|
"github.com/crowdmob/goamz/s3"
|
|
|
|
"github.com/docker/docker-registry/storagedriver"
|
2014-10-29 01:15:40 +00:00
|
|
|
"github.com/docker/docker-registry/storagedriver/factory"
|
2014-10-24 23:37:25 +00:00
|
|
|
)
|
|
|
|
|
2014-11-17 23:44:07 +00:00
|
|
|
const driverName = "s3"
|
2014-10-29 01:15:40 +00:00
|
|
|
|
2014-10-29 19:14:19 +00:00
|
|
|
// minChunkSize defines the minimum multipart upload chunk size
|
|
|
|
// S3 API requires multipart upload chunks to be at least 5MB
|
2014-10-24 23:37:25 +00:00
|
|
|
const minChunkSize = uint64(5 * 1024 * 1024)
|
|
|
|
|
2014-10-29 19:14:19 +00:00
|
|
|
// listPartsMax is the largest amount of parts you can request from S3
|
2014-10-24 23:37:25 +00:00
|
|
|
const listPartsMax = 1000
|
|
|
|
|
2014-10-29 01:15:40 +00:00
|
|
|
func init() {
|
2014-11-17 23:44:07 +00:00
|
|
|
factory.Register(driverName, &s3DriverFactory{})
|
2014-10-29 01:15:40 +00:00
|
|
|
}
|
|
|
|
|
2014-10-29 19:14:19 +00:00
|
|
|
// s3DriverFactory implements the factory.StorageDriverFactory interface
|
2014-10-29 01:15:40 +00:00
|
|
|
type s3DriverFactory struct{}
|
|
|
|
|
|
|
|
func (factory *s3DriverFactory) Create(parameters map[string]string) (storagedriver.StorageDriver, error) {
|
|
|
|
return FromParameters(parameters)
|
|
|
|
}
|
|
|
|
|
2014-11-17 23:44:07 +00:00
|
|
|
// Driver is a storagedriver.StorageDriver implementation backed by Amazon S3
|
2014-10-29 01:15:40 +00:00
|
|
|
// Objects are stored at absolute keys in the provided bucket
|
2014-11-17 23:44:07 +00:00
|
|
|
type Driver struct {
|
2014-10-24 23:37:25 +00:00
|
|
|
S3 *s3.S3
|
|
|
|
Bucket *s3.Bucket
|
|
|
|
Encrypt bool
|
|
|
|
}
|
|
|
|
|
2014-11-17 23:44:07 +00:00
|
|
|
// FromParameters constructs a new Driver with a given parameters map
|
2014-10-29 01:15:40 +00:00
|
|
|
// Required parameters:
|
|
|
|
// - accesskey
|
|
|
|
// - secretkey
|
|
|
|
// - region
|
|
|
|
// - bucket
|
|
|
|
// - encrypt
|
2014-11-17 23:44:07 +00:00
|
|
|
func FromParameters(parameters map[string]string) (*Driver, error) {
|
2014-10-29 01:15:40 +00:00
|
|
|
accessKey, ok := parameters["accesskey"]
|
|
|
|
if !ok || accessKey == "" {
|
|
|
|
return nil, fmt.Errorf("No accesskey parameter provided")
|
|
|
|
}
|
|
|
|
|
|
|
|
secretKey, ok := parameters["secretkey"]
|
|
|
|
if !ok || secretKey == "" {
|
|
|
|
return nil, fmt.Errorf("No secretkey parameter provided")
|
|
|
|
}
|
|
|
|
|
|
|
|
regionName, ok := parameters["region"]
|
|
|
|
if !ok || regionName == "" {
|
|
|
|
return nil, fmt.Errorf("No region parameter provided")
|
|
|
|
}
|
|
|
|
region := aws.GetRegion(regionName)
|
|
|
|
if region.Name == "" {
|
2014-11-13 01:19:19 +00:00
|
|
|
return nil, fmt.Errorf("Invalid region provided: %v", region)
|
2014-10-29 01:15:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bucket, ok := parameters["bucket"]
|
|
|
|
if !ok || bucket == "" {
|
|
|
|
return nil, fmt.Errorf("No bucket parameter provided")
|
|
|
|
}
|
|
|
|
|
|
|
|
encrypt, ok := parameters["encrypt"]
|
|
|
|
if !ok {
|
|
|
|
return nil, fmt.Errorf("No encrypt parameter provided")
|
|
|
|
}
|
|
|
|
|
|
|
|
encryptBool, err := strconv.ParseBool(encrypt)
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("Unable to parse the encrypt parameter: %v", err)
|
|
|
|
}
|
|
|
|
return New(accessKey, secretKey, region, encryptBool, bucket)
|
|
|
|
}
|
|
|
|
|
2014-11-17 23:44:07 +00:00
|
|
|
// New constructs a new Driver with the given AWS credentials, region, encryption flag, and
|
2014-10-29 19:14:19 +00:00
|
|
|
// bucketName
|
2014-11-17 23:44:07 +00:00
|
|
|
func New(accessKey string, secretKey string, region aws.Region, encrypt bool, bucketName string) (*Driver, error) {
|
2014-10-24 23:37:25 +00:00
|
|
|
auth := aws.Auth{AccessKey: accessKey, SecretKey: secretKey}
|
|
|
|
s3obj := s3.New(auth, region)
|
|
|
|
bucket := s3obj.Bucket(bucketName)
|
|
|
|
|
2014-10-26 17:00:53 +00:00
|
|
|
if err := bucket.PutBucket(getPermissions()); err != nil {
|
2014-10-24 23:37:25 +00:00
|
|
|
s3Err, ok := err.(*s3.Error)
|
|
|
|
if !(ok && s3Err.Code == "BucketAlreadyOwnedByYou") {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-11-17 23:44:07 +00:00
|
|
|
return &Driver{s3obj, bucket, encrypt}, nil
|
2014-10-24 23:37:25 +00:00
|
|
|
}
|
|
|
|
|
2014-10-29 19:14:19 +00:00
|
|
|
// Implement the storagedriver.StorageDriver interface
|
|
|
|
|
2014-11-17 23:44:07 +00:00
|
|
|
// GetContent retrieves the content stored at "path" as a []byte.
|
|
|
|
func (d *Driver) GetContent(path string) ([]byte, error) {
|
2014-10-24 23:37:25 +00:00
|
|
|
return d.Bucket.Get(path)
|
|
|
|
}
|
|
|
|
|
2014-11-17 23:44:07 +00:00
|
|
|
// PutContent stores the []byte content at a location designated by "path".
|
|
|
|
func (d *Driver) PutContent(path string, contents []byte) error {
|
2014-10-26 17:00:53 +00:00
|
|
|
return d.Bucket.Put(path, contents, d.getContentType(), getPermissions(), d.getOptions())
|
2014-10-24 23:37:25 +00:00
|
|
|
}
|
|
|
|
|
2014-11-17 23:44:07 +00:00
|
|
|
// ReadStream retrieves an io.ReadCloser for the content stored at "path" with a
|
|
|
|
// given byte offset.
|
|
|
|
func (d *Driver) ReadStream(path string, offset uint64) (io.ReadCloser, error) {
|
2014-10-24 23:37:25 +00:00
|
|
|
headers := make(http.Header)
|
|
|
|
headers.Add("Range", "bytes="+strconv.FormatUint(offset, 10)+"-")
|
|
|
|
|
|
|
|
resp, err := d.Bucket.GetResponseWithHeaders(path, headers)
|
|
|
|
if resp != nil {
|
|
|
|
return resp.Body, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2014-11-17 23:44:07 +00:00
|
|
|
// WriteStream stores the contents of the provided io.ReadCloser at a location
|
|
|
|
// designated by the given path.
|
|
|
|
func (d *Driver) WriteStream(path string, offset, size uint64, reader io.ReadCloser) error {
|
2014-10-24 23:37:25 +00:00
|
|
|
defer reader.Close()
|
|
|
|
|
|
|
|
chunkSize := minChunkSize
|
|
|
|
for size/chunkSize >= listPartsMax {
|
|
|
|
chunkSize *= 2
|
|
|
|
}
|
|
|
|
|
|
|
|
partNumber := 1
|
|
|
|
totalRead := uint64(0)
|
|
|
|
multi, parts, err := d.getAllParts(path)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if (offset) > uint64(len(parts))*chunkSize || (offset < size && offset%chunkSize != 0) {
|
2014-11-13 01:19:19 +00:00
|
|
|
return storagedriver.InvalidOffsetError{Path: path, Offset: offset}
|
2014-10-24 23:37:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if len(parts) > 0 {
|
|
|
|
partNumber = int(offset/chunkSize) + 1
|
|
|
|
totalRead = offset
|
|
|
|
parts = parts[0 : partNumber-1]
|
|
|
|
}
|
|
|
|
|
|
|
|
buf := make([]byte, chunkSize)
|
|
|
|
for {
|
|
|
|
bytesRead, err := io.ReadFull(reader, buf)
|
|
|
|
totalRead += uint64(bytesRead)
|
|
|
|
|
|
|
|
if err != nil && err != io.ErrUnexpectedEOF && err != io.EOF {
|
|
|
|
return err
|
|
|
|
} else if (uint64(bytesRead) < chunkSize) && totalRead != size {
|
|
|
|
break
|
|
|
|
} else {
|
|
|
|
part, err := multi.PutPart(int(partNumber), bytes.NewReader(buf[0:bytesRead]))
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
parts = append(parts, part)
|
|
|
|
if totalRead == size {
|
|
|
|
multi.Complete(parts)
|
|
|
|
break
|
|
|
|
}
|
|
|
|
|
|
|
|
partNumber++
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2014-11-17 23:44:07 +00:00
|
|
|
// CurrentSize retrieves the curernt size in bytes of the object at the given
|
|
|
|
// path.
|
|
|
|
func (d *Driver) CurrentSize(path string) (uint64, error) {
|
2014-10-24 23:37:25 +00:00
|
|
|
_, parts, err := d.getAllParts(path)
|
|
|
|
if err != nil {
|
|
|
|
return 0, err
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(parts) == 0 {
|
|
|
|
return 0, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
return (((uint64(len(parts)) - 1) * uint64(parts[0].Size)) + uint64(parts[len(parts)-1].Size)), nil
|
|
|
|
}
|
|
|
|
|
2014-11-17 23:44:07 +00:00
|
|
|
// List returns a list of the objects that are direct descendants of the given
|
|
|
|
// path.
|
|
|
|
func (d *Driver) List(path string) ([]string, error) {
|
2014-11-07 20:58:48 +00:00
|
|
|
if path[len(path)-1] != '/' {
|
|
|
|
path = path + "/"
|
2014-11-04 00:20:38 +00:00
|
|
|
}
|
2014-11-07 20:58:48 +00:00
|
|
|
listResponse, err := d.Bucket.List(path, "/", "", listPartsMax)
|
2014-10-24 23:37:25 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
files := []string{}
|
|
|
|
directories := []string{}
|
|
|
|
|
2014-11-04 00:20:38 +00:00
|
|
|
for {
|
2014-10-24 23:37:25 +00:00
|
|
|
for _, key := range listResponse.Contents {
|
|
|
|
files = append(files, key.Key)
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, commonPrefix := range listResponse.CommonPrefixes {
|
|
|
|
directories = append(directories, commonPrefix[0:len(commonPrefix)-1])
|
|
|
|
}
|
|
|
|
|
2014-11-04 00:20:38 +00:00
|
|
|
if listResponse.IsTruncated {
|
2014-11-07 20:58:48 +00:00
|
|
|
listResponse, err = d.Bucket.List(path, "/", listResponse.NextMarker, listPartsMax)
|
2014-11-04 00:20:38 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2014-10-24 23:37:25 +00:00
|
|
|
} else {
|
2014-11-04 00:20:38 +00:00
|
|
|
break
|
2014-10-24 23:37:25 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return append(files, directories...), nil
|
|
|
|
}
|
|
|
|
|
2014-11-17 23:44:07 +00:00
|
|
|
// Move moves an object stored at sourcePath to destPath, removing the original
|
|
|
|
// object.
|
|
|
|
func (d *Driver) Move(sourcePath string, destPath string) error {
|
2014-10-24 23:37:25 +00:00
|
|
|
/* This is terrible, but aws doesn't have an actual move. */
|
2014-11-13 01:19:19 +00:00
|
|
|
_, err := d.Bucket.PutCopy(destPath, getPermissions(),
|
|
|
|
s3.CopyOptions{Options: d.getOptions(), MetadataDirective: "", ContentType: d.getContentType()},
|
|
|
|
d.Bucket.Name+"/"+sourcePath)
|
2014-10-24 23:37:25 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return d.Delete(sourcePath)
|
|
|
|
}
|
|
|
|
|
2014-11-17 23:44:07 +00:00
|
|
|
// Delete recursively deletes all objects stored at "path" and its subpaths.
|
|
|
|
func (d *Driver) Delete(path string) error {
|
2014-10-24 23:37:25 +00:00
|
|
|
listResponse, err := d.Bucket.List(path, "", "", listPartsMax)
|
|
|
|
if err != nil || len(listResponse.Contents) == 0 {
|
2014-11-13 01:19:19 +00:00
|
|
|
return storagedriver.PathNotFoundError{Path: path}
|
2014-10-24 23:37:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
s3Objects := make([]s3.Object, listPartsMax)
|
|
|
|
|
|
|
|
for len(listResponse.Contents) > 0 {
|
|
|
|
for index, key := range listResponse.Contents {
|
|
|
|
s3Objects[index].Key = key.Key
|
|
|
|
}
|
|
|
|
|
2014-11-13 01:19:19 +00:00
|
|
|
err := d.Bucket.DelMulti(s3.Delete{Quiet: false, Objects: s3Objects[0:len(listResponse.Contents)]})
|
2014-10-24 23:37:25 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
listResponse, err = d.Bucket.List(path, "", "", listPartsMax)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2014-11-17 23:44:07 +00:00
|
|
|
func (d *Driver) getHighestIDMulti(path string) (multi *s3.Multi, err error) {
|
2014-10-24 23:37:25 +00:00
|
|
|
multis, _, err := d.Bucket.ListMulti(path, "")
|
|
|
|
if err != nil && !hasCode(err, "NoSuchUpload") {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2014-11-17 23:44:07 +00:00
|
|
|
uploadID := ""
|
2014-10-24 23:37:25 +00:00
|
|
|
|
|
|
|
if len(multis) > 0 {
|
|
|
|
for _, m := range multis {
|
2014-11-17 23:44:07 +00:00
|
|
|
if m.Key == path && m.UploadId >= uploadID {
|
|
|
|
uploadID = m.UploadId
|
2014-10-24 23:37:25 +00:00
|
|
|
multi = m
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return multi, nil
|
|
|
|
}
|
2014-11-17 23:44:07 +00:00
|
|
|
multi, err = d.Bucket.InitMulti(path, d.getContentType(), getPermissions(), d.getOptions())
|
|
|
|
return multi, err
|
2014-10-24 23:37:25 +00:00
|
|
|
}
|
|
|
|
|
2014-11-17 23:44:07 +00:00
|
|
|
func (d *Driver) getAllParts(path string) (*s3.Multi, []s3.Part, error) {
|
|
|
|
multi, err := d.getHighestIDMulti(path)
|
2014-10-24 23:37:25 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
parts, err := multi.ListParts()
|
|
|
|
return multi, parts, err
|
|
|
|
}
|
|
|
|
|
|
|
|
func hasCode(err error, code string) bool {
|
|
|
|
s3err, ok := err.(*aws.Error)
|
|
|
|
return ok && s3err.Code == code
|
|
|
|
}
|
|
|
|
|
2014-11-17 23:44:07 +00:00
|
|
|
func (d *Driver) getOptions() s3.Options {
|
2014-10-24 23:37:25 +00:00
|
|
|
return s3.Options{SSE: d.Encrypt}
|
|
|
|
}
|
|
|
|
|
2014-10-26 17:00:53 +00:00
|
|
|
func getPermissions() s3.ACL {
|
2014-10-24 23:37:25 +00:00
|
|
|
return s3.Private
|
|
|
|
}
|
|
|
|
|
2014-11-17 23:44:07 +00:00
|
|
|
func (d *Driver) getContentType() string {
|
2014-10-24 23:37:25 +00:00
|
|
|
return "application/octet-stream"
|
|
|
|
}
|