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"
|
2014-12-19 17:16:51 +00:00
|
|
|
"io/ioutil"
|
2014-10-24 23:37:25 +00:00
|
|
|
"net/http"
|
|
|
|
"strconv"
|
2014-12-19 17:16:51 +00:00
|
|
|
"strings"
|
|
|
|
"time"
|
2014-10-24 23:37:25 +00:00
|
|
|
|
|
|
|
"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-12-19 17:16:51 +00:00
|
|
|
const chunkSize = 5 * 1024 * 1024
|
2014-10-24 23:37:25 +00:00
|
|
|
|
2014-12-19 17:16:51 +00:00
|
|
|
// listMax is the largest amount of objects you can request from S3 in a list call
|
|
|
|
const listMax = 1000
|
2014-10-24 23:37:25 +00:00
|
|
|
|
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{}
|
|
|
|
|
2014-12-19 17:16:51 +00:00
|
|
|
func (factory *s3DriverFactory) Create(parameters map[string]interface{}) (storagedriver.StorageDriver, error) {
|
2014-10-29 01:15:40 +00:00
|
|
|
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-12-19 17:16:51 +00:00
|
|
|
S3 *s3.S3
|
|
|
|
Bucket *s3.Bucket
|
|
|
|
Encrypt bool
|
|
|
|
rootDirectory string
|
2014-10-24 23:37:25 +00:00
|
|
|
}
|
|
|
|
|
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-12-19 17:16:51 +00:00
|
|
|
func FromParameters(parameters map[string]interface{}) (*Driver, error) {
|
2014-10-29 01:15:40 +00:00
|
|
|
accessKey, ok := parameters["accesskey"]
|
2014-12-19 17:16:51 +00:00
|
|
|
if !ok {
|
|
|
|
accessKey = ""
|
2014-10-29 01:15:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
secretKey, ok := parameters["secretkey"]
|
2014-12-19 17:16:51 +00:00
|
|
|
if !ok {
|
|
|
|
secretKey = ""
|
2014-10-29 01:15:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
regionName, ok := parameters["region"]
|
2014-12-19 17:16:51 +00:00
|
|
|
if !ok || fmt.Sprint(regionName) == "" {
|
2014-10-29 01:15:40 +00:00
|
|
|
return nil, fmt.Errorf("No region parameter provided")
|
|
|
|
}
|
2014-12-19 17:16:51 +00:00
|
|
|
region := aws.GetRegion(fmt.Sprint(regionName))
|
2014-10-29 01:15:40 +00:00
|
|
|
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"]
|
2014-12-19 17:16:51 +00:00
|
|
|
if !ok || fmt.Sprint(bucket) == "" {
|
2014-10-29 01:15:40 +00:00
|
|
|
return nil, fmt.Errorf("No bucket parameter provided")
|
|
|
|
}
|
|
|
|
|
|
|
|
encrypt, ok := parameters["encrypt"]
|
|
|
|
if !ok {
|
|
|
|
return nil, fmt.Errorf("No encrypt parameter provided")
|
|
|
|
}
|
|
|
|
|
2014-12-19 17:16:51 +00:00
|
|
|
encryptBool, err := strconv.ParseBool(fmt.Sprint(encrypt))
|
2014-10-29 01:15:40 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("Unable to parse the encrypt parameter: %v", err)
|
|
|
|
}
|
2014-12-19 17:16:51 +00:00
|
|
|
|
|
|
|
rootDirectory, ok := parameters["rootdirectory"]
|
|
|
|
if !ok {
|
|
|
|
return nil, fmt.Errorf("No rootDirectory parameter provided")
|
|
|
|
}
|
|
|
|
|
|
|
|
return New(fmt.Sprint(accessKey), fmt.Sprint(secretKey), fmt.Sprint(bucket), fmt.Sprint(rootDirectory), region, encryptBool)
|
2014-10-29 01:15:40 +00:00
|
|
|
}
|
|
|
|
|
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-12-19 17:16:51 +00:00
|
|
|
func New(accessKey, secretKey, bucketName, rootDirectory string, region aws.Region, encrypt bool) (*Driver, error) {
|
|
|
|
auth, err := aws.GetAuth(accessKey, secretKey, "", time.Time{})
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2014-10-24 23:37:25 +00:00
|
|
|
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-12-19 17:16:51 +00:00
|
|
|
// TODO What if they use this bucket for other things? I can't just clean out the multis
|
|
|
|
// TODO Add timestamp checking
|
|
|
|
multis, _, err := bucket.ListMulti("", "")
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, multi := range multis {
|
|
|
|
err := multi.Abort()
|
|
|
|
//TODO appropriate to do this error checking?
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return &Driver{s3obj, bucket, encrypt, rootDirectory}, 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-12-19 17:16:51 +00:00
|
|
|
if !storagedriver.PathRegexp.MatchString(path) {
|
|
|
|
return nil, storagedriver.InvalidPathError{Path: path}
|
|
|
|
}
|
|
|
|
|
|
|
|
content, err := d.Bucket.Get(d.s3Path(path))
|
2014-11-19 01:41:48 +00:00
|
|
|
if err != nil {
|
2014-12-19 17:16:51 +00:00
|
|
|
return nil, parseError(path, err)
|
2014-11-19 01:41:48 +00:00
|
|
|
}
|
|
|
|
return content, nil
|
2014-10-24 23:37:25 +00:00
|
|
|
}
|
|
|
|
|
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-12-19 17:16:51 +00:00
|
|
|
if !storagedriver.PathRegexp.MatchString(path) {
|
|
|
|
return storagedriver.InvalidPathError{Path: path}
|
|
|
|
}
|
|
|
|
|
|
|
|
return parseError(path, d.Bucket.Put(d.s3Path(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.
|
2014-12-03 03:01:00 +00:00
|
|
|
func (d *Driver) ReadStream(path string, offset int64) (io.ReadCloser, error) {
|
2014-12-19 17:16:51 +00:00
|
|
|
if !storagedriver.PathRegexp.MatchString(path) {
|
|
|
|
return nil, storagedriver.InvalidPathError{Path: path}
|
|
|
|
}
|
|
|
|
|
|
|
|
if offset < 0 {
|
|
|
|
return nil, storagedriver.InvalidOffsetError{Path: path, Offset: offset}
|
|
|
|
}
|
|
|
|
|
2014-10-24 23:37:25 +00:00
|
|
|
headers := make(http.Header)
|
2014-12-03 03:01:00 +00:00
|
|
|
headers.Add("Range", "bytes="+strconv.FormatInt(offset, 10)+"-")
|
2014-10-24 23:37:25 +00:00
|
|
|
|
2014-12-19 17:16:51 +00:00
|
|
|
resp, err := d.Bucket.GetResponseWithHeaders(d.s3Path(path), headers)
|
2014-11-19 01:41:48 +00:00
|
|
|
if err != nil {
|
2014-12-19 17:16:51 +00:00
|
|
|
if s3Err, ok := err.(*s3.Error); ok && s3Err.Code == "InvalidRange" {
|
|
|
|
return ioutil.NopCloser(bytes.NewReader(nil)), nil
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil, parseError(path, err)
|
2014-10-24 23:37:25 +00:00
|
|
|
}
|
2014-11-19 01:41:48 +00:00
|
|
|
return resp.Body, nil
|
2014-10-24 23:37:25 +00:00
|
|
|
}
|
|
|
|
|
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.
|
2014-12-19 17:16:51 +00:00
|
|
|
func (d *Driver) WriteStream(path string, offset int64, reader io.Reader) (totalRead int64, err error) {
|
|
|
|
if !storagedriver.PathRegexp.MatchString(path) {
|
|
|
|
return 0, storagedriver.InvalidPathError{Path: path}
|
|
|
|
}
|
2014-10-24 23:37:25 +00:00
|
|
|
|
2014-12-19 17:16:51 +00:00
|
|
|
if offset < 0 {
|
|
|
|
return 0, storagedriver.InvalidOffsetError{Path: path, Offset: offset}
|
2014-10-24 23:37:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
partNumber := 1
|
2014-12-19 17:16:51 +00:00
|
|
|
parts := []s3.Part{}
|
|
|
|
var part s3.Part
|
|
|
|
|
|
|
|
multi, err := d.Bucket.InitMulti(d.s3Path(path), d.getContentType(), getPermissions(), d.getOptions())
|
2014-10-24 23:37:25 +00:00
|
|
|
if err != nil {
|
2014-12-19 17:16:51 +00:00
|
|
|
return 0, err
|
2014-10-24 23:37:25 +00:00
|
|
|
}
|
|
|
|
|
2014-12-19 17:16:51 +00:00
|
|
|
buf := make([]byte, chunkSize)
|
|
|
|
|
|
|
|
// We never want to leave a dangling multipart upload, our only consistent state is
|
|
|
|
// when there is a whole object at path. This is in order to remain consistent with
|
|
|
|
// the stat call.
|
|
|
|
//
|
|
|
|
// Note that if the machine dies before executing the defer, we will be left with a dangling
|
|
|
|
// multipart upload, which will eventually be cleaned up, but we will lose all of the progress
|
|
|
|
// made prior to the machine crashing.
|
|
|
|
defer func() {
|
|
|
|
if len(parts) > 0 {
|
|
|
|
err = multi.Complete(parts)
|
|
|
|
if err != nil {
|
|
|
|
multi.Abort()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}()
|
2014-10-24 23:37:25 +00:00
|
|
|
|
2014-12-19 17:16:51 +00:00
|
|
|
if offset > 0 {
|
|
|
|
resp, err := d.Bucket.Head(d.s3Path(path), nil)
|
|
|
|
if err != nil {
|
|
|
|
return 0, err
|
|
|
|
}
|
|
|
|
if resp.ContentLength < offset {
|
|
|
|
return 0, storagedriver.InvalidOffsetError{Path: path, Offset: offset}
|
|
|
|
}
|
|
|
|
|
|
|
|
if resp.ContentLength < chunkSize {
|
|
|
|
// If everything written so far is less than the minimum part size of 5MB, we need
|
|
|
|
// to fill out the first part up to that minimum.
|
|
|
|
current, err := d.ReadStream(path, 0)
|
|
|
|
if err != nil {
|
|
|
|
return 0, err
|
|
|
|
}
|
|
|
|
|
|
|
|
bytesRead, err := io.ReadFull(current, buf[0:offset])
|
|
|
|
if err != nil && err != io.ErrUnexpectedEOF && err != io.EOF {
|
|
|
|
return 0, err
|
|
|
|
} else if int64(bytesRead) != offset {
|
|
|
|
//TODO Maybe a different error? I don't even think this case is reachable...
|
|
|
|
return 0, storagedriver.InvalidOffsetError{Path: path, Offset: offset}
|
|
|
|
}
|
|
|
|
|
|
|
|
bytesRead, err = io.ReadFull(reader, buf[offset:])
|
|
|
|
totalRead += int64(bytesRead)
|
|
|
|
|
|
|
|
if err != nil && err != io.ErrUnexpectedEOF && err != io.EOF {
|
|
|
|
return totalRead, err
|
|
|
|
}
|
|
|
|
|
|
|
|
part, err = multi.PutPart(int(partNumber), bytes.NewReader(buf[0:int64(bytesRead)+offset]))
|
|
|
|
if err != nil {
|
|
|
|
return totalRead, err
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
fmt.Println("About to PutPartCopy")
|
|
|
|
// If the file that we already have is larger than 5MB, then we make it the first part
|
|
|
|
// of the new multipart upload.
|
|
|
|
_, part, err = multi.PutPartCopy(partNumber, s3.CopyOptions{}, d.Bucket.Name+"/"+d.s3Path(path))
|
|
|
|
if err != nil {
|
|
|
|
return 0, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
parts = append(parts, part)
|
|
|
|
partNumber++
|
|
|
|
|
|
|
|
if totalRead+offset < chunkSize {
|
|
|
|
return totalRead, nil
|
|
|
|
}
|
2014-10-24 23:37:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
for {
|
|
|
|
bytesRead, err := io.ReadFull(reader, buf)
|
2014-12-03 03:01:00 +00:00
|
|
|
totalRead += int64(bytesRead)
|
2014-10-24 23:37:25 +00:00
|
|
|
|
|
|
|
if err != nil && err != io.ErrUnexpectedEOF && err != io.EOF {
|
2014-12-19 17:16:51 +00:00
|
|
|
return totalRead, err
|
|
|
|
}
|
2014-10-24 23:37:25 +00:00
|
|
|
|
2014-12-19 17:16:51 +00:00
|
|
|
part, err := multi.PutPart(int(partNumber), bytes.NewReader(buf[0:bytesRead]))
|
|
|
|
if err != nil {
|
|
|
|
return totalRead, err
|
|
|
|
}
|
2014-10-24 23:37:25 +00:00
|
|
|
|
2014-12-19 17:16:51 +00:00
|
|
|
parts = append(parts, part)
|
|
|
|
partNumber++
|
|
|
|
|
|
|
|
if int64(bytesRead) < chunkSize {
|
|
|
|
break
|
2014-10-24 23:37:25 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-12-19 17:16:51 +00:00
|
|
|
return totalRead, nil
|
2014-10-24 23:37:25 +00:00
|
|
|
}
|
|
|
|
|
2014-12-19 17:16:51 +00:00
|
|
|
// Stat retrieves the FileInfo for the given path, including the current size
|
|
|
|
// in bytes and the creation time.
|
|
|
|
func (d *Driver) Stat(path string) (storagedriver.FileInfo, error) {
|
|
|
|
if !storagedriver.PathRegexp.MatchString(path) {
|
|
|
|
return nil, storagedriver.InvalidPathError{Path: path}
|
|
|
|
}
|
|
|
|
|
|
|
|
listResponse, err := d.Bucket.List(d.s3Path(path), "", "", 1)
|
2014-10-24 23:37:25 +00:00
|
|
|
if err != nil {
|
2014-12-19 17:16:51 +00:00
|
|
|
return nil, err
|
2014-10-24 23:37:25 +00:00
|
|
|
}
|
|
|
|
|
2014-12-19 17:16:51 +00:00
|
|
|
fi := storagedriver.FileInfoFields{
|
|
|
|
Path: path,
|
2014-10-24 23:37:25 +00:00
|
|
|
}
|
|
|
|
|
2014-12-19 17:16:51 +00:00
|
|
|
if len(listResponse.Contents) == 1 {
|
|
|
|
if listResponse.Contents[0].Key != d.s3Path(path) {
|
|
|
|
fi.IsDir = true
|
|
|
|
} else {
|
|
|
|
fi.IsDir = false
|
|
|
|
fi.Size = listResponse.Contents[0].Size
|
|
|
|
|
|
|
|
timestamp, err := time.Parse(time.RFC3339Nano, listResponse.Contents[0].LastModified)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
fi.ModTime = timestamp
|
|
|
|
}
|
|
|
|
} else if len(listResponse.CommonPrefixes) == 1 {
|
|
|
|
fi.IsDir = true
|
|
|
|
} else {
|
|
|
|
return nil, storagedriver.PathNotFoundError{Path: path}
|
|
|
|
}
|
|
|
|
|
|
|
|
return storagedriver.FileInfoInternal{FileInfoFields: fi}, nil
|
2014-10-24 23:37:25 +00:00
|
|
|
}
|
|
|
|
|
2014-12-19 17:16:51 +00:00
|
|
|
// List returns a list of the objects that are direct descendants of the given path.
|
2014-11-17 23:44:07 +00:00
|
|
|
func (d *Driver) List(path string) ([]string, error) {
|
2014-12-19 17:16:51 +00:00
|
|
|
if !storagedriver.PathRegexp.MatchString(path) && path != "/" {
|
|
|
|
return nil, storagedriver.InvalidPathError{Path: path}
|
|
|
|
}
|
|
|
|
|
|
|
|
if path != "/" && path[len(path)-1] != '/' {
|
2014-11-07 20:58:48 +00:00
|
|
|
path = path + "/"
|
2014-11-04 00:20:38 +00:00
|
|
|
}
|
2014-12-19 17:16:51 +00:00
|
|
|
listResponse, err := d.Bucket.List(d.s3Path(path), "/", "", listMax)
|
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 {
|
2014-12-19 17:16:51 +00:00
|
|
|
files = append(files, strings.Replace(key.Key, d.s3Path(""), "", 1))
|
2014-10-24 23:37:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
for _, commonPrefix := range listResponse.CommonPrefixes {
|
2014-12-19 17:16:51 +00:00
|
|
|
directories = append(directories, strings.Replace(commonPrefix[0:len(commonPrefix)-1], d.s3Path(""), "", 1))
|
2014-10-24 23:37:25 +00:00
|
|
|
}
|
|
|
|
|
2014-11-04 00:20:38 +00:00
|
|
|
if listResponse.IsTruncated {
|
2014-12-19 17:16:51 +00:00
|
|
|
listResponse, err = d.Bucket.List(d.s3Path(path), "/", listResponse.NextMarker, listMax)
|
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-12-19 17:16:51 +00:00
|
|
|
if !storagedriver.PathRegexp.MatchString(sourcePath) {
|
|
|
|
return storagedriver.InvalidPathError{Path: sourcePath}
|
|
|
|
} else if !storagedriver.PathRegexp.MatchString(destPath) {
|
|
|
|
return storagedriver.InvalidPathError{Path: destPath}
|
|
|
|
}
|
|
|
|
|
2014-10-24 23:37:25 +00:00
|
|
|
/* This is terrible, but aws doesn't have an actual move. */
|
2014-12-19 17:16:51 +00:00
|
|
|
_, err := d.Bucket.PutCopy(d.s3Path(destPath), getPermissions(),
|
|
|
|
s3.CopyOptions{Options: d.getOptions(), ContentType: d.getContentType()}, d.Bucket.Name+"/"+d.s3Path(sourcePath))
|
2014-10-24 23:37:25 +00:00
|
|
|
if err != nil {
|
2014-12-19 17:16:51 +00:00
|
|
|
return parseError(sourcePath, err)
|
2014-10-24 23:37:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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-12-19 17:16:51 +00:00
|
|
|
if !storagedriver.PathRegexp.MatchString(path) {
|
|
|
|
return storagedriver.InvalidPathError{Path: path}
|
|
|
|
}
|
|
|
|
|
|
|
|
listResponse, err := d.Bucket.List(d.s3Path(path), "", "", listMax)
|
2014-10-24 23:37:25 +00:00
|
|
|
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
|
|
|
}
|
|
|
|
|
2014-12-19 17:16:51 +00:00
|
|
|
s3Objects := make([]s3.Object, listMax)
|
2014-10-24 23:37:25 +00:00
|
|
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2014-12-19 17:16:51 +00:00
|
|
|
listResponse, err = d.Bucket.List(d.s3Path(path), "", "", listMax)
|
2014-10-24 23:37:25 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2014-12-19 17:16:51 +00:00
|
|
|
func (d *Driver) s3Path(path string) string {
|
|
|
|
return strings.TrimLeft(d.rootDirectory+path, "/")
|
|
|
|
}
|
2014-10-24 23:37:25 +00:00
|
|
|
|
2014-12-19 17:16:51 +00:00
|
|
|
func (d *Driver) fullPath(path string) string {
|
|
|
|
return d.rootDirectory + path
|
2014-10-24 23:37:25 +00:00
|
|
|
}
|
|
|
|
|
2014-12-19 17:16:51 +00:00
|
|
|
func parseError(path string, err error) error {
|
|
|
|
if s3Err, ok := err.(*s3.Error); ok && s3Err.Code == "NoSuchKey" {
|
|
|
|
return storagedriver.PathNotFoundError{Path: path}
|
2014-10-24 23:37:25 +00:00
|
|
|
}
|
|
|
|
|
2014-12-19 17:16:51 +00:00
|
|
|
return err
|
2014-10-24 23:37:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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"
|
|
|
|
}
|