2013-01-08 19:53:35 +01:00
|
|
|
// S3 interface
|
2013-06-27 21:13:07 +02:00
|
|
|
package s3
|
2013-01-08 19:53:35 +01:00
|
|
|
|
2013-01-23 23:43:20 +01:00
|
|
|
// FIXME need to prevent anything but ListDir working for s3://
|
|
|
|
|
2014-12-23 13:09:02 +01:00
|
|
|
/*
|
|
|
|
Progress of port to aws-sdk
|
|
|
|
|
|
|
|
* Don't really need o.meta at all?
|
|
|
|
|
|
|
|
What happens if you CTRL-C a multipart upload
|
|
|
|
* get an incomplete upload
|
|
|
|
* disappears when you delete the bucket
|
|
|
|
*/
|
|
|
|
|
2013-01-08 19:53:35 +01:00
|
|
|
import (
|
|
|
|
"errors"
|
|
|
|
"fmt"
|
|
|
|
"io"
|
|
|
|
"path"
|
|
|
|
"regexp"
|
|
|
|
"strings"
|
|
|
|
"time"
|
|
|
|
|
2014-12-23 13:09:02 +01:00
|
|
|
"github.com/aws/aws-sdk-go/aws"
|
|
|
|
"github.com/aws/aws-sdk-go/aws/awserr"
|
|
|
|
"github.com/aws/aws-sdk-go/aws/credentials"
|
2015-08-10 12:02:34 +02:00
|
|
|
"github.com/aws/aws-sdk-go/aws/service"
|
2014-12-23 13:09:02 +01:00
|
|
|
"github.com/aws/aws-sdk-go/service/s3"
|
|
|
|
"github.com/aws/aws-sdk-go/service/s3/s3manager"
|
2014-03-15 17:06:11 +01:00
|
|
|
"github.com/ncw/rclone/fs"
|
|
|
|
"github.com/ncw/swift"
|
|
|
|
)
|
2013-06-27 21:13:07 +02:00
|
|
|
|
|
|
|
// Register with Fs
|
|
|
|
func init() {
|
2014-03-15 17:06:11 +01:00
|
|
|
fs.Register(&fs.FsInfo{
|
|
|
|
Name: "s3",
|
|
|
|
NewFs: NewFs,
|
|
|
|
// AWS endpoints: http://docs.amazonwebservices.com/general/latest/gr/rande.html#s3_region
|
|
|
|
Options: []fs.Option{{
|
|
|
|
Name: "access_key_id",
|
|
|
|
Help: "AWS Access Key ID.",
|
|
|
|
}, {
|
|
|
|
Name: "secret_access_key",
|
|
|
|
Help: "AWS Secret Access Key (password). ",
|
|
|
|
}, {
|
2014-12-23 13:09:02 +01:00
|
|
|
Name: "region",
|
|
|
|
Help: "Region to connect to.",
|
2014-03-15 17:06:11 +01:00
|
|
|
Examples: []fs.OptionExample{{
|
2014-12-23 13:09:02 +01:00
|
|
|
Value: "us-east-1",
|
2014-03-15 17:06:11 +01:00
|
|
|
Help: "The default endpoint - a good choice if you are unsure.\nUS Region, Northern Virginia or Pacific Northwest.\nLeave location constraint empty.",
|
|
|
|
}, {
|
2014-12-23 13:09:02 +01:00
|
|
|
Value: "us-west-2",
|
2014-03-15 17:06:11 +01:00
|
|
|
Help: "US West (Oregon) Region\nNeeds location constraint us-west-2.",
|
|
|
|
}, {
|
2014-12-23 13:09:02 +01:00
|
|
|
Value: "us-west-1",
|
2014-03-15 17:06:11 +01:00
|
|
|
Help: "US West (Northern California) Region\nNeeds location constraint us-west-1.",
|
|
|
|
}, {
|
2014-12-23 13:09:02 +01:00
|
|
|
Value: "eu-west-1",
|
2014-03-15 17:06:11 +01:00
|
|
|
Help: "EU (Ireland) Region Region\nNeeds location constraint EU or eu-west-1.",
|
|
|
|
}, {
|
2014-12-23 13:09:02 +01:00
|
|
|
Value: "eu-central-1",
|
|
|
|
Help: "EU (Frankfurt) Region\nNeeds location constraint eu-central-1.",
|
|
|
|
}, {
|
|
|
|
Value: "ap-southeast-1",
|
2014-03-15 17:06:11 +01:00
|
|
|
Help: "Asia Pacific (Singapore) Region\nNeeds location constraint ap-southeast-1.",
|
|
|
|
}, {
|
2014-12-23 13:09:02 +01:00
|
|
|
Value: "ap-southeast-2",
|
|
|
|
Help: "Asia Pacific (Sydney) Region\nNeeds location constraint ap-southeast-2.",
|
2014-03-15 17:06:11 +01:00
|
|
|
}, {
|
2014-12-23 13:09:02 +01:00
|
|
|
Value: "ap-northeast-1",
|
2014-03-15 17:06:11 +01:00
|
|
|
Help: "Asia Pacific (Tokyo) Region\nNeeds location constraint ap-northeast-1.",
|
|
|
|
}, {
|
2014-12-23 13:09:02 +01:00
|
|
|
Value: "sa-east-1",
|
2014-03-15 17:06:11 +01:00
|
|
|
Help: "South America (Sao Paulo) Region\nNeeds location constraint sa-east-1.",
|
2014-12-23 13:09:02 +01:00
|
|
|
}, {
|
|
|
|
Value: "other-v2-signature",
|
|
|
|
Help: "If using an S3 clone that only understands v2 signatures - eg Ceph - set this and make sure you set the endpoint.",
|
|
|
|
}, {
|
|
|
|
Value: "other-v4-signature",
|
|
|
|
Help: "If using an S3 clone that understands v4 signatures set this and make sure you set the endpoint.",
|
2014-03-15 17:06:11 +01:00
|
|
|
}},
|
2014-12-23 13:09:02 +01:00
|
|
|
}, {
|
|
|
|
Name: "endpoint",
|
|
|
|
Help: "Endpoint for S3 API.\nLeave blank if using AWS to use the default endpoint for the region.\nSpecify if using an S3 clone such as Ceph.",
|
2014-03-15 17:06:11 +01:00
|
|
|
}, {
|
|
|
|
Name: "location_constraint",
|
2014-12-23 13:09:02 +01:00
|
|
|
Help: "Location constraint - must be set to match the Region. Used when creating buckets only.",
|
2014-03-15 17:06:11 +01:00
|
|
|
Examples: []fs.OptionExample{{
|
|
|
|
Value: "",
|
|
|
|
Help: "Empty for US Region, Northern Virginia or Pacific Northwest.",
|
|
|
|
}, {
|
|
|
|
Value: "us-west-2",
|
|
|
|
Help: "US West (Oregon) Region.",
|
|
|
|
}, {
|
|
|
|
Value: "us-west-1",
|
|
|
|
Help: "US West (Northern California) Region.",
|
|
|
|
}, {
|
|
|
|
Value: "eu-west-1",
|
|
|
|
Help: "EU (Ireland) Region.",
|
|
|
|
}, {
|
|
|
|
Value: "EU",
|
|
|
|
Help: "EU Region.",
|
|
|
|
}, {
|
|
|
|
Value: "ap-southeast-1",
|
|
|
|
Help: "Asia Pacific (Singapore) Region.",
|
|
|
|
}, {
|
|
|
|
Value: "ap-southeast-2",
|
|
|
|
Help: "Asia Pacific (Sydney) Region.",
|
|
|
|
}, {
|
|
|
|
Value: "ap-northeast-1",
|
|
|
|
Help: "Asia Pacific (Tokyo) Region.",
|
|
|
|
}, {
|
|
|
|
Value: "sa-east-1",
|
|
|
|
Help: "South America (Sao Paulo) Region.",
|
|
|
|
}},
|
|
|
|
}},
|
|
|
|
})
|
2013-06-27 21:13:07 +02:00
|
|
|
}
|
|
|
|
|
2013-01-08 19:53:35 +01:00
|
|
|
// Constants
|
|
|
|
const (
|
2014-12-23 13:09:02 +01:00
|
|
|
metaMtime = "Mtime" // the meta key to store mtime in - eg X-Amz-Meta-Mtime
|
|
|
|
listChunkSize = 1024 // number of items to read at once
|
|
|
|
maxRetries = 10 // number of retries to make of operations
|
2013-01-08 19:53:35 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
// FsS3 represents a remote s3 server
|
|
|
|
type FsS3 struct {
|
2015-08-22 17:53:11 +02:00
|
|
|
name string // the name of the remote
|
2014-12-23 13:09:02 +01:00
|
|
|
c *s3.S3 // the connection to the s3 server
|
|
|
|
bucket string // the bucket we are working on
|
|
|
|
perm string // permissions for new buckets / objects
|
|
|
|
root string // root of the bucket - ignore all objects above this
|
|
|
|
locationConstraint string // location constraint of new buckets
|
2013-01-08 19:53:35 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// FsObjectS3 describes a s3 object
|
|
|
|
type FsObjectS3 struct {
|
|
|
|
// Will definitely have everything but meta which may be nil
|
|
|
|
//
|
|
|
|
// List will read everything but meta - to fill that in need to call
|
|
|
|
// readMetaData
|
2014-12-23 13:09:02 +01:00
|
|
|
s3 *FsS3 // what this object is part of
|
|
|
|
remote string // The remote path
|
|
|
|
etag string // md5sum of the object
|
|
|
|
bytes int64 // size of the object
|
|
|
|
lastModified time.Time // Last modified
|
|
|
|
meta map[string]*string // The object metadata if known - may be nil
|
2013-01-08 19:53:35 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// ------------------------------------------------------------
|
|
|
|
|
2015-08-22 17:53:11 +02:00
|
|
|
// The name of the remote (as passed into NewFs)
|
|
|
|
func (f *FsS3) Name() string {
|
|
|
|
return f.name
|
|
|
|
}
|
|
|
|
|
2013-01-08 19:53:35 +01:00
|
|
|
// String converts this FsS3 to a string
|
|
|
|
func (f *FsS3) String() string {
|
2014-05-05 19:25:32 +02:00
|
|
|
if f.root == "" {
|
|
|
|
return fmt.Sprintf("S3 bucket %s", f.bucket)
|
|
|
|
}
|
|
|
|
return fmt.Sprintf("S3 bucket %s path %s", f.bucket, f.root)
|
2013-01-08 19:53:35 +01:00
|
|
|
}
|
|
|
|
|
2014-03-15 17:06:11 +01:00
|
|
|
// Pattern to match a s3 path
|
|
|
|
var matcher = regexp.MustCompile(`^([^/]*)(.*)$`)
|
|
|
|
|
2013-01-08 19:53:35 +01:00
|
|
|
// parseParse parses a s3 'url'
|
|
|
|
func s3ParsePath(path string) (bucket, directory string, err error) {
|
2014-03-15 17:06:11 +01:00
|
|
|
parts := matcher.FindStringSubmatch(path)
|
|
|
|
if parts == nil {
|
|
|
|
err = fmt.Errorf("Couldn't parse bucket out of s3 path %q", path)
|
2013-01-08 19:53:35 +01:00
|
|
|
} else {
|
2014-03-15 17:06:11 +01:00
|
|
|
bucket, directory = parts[1], parts[2]
|
2013-01-08 19:53:35 +01:00
|
|
|
directory = strings.Trim(directory, "/")
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// s3Connection makes a connection to s3
|
2014-03-15 17:06:11 +01:00
|
|
|
func s3Connection(name string) (*s3.S3, error) {
|
2013-01-08 19:53:35 +01:00
|
|
|
// Make the auth
|
2014-03-15 17:06:11 +01:00
|
|
|
accessKeyId := fs.ConfigFile.MustValue(name, "access_key_id")
|
|
|
|
if accessKeyId == "" {
|
|
|
|
return nil, errors.New("access_key_id not found")
|
2013-01-08 19:53:35 +01:00
|
|
|
}
|
2014-03-15 17:06:11 +01:00
|
|
|
secretAccessKey := fs.ConfigFile.MustValue(name, "secret_access_key")
|
|
|
|
if secretAccessKey == "" {
|
|
|
|
return nil, errors.New("secret_access_key not found")
|
2013-01-08 19:53:35 +01:00
|
|
|
}
|
2014-12-23 13:09:02 +01:00
|
|
|
auth := credentials.NewStaticCredentials(accessKeyId, secretAccessKey, "")
|
2013-01-08 19:53:35 +01:00
|
|
|
|
2014-12-23 13:09:02 +01:00
|
|
|
endpoint := fs.ConfigFile.MustValue(name, "endpoint")
|
|
|
|
region := fs.ConfigFile.MustValue(name, "region")
|
|
|
|
if region == "" && endpoint == "" {
|
|
|
|
endpoint = "https://s3.amazonaws.com/"
|
2013-01-08 19:53:35 +01:00
|
|
|
}
|
2014-12-23 13:09:02 +01:00
|
|
|
if region == "" {
|
|
|
|
region = "us-east-1"
|
2013-01-08 19:53:35 +01:00
|
|
|
}
|
2014-12-23 13:09:02 +01:00
|
|
|
awsConfig := aws.NewConfig().
|
|
|
|
WithRegion(region).
|
|
|
|
WithMaxRetries(maxRetries).
|
|
|
|
WithCredentials(auth).
|
|
|
|
WithEndpoint(endpoint).
|
2015-08-10 12:02:34 +02:00
|
|
|
WithHTTPClient(fs.Config.Client()).
|
|
|
|
WithS3ForcePathStyle(true)
|
|
|
|
// awsConfig.WithLogLevel(aws.LogDebugWithSigning)
|
2014-12-23 13:09:02 +01:00
|
|
|
c := s3.New(awsConfig)
|
|
|
|
if region == "other-v2-signature" {
|
2015-08-10 12:02:34 +02:00
|
|
|
fs.Debug(name, "Using v2 auth")
|
|
|
|
signer := func(req *service.Request) {
|
|
|
|
// Ignore AnonymousCredentials object
|
|
|
|
if req.Service.Config.Credentials == credentials.AnonymousCredentials {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
sign(accessKeyId, secretAccessKey, req.HTTPRequest)
|
|
|
|
}
|
|
|
|
c.Handlers.Sign.Clear()
|
|
|
|
c.Handlers.Sign.PushBack(service.BuildContentLength)
|
|
|
|
c.Handlers.Sign.PushBack(signer)
|
2013-01-08 19:53:35 +01:00
|
|
|
}
|
2015-08-10 12:02:34 +02:00
|
|
|
// Add user agent
|
|
|
|
c.Handlers.Build.PushBack(func(r *service.Request) {
|
|
|
|
r.HTTPRequest.Header.Set("User-Agent", fs.UserAgent)
|
|
|
|
})
|
2013-01-08 19:53:35 +01:00
|
|
|
return c, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// NewFsS3 contstructs an FsS3 from the path, bucket:path
|
2014-05-05 20:52:52 +02:00
|
|
|
func NewFs(name, root string) (fs.Fs, error) {
|
|
|
|
bucket, directory, err := s3ParsePath(root)
|
2013-01-08 19:53:35 +01:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2014-03-15 17:06:11 +01:00
|
|
|
c, err := s3Connection(name)
|
2013-01-08 19:53:35 +01:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
f := &FsS3{
|
2015-08-22 17:53:11 +02:00
|
|
|
name: name,
|
2013-01-08 19:53:35 +01:00
|
|
|
c: c,
|
|
|
|
bucket: bucket,
|
2014-12-23 13:09:02 +01:00
|
|
|
// FIXME perm: s3.Private, // FIXME need user to specify
|
|
|
|
root: directory,
|
|
|
|
locationConstraint: fs.ConfigFile.MustValue(name, "location_constraint"),
|
2013-01-08 19:53:35 +01:00
|
|
|
}
|
2014-05-05 20:52:52 +02:00
|
|
|
if f.root != "" {
|
|
|
|
f.root += "/"
|
|
|
|
// Check to see if the object exists
|
2014-12-23 13:09:02 +01:00
|
|
|
req := s3.HeadObjectInput{
|
|
|
|
Bucket: &f.bucket,
|
|
|
|
Key: &directory,
|
|
|
|
}
|
|
|
|
_, err = f.c.HeadObject(&req)
|
2014-05-05 20:52:52 +02:00
|
|
|
if err == nil {
|
|
|
|
remote := path.Base(directory)
|
|
|
|
f.root = path.Dir(directory)
|
|
|
|
if f.root == "." {
|
|
|
|
f.root = ""
|
|
|
|
} else {
|
|
|
|
f.root += "/"
|
|
|
|
}
|
|
|
|
obj := f.NewFsObject(remote)
|
|
|
|
// return a Fs Limited to this object
|
|
|
|
return fs.NewLimited(f, obj), nil
|
|
|
|
}
|
|
|
|
}
|
2014-12-23 13:09:02 +01:00
|
|
|
// f.listMultipartUploads()
|
2013-01-08 19:53:35 +01:00
|
|
|
return f, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Return an FsObject from a path
|
|
|
|
//
|
|
|
|
// May return nil if an error occurred
|
2014-12-23 13:09:02 +01:00
|
|
|
func (f *FsS3) newFsObjectWithInfo(remote string, info *s3.Object) fs.Object {
|
2013-06-27 21:13:07 +02:00
|
|
|
o := &FsObjectS3{
|
2013-01-08 19:53:35 +01:00
|
|
|
s3: f,
|
|
|
|
remote: remote,
|
|
|
|
}
|
|
|
|
if info != nil {
|
|
|
|
// Set info but not meta
|
2014-12-23 13:09:02 +01:00
|
|
|
if info.LastModified == nil {
|
|
|
|
fs.Log(o, "Failed to read last modified")
|
2013-06-27 21:13:07 +02:00
|
|
|
o.lastModified = time.Now()
|
2014-12-23 13:09:02 +01:00
|
|
|
} else {
|
|
|
|
o.lastModified = *info.LastModified
|
2013-01-08 19:53:35 +01:00
|
|
|
}
|
2014-12-23 13:09:02 +01:00
|
|
|
o.etag = aws.StringValue(info.ETag)
|
|
|
|
o.bytes = aws.Int64Value(info.Size)
|
2013-01-08 19:53:35 +01:00
|
|
|
} else {
|
2013-06-27 21:13:07 +02:00
|
|
|
err := o.readMetaData() // reads info and meta, returning an error
|
2013-01-08 19:53:35 +01:00
|
|
|
if err != nil {
|
|
|
|
// logged already FsDebug("Failed to read info: %s", err)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
2013-06-27 21:13:07 +02:00
|
|
|
return o
|
2013-01-08 19:53:35 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// Return an FsObject from a path
|
|
|
|
//
|
|
|
|
// May return nil if an error occurred
|
2013-06-28 09:57:32 +02:00
|
|
|
func (f *FsS3) NewFsObject(remote string) fs.Object {
|
2014-07-29 18:50:07 +02:00
|
|
|
return f.newFsObjectWithInfo(remote, nil)
|
2013-01-08 19:53:35 +01:00
|
|
|
}
|
|
|
|
|
2014-05-05 19:25:32 +02:00
|
|
|
// list the objects into the function supplied
|
|
|
|
//
|
|
|
|
// If directories is set it only sends directories
|
2014-12-23 13:09:02 +01:00
|
|
|
func (f *FsS3) list(directories bool, fn func(string, *s3.Object)) {
|
|
|
|
maxKeys := int64(listChunkSize)
|
2014-05-05 19:25:32 +02:00
|
|
|
delimiter := ""
|
|
|
|
if directories {
|
|
|
|
delimiter = "/"
|
|
|
|
}
|
2014-12-23 13:09:02 +01:00
|
|
|
var marker *string
|
2015-02-10 18:58:29 +01:00
|
|
|
for {
|
2014-12-23 13:09:02 +01:00
|
|
|
// FIXME need to implement ALL loop
|
|
|
|
req := s3.ListObjectsInput{
|
|
|
|
Bucket: &f.bucket,
|
|
|
|
Delimiter: &delimiter,
|
|
|
|
Prefix: &f.root,
|
|
|
|
MaxKeys: &maxKeys,
|
|
|
|
Marker: marker,
|
|
|
|
}
|
|
|
|
resp, err := f.c.ListObjects(&req)
|
2015-02-10 18:58:29 +01:00
|
|
|
if err != nil {
|
|
|
|
fs.Stats.Error()
|
2015-08-08 21:10:31 +02:00
|
|
|
fs.ErrorLog(f, "Couldn't read bucket %q: %s", f.bucket, err)
|
2014-12-23 13:09:02 +01:00
|
|
|
break
|
2013-01-08 19:53:35 +01:00
|
|
|
} else {
|
2015-02-10 18:58:29 +01:00
|
|
|
rootLength := len(f.root)
|
|
|
|
if directories {
|
2014-12-23 13:09:02 +01:00
|
|
|
for _, commonPrefix := range resp.CommonPrefixes {
|
|
|
|
if commonPrefix.Prefix == nil {
|
|
|
|
fs.Log(f, "Nil common prefix received")
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
remote := *commonPrefix.Prefix
|
2015-02-10 18:58:29 +01:00
|
|
|
if !strings.HasPrefix(remote, f.root) {
|
|
|
|
fs.Log(f, "Odd name received %q", remote)
|
|
|
|
continue
|
|
|
|
}
|
2014-12-23 13:09:02 +01:00
|
|
|
remote = remote[rootLength:]
|
2015-02-10 18:58:29 +01:00
|
|
|
if strings.HasSuffix(remote, "/") {
|
|
|
|
remote = remote[:len(remote)-1]
|
|
|
|
}
|
2014-12-23 13:09:02 +01:00
|
|
|
fn(remote, &s3.Object{Key: &remote})
|
2015-02-10 18:58:29 +01:00
|
|
|
}
|
|
|
|
} else {
|
2014-12-23 13:09:02 +01:00
|
|
|
for _, object := range resp.Contents {
|
|
|
|
key := aws.StringValue(object.Key)
|
|
|
|
if !strings.HasPrefix(key, f.root) {
|
|
|
|
fs.Log(f, "Odd name received %q", key)
|
2015-02-10 18:58:29 +01:00
|
|
|
continue
|
|
|
|
}
|
2014-12-23 13:09:02 +01:00
|
|
|
remote := key[rootLength:]
|
2015-02-10 18:58:29 +01:00
|
|
|
fn(remote, object)
|
2013-01-08 19:53:35 +01:00
|
|
|
}
|
|
|
|
}
|
2014-12-23 13:09:02 +01:00
|
|
|
if !aws.BoolValue(resp.IsTruncated) {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
// Use NextMarker if set, otherwise use last Key
|
|
|
|
if resp.NextMarker == nil || *resp.NextMarker == "" {
|
|
|
|
marker = resp.Contents[len(resp.Contents)-1].Key
|
|
|
|
} else {
|
|
|
|
marker = resp.NextMarker
|
|
|
|
}
|
2015-02-10 18:58:29 +01:00
|
|
|
}
|
2014-05-05 19:25:32 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Walk the path returning a channel of FsObjects
|
|
|
|
func (f *FsS3) List() fs.ObjectsChan {
|
|
|
|
out := make(fs.ObjectsChan, fs.Config.Checkers)
|
|
|
|
if f.bucket == "" {
|
|
|
|
// Return no objects at top level list
|
2013-01-08 19:53:35 +01:00
|
|
|
close(out)
|
2014-05-05 19:25:32 +02:00
|
|
|
fs.Stats.Error()
|
2015-08-08 21:10:31 +02:00
|
|
|
fs.ErrorLog(f, "Can't list objects at root - choose a bucket using lsd")
|
2014-05-05 19:25:32 +02:00
|
|
|
} else {
|
|
|
|
go func() {
|
|
|
|
defer close(out)
|
2014-12-23 13:09:02 +01:00
|
|
|
f.list(false, func(remote string, object *s3.Object) {
|
2014-07-29 18:50:07 +02:00
|
|
|
if fs := f.newFsObjectWithInfo(remote, object); fs != nil {
|
2014-05-05 19:25:32 +02:00
|
|
|
out <- fs
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}()
|
|
|
|
}
|
2013-01-08 19:53:35 +01:00
|
|
|
return out
|
|
|
|
}
|
|
|
|
|
2013-01-23 23:43:20 +01:00
|
|
|
// Lists the buckets
|
2013-06-28 09:57:32 +02:00
|
|
|
func (f *FsS3) ListDir() fs.DirChan {
|
|
|
|
out := make(fs.DirChan, fs.Config.Checkers)
|
2014-05-05 19:25:32 +02:00
|
|
|
if f.bucket == "" {
|
|
|
|
// List the buckets
|
|
|
|
go func() {
|
|
|
|
defer close(out)
|
2014-12-23 13:09:02 +01:00
|
|
|
req := s3.ListBucketsInput{}
|
|
|
|
resp, err := f.c.ListBuckets(&req)
|
2014-05-05 19:25:32 +02:00
|
|
|
if err != nil {
|
|
|
|
fs.Stats.Error()
|
2015-08-08 21:10:31 +02:00
|
|
|
fs.ErrorLog(f, "Couldn't list buckets: %s", err)
|
2014-05-05 19:25:32 +02:00
|
|
|
} else {
|
2014-12-23 13:09:02 +01:00
|
|
|
for _, bucket := range resp.Buckets {
|
2014-05-05 19:25:32 +02:00
|
|
|
out <- &fs.Dir{
|
2014-12-23 13:09:02 +01:00
|
|
|
Name: aws.StringValue(bucket.Name),
|
|
|
|
When: aws.TimeValue(bucket.CreationDate),
|
2014-05-05 19:25:32 +02:00
|
|
|
Bytes: -1,
|
|
|
|
Count: -1,
|
|
|
|
}
|
2013-01-23 23:43:20 +01:00
|
|
|
}
|
|
|
|
}
|
2014-05-05 19:25:32 +02:00
|
|
|
}()
|
|
|
|
} else {
|
2014-05-05 20:52:52 +02:00
|
|
|
// List the directories in the path in the bucket
|
2014-05-05 19:25:32 +02:00
|
|
|
go func() {
|
|
|
|
defer close(out)
|
2014-12-23 13:09:02 +01:00
|
|
|
f.list(true, func(remote string, object *s3.Object) {
|
|
|
|
size := int64(0)
|
|
|
|
if object.Size != nil {
|
|
|
|
size = *object.Size
|
|
|
|
}
|
2014-05-05 19:25:32 +02:00
|
|
|
out <- &fs.Dir{
|
|
|
|
Name: remote,
|
2014-12-23 13:09:02 +01:00
|
|
|
Bytes: size,
|
2014-05-05 19:25:32 +02:00
|
|
|
Count: 0,
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}()
|
|
|
|
}
|
2013-01-23 23:43:20 +01:00
|
|
|
return out
|
|
|
|
}
|
|
|
|
|
2013-01-08 19:53:35 +01:00
|
|
|
// Put the FsObject into the bucket
|
2013-06-28 09:57:32 +02:00
|
|
|
func (f *FsS3) Put(in io.Reader, remote string, modTime time.Time, size int64) (fs.Object, error) {
|
2013-01-08 19:53:35 +01:00
|
|
|
// Temporary FsObject under construction
|
2013-01-10 22:58:46 +01:00
|
|
|
fs := &FsObjectS3{s3: f, remote: remote}
|
2014-04-18 18:04:21 +02:00
|
|
|
return fs, fs.Update(in, modTime, size)
|
2013-01-08 19:53:35 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// Mkdir creates the bucket if it doesn't exist
|
|
|
|
func (f *FsS3) Mkdir() error {
|
2014-12-23 13:09:02 +01:00
|
|
|
req := s3.CreateBucketInput{
|
|
|
|
Bucket: &f.bucket,
|
|
|
|
ACL: &f.perm,
|
|
|
|
}
|
|
|
|
if f.locationConstraint != "" {
|
|
|
|
req.CreateBucketConfiguration = &s3.CreateBucketConfiguration{
|
|
|
|
LocationConstraint: &f.locationConstraint,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
_, err := f.c.CreateBucket(&req)
|
|
|
|
if err, ok := err.(awserr.Error); ok {
|
|
|
|
if err.Code() == "BucketAlreadyOwnedByYou" {
|
2013-01-08 23:31:16 +01:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return err
|
2013-01-08 19:53:35 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// Rmdir deletes the bucket
|
|
|
|
//
|
|
|
|
// Returns an error if it isn't empty
|
|
|
|
func (f *FsS3) Rmdir() error {
|
2014-12-23 13:09:02 +01:00
|
|
|
req := s3.DeleteBucketInput{
|
|
|
|
Bucket: &f.bucket,
|
|
|
|
}
|
|
|
|
_, err := f.c.DeleteBucket(&req)
|
|
|
|
return err
|
2013-01-08 19:53:35 +01:00
|
|
|
}
|
|
|
|
|
2013-01-19 00:21:02 +01:00
|
|
|
// Return the precision
|
2013-06-27 21:13:07 +02:00
|
|
|
func (f *FsS3) Precision() time.Duration {
|
2013-01-19 00:21:02 +01:00
|
|
|
return time.Nanosecond
|
|
|
|
}
|
|
|
|
|
2013-01-08 19:53:35 +01:00
|
|
|
// ------------------------------------------------------------
|
|
|
|
|
2014-03-28 18:56:04 +01:00
|
|
|
// Return the parent Fs
|
|
|
|
func (o *FsObjectS3) Fs() fs.Fs {
|
|
|
|
return o.s3
|
|
|
|
}
|
|
|
|
|
|
|
|
// Return a string version
|
|
|
|
func (o *FsObjectS3) String() string {
|
|
|
|
if o == nil {
|
|
|
|
return "<nil>"
|
|
|
|
}
|
|
|
|
return o.remote
|
|
|
|
}
|
|
|
|
|
2013-01-08 19:53:35 +01:00
|
|
|
// Return the remote path
|
2013-06-27 21:13:07 +02:00
|
|
|
func (o *FsObjectS3) Remote() string {
|
|
|
|
return o.remote
|
2013-01-08 19:53:35 +01:00
|
|
|
}
|
|
|
|
|
2015-05-09 11:37:43 +02:00
|
|
|
var matchMd5 = regexp.MustCompile(`^[0-9a-f]{32}$`)
|
|
|
|
|
2013-01-08 19:53:35 +01:00
|
|
|
// Md5sum returns the Md5sum of an object returning a lowercase hex string
|
2013-06-27 21:13:07 +02:00
|
|
|
func (o *FsObjectS3) Md5sum() (string, error) {
|
2015-05-09 11:37:43 +02:00
|
|
|
etag := strings.Trim(strings.ToLower(o.etag), `"`)
|
|
|
|
// Check the etag is a valid md5sum
|
|
|
|
if !matchMd5.MatchString(etag) {
|
2015-08-16 19:14:22 +02:00
|
|
|
// fs.Debug(o, "Invalid md5sum (probably multipart uploaded) - ignoring: %q", etag)
|
2015-05-09 11:37:43 +02:00
|
|
|
return "", nil
|
|
|
|
}
|
|
|
|
return etag, nil
|
2013-01-08 19:53:35 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// Size returns the size of an object in bytes
|
2013-06-27 21:13:07 +02:00
|
|
|
func (o *FsObjectS3) Size() int64 {
|
|
|
|
return o.bytes
|
2013-01-08 19:53:35 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// readMetaData gets the metadata if it hasn't already been fetched
|
|
|
|
//
|
|
|
|
// it also sets the info
|
2013-06-27 21:13:07 +02:00
|
|
|
func (o *FsObjectS3) readMetaData() (err error) {
|
|
|
|
if o.meta != nil {
|
2013-01-08 19:53:35 +01:00
|
|
|
return nil
|
|
|
|
}
|
2014-12-23 13:09:02 +01:00
|
|
|
key := o.s3.root + o.remote
|
|
|
|
req := s3.HeadObjectInput{
|
|
|
|
Bucket: &o.s3.bucket,
|
|
|
|
Key: &key,
|
2014-07-28 23:32:15 +02:00
|
|
|
}
|
2014-12-23 13:09:02 +01:00
|
|
|
resp, err := o.s3.c.HeadObject(&req)
|
2013-01-08 19:53:35 +01:00
|
|
|
if err != nil {
|
2013-06-28 09:57:32 +02:00
|
|
|
fs.Debug(o, "Failed to read info: %s", err)
|
2013-01-08 19:53:35 +01:00
|
|
|
return err
|
|
|
|
}
|
2014-05-16 17:27:53 +02:00
|
|
|
var size int64
|
|
|
|
// Ignore missing Content-Length assuming it is 0
|
|
|
|
// Some versions of ceph do this due their apache proxies
|
2014-12-23 13:09:02 +01:00
|
|
|
if resp.ContentLength != nil {
|
|
|
|
size = *resp.ContentLength
|
2013-01-08 19:53:35 +01:00
|
|
|
}
|
2014-12-23 13:09:02 +01:00
|
|
|
o.etag = aws.StringValue(resp.ETag)
|
2013-06-27 21:13:07 +02:00
|
|
|
o.bytes = size
|
2014-12-23 13:09:02 +01:00
|
|
|
o.meta = resp.Metadata
|
|
|
|
if resp.LastModified == nil {
|
2013-06-28 09:57:32 +02:00
|
|
|
fs.Log(o, "Failed to read last modified from HEAD: %s", err)
|
2013-06-27 21:13:07 +02:00
|
|
|
o.lastModified = time.Now()
|
2014-12-23 13:09:02 +01:00
|
|
|
} else {
|
|
|
|
o.lastModified = *resp.LastModified
|
2013-01-08 19:53:35 +01:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// ModTime returns the modification time of the object
|
|
|
|
//
|
|
|
|
// It attempts to read the objects mtime and if that isn't present the
|
|
|
|
// LastModified returned in the http headers
|
2013-06-27 21:13:07 +02:00
|
|
|
func (o *FsObjectS3) ModTime() time.Time {
|
|
|
|
err := o.readMetaData()
|
2013-01-08 19:53:35 +01:00
|
|
|
if err != nil {
|
2013-06-28 09:57:32 +02:00
|
|
|
fs.Log(o, "Failed to read metadata: %s", err)
|
2013-01-08 19:53:35 +01:00
|
|
|
return time.Now()
|
|
|
|
}
|
|
|
|
// read mtime out of metadata if available
|
2013-06-27 21:13:07 +02:00
|
|
|
d, ok := o.meta[metaMtime]
|
2014-12-23 13:09:02 +01:00
|
|
|
if !ok || d == nil {
|
2013-06-28 09:57:32 +02:00
|
|
|
// fs.Debug(o, "No metadata")
|
2013-06-27 21:13:07 +02:00
|
|
|
return o.lastModified
|
2013-01-08 19:53:35 +01:00
|
|
|
}
|
2014-12-23 13:09:02 +01:00
|
|
|
modTime, err := swift.FloatStringToTime(*d)
|
2013-01-08 19:53:35 +01:00
|
|
|
if err != nil {
|
2013-06-28 09:57:32 +02:00
|
|
|
fs.Log(o, "Failed to read mtime from object: %s", err)
|
2013-06-27 21:13:07 +02:00
|
|
|
return o.lastModified
|
2013-01-08 19:53:35 +01:00
|
|
|
}
|
|
|
|
return modTime
|
|
|
|
}
|
|
|
|
|
|
|
|
// Sets the modification time of the local fs object
|
2013-06-27 21:13:07 +02:00
|
|
|
func (o *FsObjectS3) SetModTime(modTime time.Time) {
|
|
|
|
err := o.readMetaData()
|
2013-01-08 23:31:16 +01:00
|
|
|
if err != nil {
|
2013-06-27 21:13:07 +02:00
|
|
|
fs.Stats.Error()
|
2015-08-08 21:10:31 +02:00
|
|
|
fs.ErrorLog(o, "Failed to read metadata: %s", err)
|
2013-01-08 23:31:16 +01:00
|
|
|
return
|
|
|
|
}
|
2014-12-23 13:09:02 +01:00
|
|
|
o.meta[metaMtime] = aws.String(swift.TimeToFloatString(modTime))
|
|
|
|
|
|
|
|
// Copy the object to itself to update the metadata
|
|
|
|
key := o.s3.root + o.remote
|
|
|
|
sourceKey := o.s3.bucket + "/" + key
|
|
|
|
directive := s3.MetadataDirectiveReplace // replace metadata with that passed in
|
|
|
|
req := s3.CopyObjectInput{
|
|
|
|
Bucket: &o.s3.bucket,
|
|
|
|
ACL: &o.s3.perm,
|
|
|
|
Key: &key,
|
|
|
|
CopySource: &sourceKey,
|
|
|
|
Metadata: o.meta,
|
|
|
|
MetadataDirective: &directive,
|
|
|
|
}
|
|
|
|
_, err = o.s3.c.CopyObject(&req)
|
2013-01-08 23:31:16 +01:00
|
|
|
if err != nil {
|
2013-06-27 21:13:07 +02:00
|
|
|
fs.Stats.Error()
|
2015-08-08 21:10:31 +02:00
|
|
|
fs.ErrorLog(o, "Failed to update remote mtime: %s", err)
|
2013-01-08 23:31:16 +01:00
|
|
|
}
|
2013-01-08 19:53:35 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// Is this object storable
|
2013-06-27 21:13:07 +02:00
|
|
|
func (o *FsObjectS3) Storable() bool {
|
2013-01-08 19:53:35 +01:00
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
// Open an object for read
|
2013-06-27 21:13:07 +02:00
|
|
|
func (o *FsObjectS3) Open() (in io.ReadCloser, err error) {
|
2014-12-23 13:09:02 +01:00
|
|
|
key := o.s3.root + o.remote
|
|
|
|
req := s3.GetObjectInput{
|
|
|
|
Bucket: &o.s3.bucket,
|
|
|
|
Key: &key,
|
|
|
|
}
|
|
|
|
resp, err := o.s3.c.GetObject(&req)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return resp.Body, nil
|
2013-01-08 19:53:35 +01:00
|
|
|
}
|
|
|
|
|
2014-04-18 18:04:21 +02:00
|
|
|
// Update the Object from in with modTime and size
|
|
|
|
func (o *FsObjectS3) Update(in io.Reader, modTime time.Time, size int64) error {
|
2014-12-23 13:09:02 +01:00
|
|
|
opts := s3manager.UploadOptions{
|
|
|
|
// PartSize: 64 * 1024 * 1024, use default
|
|
|
|
Concurrency: 2, // limit concurrency
|
|
|
|
LeavePartsOnError: false,
|
|
|
|
S3: o.s3.c,
|
2014-04-18 18:04:21 +02:00
|
|
|
}
|
2014-12-23 13:09:02 +01:00
|
|
|
uploader := s3manager.NewUploader(&opts)
|
2014-04-18 18:04:21 +02:00
|
|
|
|
2014-12-23 13:09:02 +01:00
|
|
|
// Set the mtime in the meta data
|
|
|
|
metadata := map[string]*string{
|
|
|
|
metaMtime: aws.String(swift.TimeToFloatString(modTime)),
|
|
|
|
}
|
|
|
|
|
|
|
|
// Guess the content type
|
|
|
|
contentType := fs.MimeType(o)
|
|
|
|
|
|
|
|
key := o.s3.root + o.remote
|
|
|
|
req := s3manager.UploadInput{
|
|
|
|
Bucket: &o.s3.bucket,
|
|
|
|
ACL: &o.s3.perm,
|
|
|
|
Key: &key,
|
|
|
|
Body: in,
|
|
|
|
ContentType: &contentType,
|
|
|
|
Metadata: metadata,
|
|
|
|
//ContentLength: &size,
|
|
|
|
}
|
|
|
|
_, err := uploader.Upload(&req)
|
2014-07-19 13:37:11 +02:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2014-12-23 13:09:02 +01:00
|
|
|
|
2014-07-19 13:37:11 +02:00
|
|
|
// Read the metadata from the newly created object
|
2014-07-20 12:23:05 +02:00
|
|
|
o.meta = nil // wipe old metadata
|
2014-07-19 13:37:11 +02:00
|
|
|
err = o.readMetaData()
|
2014-04-18 18:04:21 +02:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2013-01-08 19:53:35 +01:00
|
|
|
// Remove an object
|
2013-06-27 21:13:07 +02:00
|
|
|
func (o *FsObjectS3) Remove() error {
|
2014-12-23 13:09:02 +01:00
|
|
|
key := o.s3.root + o.remote
|
|
|
|
req := s3.DeleteObjectInput{
|
|
|
|
Bucket: &o.s3.bucket,
|
|
|
|
Key: &key,
|
|
|
|
}
|
|
|
|
_, err := o.s3.c.DeleteObject(&req)
|
|
|
|
return err
|
2013-01-08 19:53:35 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// Check the interfaces are satisfied
|
2013-06-27 21:13:07 +02:00
|
|
|
var _ fs.Fs = &FsS3{}
|
2013-06-28 09:57:32 +02:00
|
|
|
var _ fs.Object = &FsObjectS3{}
|