2014-12-22 23:24:45 +01:00
// Package s3 provides a storagedriver.StorageDriver implementation to
// store blobs in Amazon S3 cloud storage.
//
2016-01-29 00:48:49 +01:00
// This package leverages the docker/goamz client library for interfacing with
2016-01-22 03:17:53 +01:00
// S3. It is intended to be deprecated in favor of the s3-aws driver
// implementation.
2014-12-22 23:24:45 +01:00
//
2016-01-22 03:17:53 +01:00
// Because S3 is a key, value store the Stat call does not support last modification
2014-12-22 23:24:45 +01:00
// time for directories (directories are an abstraction for key, value stores)
//
2016-01-22 03:17:53 +01:00
// Keep in mind that S3 guarantees only read-after-write consistency for new
// objects, but no read-after-update or list-after-write consistency.
2014-10-25 01:37:25 +02:00
package s3
import (
"bytes"
2017-08-12 00:31:16 +02:00
"context"
2014-10-29 02:15:40 +01:00
"fmt"
2014-10-25 01:37:25 +02:00
"io"
2014-12-19 18:16:51 +01:00
"io/ioutil"
2014-10-25 01:37:25 +02:00
"net/http"
2015-04-22 23:31:34 +02:00
"reflect"
2014-10-25 01:37:25 +02:00
"strconv"
2014-12-19 18:16:51 +01:00
"strings"
"time"
2014-10-25 01:37:25 +02:00
2016-01-21 01:40:58 +01:00
"github.com/docker/distribution/registry/client/transport"
2015-02-11 03:14:23 +01:00
storagedriver "github.com/docker/distribution/registry/storage/driver"
"github.com/docker/distribution/registry/storage/driver/base"
"github.com/docker/distribution/registry/storage/driver/factory"
2017-08-12 00:31:16 +02:00
"github.com/docker/goamz/aws"
"github.com/docker/goamz/s3"
2014-10-25 01:37:25 +02:00
)
2016-01-22 03:17:53 +01:00
const driverName = "s3goamz"
2014-10-29 02:15:40 +01:00
2014-10-29 20:14:19 +01:00
// minChunkSize defines the minimum multipart upload chunk size
// S3 API requires multipart upload chunks to be at least 5MB
2015-01-24 01:46:43 +01:00
const minChunkSize = 5 << 20
const defaultChunkSize = 2 * minChunkSize
2014-10-25 01:37:25 +02:00
2014-12-19 18:16:51 +01:00
// listMax is the largest amount of objects you can request from S3 in a list call
const listMax = 1000
2014-10-25 01:37:25 +02:00
2015-01-07 11:18:42 +01:00
//DriverParameters A struct that encapsulates all of the driver parameters after all values have been set
type DriverParameters struct {
AccessKey string
SecretKey string
Bucket string
Region aws . Region
Encrypt bool
Secure bool
V4Auth bool
2015-01-24 01:46:43 +01:00
ChunkSize int64
2015-01-07 11:18:42 +01:00
RootDirectory string
2016-01-29 00:48:49 +01:00
StorageClass s3 . StorageClass
2016-01-21 01:40:58 +01:00
UserAgent string
2015-01-07 11:18:42 +01:00
}
2014-10-29 02:15:40 +01:00
func init ( ) {
2014-11-18 00:44:07 +01:00
factory . Register ( driverName , & s3DriverFactory { } )
2014-10-29 02:15:40 +01:00
}
2014-10-29 20:14:19 +01:00
// s3DriverFactory implements the factory.StorageDriverFactory interface
2014-10-29 02:15:40 +01:00
type s3DriverFactory struct { }
2014-12-19 18:16:51 +01:00
func ( factory * s3DriverFactory ) Create ( parameters map [ string ] interface { } ) ( storagedriver . StorageDriver , error ) {
2014-10-29 02:15:40 +01:00
return FromParameters ( parameters )
}
2015-02-04 01:54:52 +01:00
type driver struct {
2014-12-19 18:16:51 +01:00
S3 * s3 . S3
Bucket * s3 . Bucket
2015-01-24 01:46:43 +01:00
ChunkSize int64
2014-12-19 18:16:51 +01:00
Encrypt bool
2015-01-24 01:46:43 +01:00
RootDirectory string
2016-01-29 00:48:49 +01:00
StorageClass s3 . StorageClass
2014-10-25 01:37:25 +02:00
}
2015-02-04 01:54:52 +01:00
type baseEmbed struct {
base . Base
}
// Driver is a storagedriver.StorageDriver implementation backed by Amazon S3
// Objects are stored at absolute keys in the provided bucket.
type Driver struct {
baseEmbed
}
2014-11-18 00:44:07 +01:00
// FromParameters constructs a new Driver with a given parameters map
2014-10-29 02:15:40 +01:00
// Required parameters:
// - accesskey
// - secretkey
// - region
// - bucket
// - encrypt
2014-12-19 18:16:51 +01:00
func FromParameters ( parameters map [ string ] interface { } ) ( * Driver , error ) {
2014-12-22 23:24:45 +01:00
// Providing no values for these is valid in case the user is authenticating
// with an IAM on an ec2 instance (in which case the instance credentials will
// be summoned when GetAuth is called)
2016-02-03 04:30:48 +01:00
accessKey := parameters [ "accesskey" ]
if accessKey == nil {
2015-01-24 00:50:55 +01:00
accessKey = ""
}
2016-02-03 04:30:48 +01:00
secretKey := parameters [ "secretkey" ]
if secretKey == nil {
2015-01-24 00:50:55 +01:00
secretKey = ""
}
2014-10-29 02:15:40 +01:00
2016-02-03 04:30:48 +01:00
regionName := parameters [ "region" ]
if regionName == nil || fmt . Sprint ( regionName ) == "" {
2014-10-29 02:15:40 +01:00
return nil , fmt . Errorf ( "No region parameter provided" )
}
2014-12-19 18:16:51 +01:00
region := aws . GetRegion ( fmt . Sprint ( regionName ) )
2014-10-29 02:15:40 +01:00
if region . Name == "" {
2014-11-13 02:19:19 +01:00
return nil , fmt . Errorf ( "Invalid region provided: %v" , region )
2014-10-29 02:15:40 +01:00
}
2016-02-03 04:30:48 +01:00
bucket := parameters [ "bucket" ]
if bucket == nil || fmt . Sprint ( bucket ) == "" {
2014-10-29 02:15:40 +01:00
return nil , fmt . Errorf ( "No bucket parameter provided" )
}
2015-01-07 10:51:29 +01:00
encryptBool := false
2016-02-03 04:30:48 +01:00
encrypt := parameters [ "encrypt" ]
switch encrypt := encrypt . ( type ) {
case string :
b , err := strconv . ParseBool ( encrypt )
if err != nil {
2014-12-30 18:31:12 +01:00
return nil , fmt . Errorf ( "The encrypt parameter should be a boolean" )
}
2016-02-03 04:30:48 +01:00
encryptBool = b
case bool :
encryptBool = encrypt
case nil :
// do nothing
default :
return nil , fmt . Errorf ( "The encrypt parameter should be a boolean" )
2014-10-29 02:15:40 +01:00
}
2014-12-19 18:16:51 +01:00
2015-01-07 10:45:31 +01:00
secureBool := true
2016-02-03 04:30:48 +01:00
secure := parameters [ "secure" ]
switch secure := secure . ( type ) {
case string :
b , err := strconv . ParseBool ( secure )
if err != nil {
2014-12-29 21:29:54 +01:00
return nil , fmt . Errorf ( "The secure parameter should be a boolean" )
}
2016-02-03 04:30:48 +01:00
secureBool = b
case bool :
secureBool = secure
case nil :
// do nothing
default :
return nil , fmt . Errorf ( "The secure parameter should be a boolean" )
2014-12-29 21:29:54 +01:00
}
2015-02-06 02:41:04 +01:00
v4AuthBool := false
2016-02-03 04:30:48 +01:00
v4Auth := parameters [ "v4auth" ]
switch v4Auth := v4Auth . ( type ) {
case string :
b , err := strconv . ParseBool ( v4Auth )
if err != nil {
2015-01-07 10:45:31 +01:00
return nil , fmt . Errorf ( "The v4auth parameter should be a boolean" )
}
2016-02-03 04:30:48 +01:00
v4AuthBool = b
case bool :
v4AuthBool = v4Auth
case nil :
// do nothing
default :
return nil , fmt . Errorf ( "The v4auth parameter should be a boolean" )
2015-01-07 10:45:31 +01:00
}
2015-01-24 01:46:43 +01:00
chunkSize := int64 ( defaultChunkSize )
2016-02-03 04:30:48 +01:00
chunkSizeParam := parameters [ "chunksize" ]
switch v := chunkSizeParam . ( type ) {
case string :
vv , err := strconv . ParseInt ( v , 0 , 64 )
if err != nil {
return nil , fmt . Errorf ( "chunksize parameter must be an integer, %v invalid" , chunkSizeParam )
2015-04-22 23:31:34 +02:00
}
2016-02-03 04:30:48 +01:00
chunkSize = vv
case int64 :
chunkSize = v
case int , uint , int32 , uint32 , uint64 :
chunkSize = reflect . ValueOf ( v ) . Convert ( reflect . TypeOf ( chunkSize ) ) . Int ( )
case nil :
// do nothing
default :
return nil , fmt . Errorf ( "invalid value for chunksize: %#v" , chunkSizeParam )
}
2015-04-22 23:31:34 +02:00
2016-02-03 04:30:48 +01:00
if chunkSize < minChunkSize {
return nil , fmt . Errorf ( "The chunksize %#v parameter should be a number that is larger than or equal to %d" , chunkSize , minChunkSize )
2015-01-24 01:46:43 +01:00
}
2016-02-03 04:30:48 +01:00
rootDirectory := parameters [ "rootdirectory" ]
if rootDirectory == nil {
2014-12-30 18:31:12 +01:00
rootDirectory = ""
2014-12-19 18:16:51 +01:00
}
2016-01-29 00:48:49 +01:00
storageClass := s3 . StandardStorage
2016-02-03 04:30:48 +01:00
storageClassParam := parameters [ "storageclass" ]
if storageClassParam != nil {
2016-01-29 00:48:49 +01:00
storageClassString , ok := storageClassParam . ( string )
if ! ok {
return nil , fmt . Errorf ( "The storageclass parameter must be one of %v, %v invalid" , [ ] s3 . StorageClass { s3 . StandardStorage , s3 . ReducedRedundancy } , storageClassParam )
}
// All valid storage class parameters are UPPERCASE, so be a bit more flexible here
storageClassCasted := s3 . StorageClass ( strings . ToUpper ( storageClassString ) )
if storageClassCasted != s3 . StandardStorage && storageClassCasted != s3 . ReducedRedundancy {
return nil , fmt . Errorf ( "The storageclass parameter must be one of %v, %v invalid" , [ ] s3 . StorageClass { s3 . StandardStorage , s3 . ReducedRedundancy } , storageClassParam )
}
storageClass = storageClassCasted
}
2016-02-03 04:30:48 +01:00
userAgent := parameters [ "useragent" ]
if userAgent == nil {
2016-01-21 01:40:58 +01:00
userAgent = ""
}
2015-01-07 11:18:42 +01:00
params := DriverParameters {
fmt . Sprint ( accessKey ) ,
fmt . Sprint ( secretKey ) ,
fmt . Sprint ( bucket ) ,
region ,
encryptBool ,
secureBool ,
v4AuthBool ,
2015-01-24 01:46:43 +01:00
chunkSize ,
2015-01-07 11:18:42 +01:00
fmt . Sprint ( rootDirectory ) ,
2016-01-29 00:48:49 +01:00
storageClass ,
2016-01-21 01:40:58 +01:00
fmt . Sprint ( userAgent ) ,
2015-01-07 11:18:42 +01:00
}
return New ( params )
2014-10-29 02:15:40 +01:00
}
2014-11-18 00:44:07 +01:00
// New constructs a new Driver with the given AWS credentials, region, encryption flag, and
2014-10-29 20:14:19 +01:00
// bucketName
2015-01-07 11:18:42 +01:00
func New ( params DriverParameters ) ( * Driver , error ) {
auth , err := aws . GetAuth ( params . AccessKey , params . SecretKey , "" , time . Time { } )
2014-12-19 18:16:51 +01:00
if err != nil {
2015-05-23 01:45:45 +02:00
return nil , fmt . Errorf ( "unable to resolve aws credentials, please ensure that 'accesskey' and 'secretkey' are properly set or the credentials are available in $HOME/.aws/credentials: %v" , err )
2014-12-19 18:16:51 +01:00
}
2015-01-07 11:18:42 +01:00
if ! params . Secure {
params . Region . S3Endpoint = strings . Replace ( params . Region . S3Endpoint , "https" , "http" , 1 )
2014-12-29 21:29:54 +01:00
}
2015-01-07 11:18:42 +01:00
s3obj := s3 . New ( auth , params . Region )
2016-01-21 01:40:58 +01:00
if params . UserAgent != "" {
s3obj . Client = & http . Client {
Transport : transport . NewTransport ( http . DefaultTransport ,
transport . NewHeaderRequestModifier ( http . Header {
http . CanonicalHeaderKey ( "User-Agent" ) : [ ] string { params . UserAgent } ,
} ) ,
) ,
}
}
2014-10-25 01:37:25 +02:00
2015-01-07 11:18:42 +01:00
if params . V4Auth {
2015-01-07 10:45:31 +01:00
s3obj . Signature = aws . V4Signature
2017-02-07 10:16:16 +01:00
} else if mustV4Auth ( params . Region . Name ) {
return nil , fmt . Errorf ( "The %s region only works with v4 authentication" , params . Region . Name )
2015-01-07 10:45:31 +01:00
}
2014-10-25 01:37:25 +02:00
2016-01-21 01:40:58 +01:00
bucket := s3obj . Bucket ( params . Bucket )
2014-12-21 07:48:42 +01:00
// TODO Currently multipart uploads have no timestamps, so this would be unwise
// if you initiated a new s3driver while another one is running on the same bucket.
// 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
// }
// }
2014-12-19 18:16:51 +01:00
2015-02-04 01:54:52 +01:00
d := & driver {
2015-01-24 01:46:43 +01:00
S3 : s3obj ,
Bucket : bucket ,
ChunkSize : params . ChunkSize ,
Encrypt : params . Encrypt ,
2015-02-04 01:54:52 +01:00
RootDirectory : params . RootDirectory ,
2016-01-29 00:48:49 +01:00
StorageClass : params . StorageClass ,
2015-02-04 01:54:52 +01:00
}
return & Driver {
baseEmbed : baseEmbed {
Base : base . Base {
StorageDriver : d ,
} ,
} ,
} , nil
2014-10-25 01:37:25 +02:00
}
2014-10-29 20:14:19 +01:00
// Implement the storagedriver.StorageDriver interface
2015-04-23 02:30:01 +02:00
func ( d * driver ) Name ( ) string {
return driverName
}
2014-11-18 00:44:07 +01:00
// GetContent retrieves the content stored at "path" as a []byte.
2015-04-28 00:58:58 +02:00
func ( d * driver ) GetContent ( ctx context . Context , path string ) ( [ ] byte , error ) {
2014-12-19 18:16:51 +01:00
content , err := d . Bucket . Get ( d . s3Path ( path ) )
2014-11-19 02:41:48 +01:00
if err != nil {
2014-12-19 18:16:51 +01:00
return nil , parseError ( path , err )
2014-11-19 02:41:48 +01:00
}
return content , nil
2014-10-25 01:37:25 +02:00
}
2014-11-18 00:44:07 +01:00
// PutContent stores the []byte content at a location designated by "path".
2015-04-28 00:58:58 +02:00
func ( d * driver ) PutContent ( ctx context . Context , path string , contents [ ] byte ) error {
2014-12-19 18:16:51 +01:00
return parseError ( path , d . Bucket . Put ( d . s3Path ( path ) , contents , d . getContentType ( ) , getPermissions ( ) , d . getOptions ( ) ) )
2014-10-25 01:37:25 +02:00
}
2016-02-08 23:29:21 +01:00
// Reader retrieves an io.ReadCloser for the content stored at "path" with a
2014-11-18 00:44:07 +01:00
// given byte offset.
2016-02-08 23:29:21 +01:00
func ( d * driver ) Reader ( ctx context . Context , path string , offset int64 ) ( io . ReadCloser , error ) {
2014-10-25 01:37:25 +02:00
headers := make ( http . Header )
2014-12-03 04:01:00 +01:00
headers . Add ( "Range" , "bytes=" + strconv . FormatInt ( offset , 10 ) + "-" )
2014-10-25 01:37:25 +02:00
2014-12-19 18:16:51 +01:00
resp , err := d . Bucket . GetResponseWithHeaders ( d . s3Path ( path ) , headers )
2014-11-19 02:41:48 +01:00
if err != nil {
2014-12-19 18:16:51 +01: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-25 01:37:25 +02:00
}
2014-11-19 02:41:48 +01:00
return resp . Body , nil
2014-10-25 01:37:25 +02:00
}
2016-02-08 23:29:21 +01:00
// Writer returns a FileWriter which will store the content written to it
// at the location designated by "path" after the call to Commit.
func ( d * driver ) Writer ( ctx context . Context , path string , append bool ) ( storagedriver . FileWriter , error ) {
key := d . s3Path ( path )
if ! append {
// TODO (brianbland): cancel other uploads at this path
multi , err := d . Bucket . InitMulti ( key , d . getContentType ( ) , getPermissions ( ) , d . getOptions ( ) )
2014-12-19 18:16:51 +01:00
if err != nil {
2016-02-08 23:29:21 +01:00
return nil , err
2014-12-19 18:20:07 +01:00
}
2016-02-08 23:29:21 +01:00
return d . newWriter ( key , multi , nil ) , nil
2014-12-19 18:20:07 +01:00
}
2016-02-08 23:29:21 +01:00
multis , _ , err := d . Bucket . ListMulti ( key , "" )
if err != nil {
return nil , parseError ( path , err )
2014-12-19 18:20:07 +01:00
}
2016-02-08 23:29:21 +01:00
for _ , multi := range multis {
if key != multi . Key {
continue
2014-12-19 18:20:07 +01:00
}
2016-02-08 23:29:21 +01:00
parts , err := multi . ListParts ( )
if err != nil {
return nil , parseError ( path , err )
2014-12-19 18:16:51 +01:00
}
2016-02-08 23:29:21 +01:00
var multiSize int64
for _ , part := range parts {
multiSize += part . Size
2014-10-25 01:37:25 +02:00
}
2016-02-08 23:29:21 +01:00
return d . newWriter ( key , multi , parts ) , nil
2014-10-25 01:37:25 +02:00
}
2016-02-08 23:29:21 +01:00
return nil , storagedriver . PathNotFoundError { Path : path }
2014-10-25 01:37:25 +02:00
}
2014-12-19 18:16:51 +01:00
// Stat retrieves the FileInfo for the given path, including the current size
// in bytes and the creation time.
2015-04-28 00:58:58 +02:00
func ( d * driver ) Stat ( ctx context . Context , path string ) ( storagedriver . FileInfo , error ) {
2014-12-19 18:16:51 +01:00
listResponse , err := d . Bucket . List ( d . s3Path ( path ) , "" , "" , 1 )
2014-10-25 01:37:25 +02:00
if err != nil {
2014-12-19 18:16:51 +01:00
return nil , err
2014-10-25 01:37:25 +02:00
}
2014-12-19 18:16:51 +01:00
fi := storagedriver . FileInfoFields {
Path : path ,
2014-10-25 01:37:25 +02:00
}
2014-12-19 18:16:51 +01: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-25 01:37:25 +02:00
}
2014-12-19 18:16:51 +01:00
// List returns a list of the objects that are direct descendants of the given path.
2015-12-08 20:02:40 +01:00
func ( d * driver ) List ( ctx context . Context , opath string ) ( [ ] string , error ) {
path := opath
2014-12-19 18:16:51 +01:00
if path != "/" && path [ len ( path ) - 1 ] != '/' {
2014-11-07 21:58:48 +01:00
path = path + "/"
2014-11-04 01:20:38 +01:00
}
2015-02-20 01:31:34 +01:00
// This is to cover for the cases when the rootDirectory of the driver is either "" or "/".
// In those cases, there is no root prefix to replace and we must actually add a "/" to all
// results in order to keep them as valid paths as recognized by storagedriver.PathRegexp
prefix := ""
if d . s3Path ( "" ) == "" {
prefix = "/"
}
2014-12-19 18:16:51 +01:00
listResponse , err := d . Bucket . List ( d . s3Path ( path ) , "/" , "" , listMax )
2014-10-25 01:37:25 +02:00
if err != nil {
2015-12-08 20:02:40 +01:00
return nil , parseError ( opath , err )
2015-11-24 23:23:12 +01:00
}
2014-10-25 01:37:25 +02:00
files := [ ] string { }
directories := [ ] string { }
2014-11-04 01:20:38 +01:00
for {
2014-10-25 01:37:25 +02:00
for _ , key := range listResponse . Contents {
2015-02-20 01:31:34 +01:00
files = append ( files , strings . Replace ( key . Key , d . s3Path ( "" ) , prefix , 1 ) )
2014-10-25 01:37:25 +02:00
}
for _ , commonPrefix := range listResponse . CommonPrefixes {
2015-02-20 01:31:34 +01:00
directories = append ( directories , strings . Replace ( commonPrefix [ 0 : len ( commonPrefix ) - 1 ] , d . s3Path ( "" ) , prefix , 1 ) )
2014-10-25 01:37:25 +02:00
}
2017-02-13 10:20:07 +01:00
if ! listResponse . IsTruncated {
2014-11-04 01:20:38 +01:00
break
2014-10-25 01:37:25 +02:00
}
2017-02-13 10:20:07 +01:00
listResponse , err = d . Bucket . List ( d . s3Path ( path ) , "/" , listResponse . NextMarker , listMax )
if err != nil {
return nil , err
}
2014-10-25 01:37:25 +02:00
}
2015-12-08 20:02:40 +01:00
if opath != "/" {
if len ( files ) == 0 && len ( directories ) == 0 {
// Treat empty response as missing directory, since we don't actually
// have directories in s3.
return nil , storagedriver . PathNotFoundError { Path : opath }
}
}
2014-10-25 01:37:25 +02:00
return append ( files , directories ... ) , nil
}
2014-11-18 00:44:07 +01:00
// Move moves an object stored at sourcePath to destPath, removing the original
// object.
2015-04-28 00:58:58 +02:00
func ( d * driver ) Move ( ctx context . Context , sourcePath string , destPath string ) error {
2014-10-25 01:37:25 +02:00
/* This is terrible, but aws doesn't have an actual move. */
2014-12-19 18:16:51 +01: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-25 01:37:25 +02:00
if err != nil {
2014-12-19 18:16:51 +01:00
return parseError ( sourcePath , err )
2014-10-25 01:37:25 +02:00
}
2015-04-28 00:58:58 +02:00
return d . Delete ( ctx , sourcePath )
2014-10-25 01:37:25 +02:00
}
2014-11-18 00:44:07 +01:00
// Delete recursively deletes all objects stored at "path" and its subpaths.
2015-04-28 00:58:58 +02:00
func ( d * driver ) Delete ( ctx context . Context , path string ) error {
2016-10-14 23:53:00 +02:00
s3Path := d . s3Path ( path )
listResponse , err := d . Bucket . List ( s3Path , "" , "" , listMax )
2014-10-25 01:37:25 +02:00
if err != nil || len ( listResponse . Contents ) == 0 {
2014-11-13 02:19:19 +01:00
return storagedriver . PathNotFoundError { Path : path }
2014-10-25 01:37:25 +02:00
}
2014-12-19 18:16:51 +01:00
s3Objects := make ( [ ] s3 . Object , listMax )
2014-10-25 01:37:25 +02:00
for len ( listResponse . Contents ) > 0 {
2016-10-14 23:53:00 +02:00
numS3Objects := len ( listResponse . Contents )
2014-10-25 01:37:25 +02:00
for index , key := range listResponse . Contents {
2016-10-14 23:53:00 +02:00
// Stop if we encounter a key that is not a subpath (so that deleting "/a" does not delete "/ab").
if len ( key . Key ) > len ( s3Path ) && ( key . Key ) [ len ( s3Path ) ] != '/' {
numS3Objects = index
break
}
2014-10-25 01:37:25 +02:00
s3Objects [ index ] . Key = key . Key
}
2016-10-14 23:53:00 +02:00
err := d . Bucket . DelMulti ( s3 . Delete { Quiet : false , Objects : s3Objects [ 0 : numS3Objects ] } )
2014-10-25 01:37:25 +02:00
if err != nil {
return nil
}
2016-10-14 23:53:00 +02:00
if numS3Objects < len ( listResponse . Contents ) {
return nil
}
2014-12-19 18:16:51 +01:00
listResponse , err = d . Bucket . List ( d . s3Path ( path ) , "" , "" , listMax )
2014-10-25 01:37:25 +02:00
if err != nil {
return err
}
}
return nil
}
2015-01-07 17:31:38 +01:00
// URLFor returns a URL which may be used to retrieve the content stored at the given path.
// May return an UnsupportedMethodErr in certain StorageDriver implementations.
2015-04-28 00:58:58 +02:00
func ( d * driver ) URLFor ( ctx context . Context , path string , options map [ string ] interface { } ) ( string , error ) {
2015-01-14 20:31:11 +01:00
methodString := "GET"
method , ok := options [ "method" ]
if ok {
methodString , ok = method . ( string )
if ! ok || ( methodString != "GET" && methodString != "HEAD" ) {
2015-11-02 22:23:53 +01:00
return "" , storagedriver . ErrUnsupportedMethod { }
2015-01-14 20:31:11 +01:00
}
}
2015-01-09 02:10:32 +01:00
expiresTime := time . Now ( ) . Add ( 20 * time . Minute )
2015-01-09 02:45:21 +01:00
expires , ok := options [ "expiry" ]
2015-01-09 02:10:32 +01:00
if ok {
et , ok := expires . ( time . Time )
if ok {
expiresTime = et
}
}
2015-01-14 20:31:11 +01:00
return d . Bucket . SignedURLWithMethod ( methodString , d . s3Path ( path ) , expiresTime , nil , nil ) , nil
2015-01-07 17:31:38 +01:00
}
2015-02-04 01:54:52 +01:00
func ( d * driver ) s3Path ( path string ) string {
2015-01-24 01:46:43 +01:00
return strings . TrimLeft ( strings . TrimRight ( d . RootDirectory , "/" ) + path , "/" )
2014-12-19 18:16:51 +01:00
}
2014-10-25 01:37:25 +02:00
2015-04-07 01:23:31 +02:00
// S3BucketKey returns the s3 bucket key for the given storage driver path.
func ( d * Driver ) S3BucketKey ( path string ) string {
return d . StorageDriver . ( * driver ) . s3Path ( path )
}
2017-11-29 20:17:39 +01:00
// Walk traverses a filesystem defined within driver, starting
// from the given path, calling f on each file
func ( d * driver ) Walk ( ctx context . Context , path string , f storagedriver . WalkFn ) error {
return storagedriver . WalkFallback ( ctx , d , path , f )
}
2014-12-19 18:16:51 +01: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-25 01:37:25 +02:00
}
2014-12-19 18:16:51 +01:00
return err
2014-10-25 01:37:25 +02:00
}
2015-02-04 01:54:52 +01:00
func ( d * driver ) getOptions ( ) s3 . Options {
2016-01-29 00:48:49 +01:00
return s3 . Options {
SSE : d . Encrypt ,
StorageClass : d . StorageClass ,
}
2014-10-25 01:37:25 +02:00
}
2014-10-26 18:00:53 +01:00
func getPermissions ( ) s3 . ACL {
2014-10-25 01:37:25 +02:00
return s3 . Private
}
2017-02-07 10:16:16 +01:00
// mustV4Auth checks whether must use v4 auth in specific region.
// Please see documentation at http://docs.aws.amazon.com/general/latest/gr/signature-version-2.html
func mustV4Auth ( region string ) bool {
switch region {
case "eu-central-1" , "cn-north-1" , "us-east-2" ,
"ca-central-1" , "ap-south-1" , "ap-northeast-2" , "eu-west-2" :
return true
}
return false
}
2015-02-04 01:54:52 +01:00
func ( d * driver ) getContentType ( ) string {
2014-10-25 01:37:25 +02:00
return "application/octet-stream"
}
2015-04-23 00:07:18 +02:00
2016-02-08 23:29:21 +01:00
// writer attempts to upload parts to S3 in a buffered fashion where the last
// part is at least as large as the chunksize, so the multipart upload could be
// cleanly resumed in the future. This is violated if Close is called after less
// than a full chunk is written.
type writer struct {
driver * driver
key string
multi * s3 . Multi
parts [ ] s3 . Part
size int64
readyPart [ ] byte
pendingPart [ ] byte
closed bool
committed bool
cancelled bool
2015-04-23 00:07:18 +02:00
}
2016-02-08 23:29:21 +01:00
func ( d * driver ) newWriter ( key string , multi * s3 . Multi , parts [ ] s3 . Part ) storagedriver . FileWriter {
var size int64
for _ , part := range parts {
size += part . Size
}
return & writer {
driver : d ,
key : key ,
multi : multi ,
parts : parts ,
size : size ,
}
}
func ( w * writer ) Write ( p [ ] byte ) ( int , error ) {
if w . closed {
return 0 , fmt . Errorf ( "already closed" )
} else if w . committed {
return 0 , fmt . Errorf ( "already committed" )
} else if w . cancelled {
return 0 , fmt . Errorf ( "already cancelled" )
}
// If the last written part is smaller than minChunkSize, we need to make a
// new multipart upload :sadface:
if len ( w . parts ) > 0 && int ( w . parts [ len ( w . parts ) - 1 ] . Size ) < minChunkSize {
err := w . multi . Complete ( w . parts )
if err != nil {
w . multi . Abort ( )
return 0 , err
}
multi , err := w . driver . Bucket . InitMulti ( w . key , w . driver . getContentType ( ) , getPermissions ( ) , w . driver . getOptions ( ) )
if err != nil {
return 0 , err
}
w . multi = multi
// If the entire written file is smaller than minChunkSize, we need to make
// a new part from scratch :double sad face:
if w . size < minChunkSize {
contents , err := w . driver . Bucket . Get ( w . key )
if err != nil {
return 0 , err
}
w . parts = nil
w . readyPart = contents
} else {
// Otherwise we can use the old file as the new first part
_ , part , err := multi . PutPartCopy ( 1 , s3 . CopyOptions { } , w . driver . Bucket . Name + "/" + w . key )
if err != nil {
return 0 , err
}
w . parts = [ ] s3 . Part { part }
}
}
var n int
for len ( p ) > 0 {
// If no parts are ready to write, fill up the first part
if neededBytes := int ( w . driver . ChunkSize ) - len ( w . readyPart ) ; neededBytes > 0 {
if len ( p ) >= neededBytes {
w . readyPart = append ( w . readyPart , p [ : neededBytes ] ... )
n += neededBytes
p = p [ neededBytes : ]
} else {
w . readyPart = append ( w . readyPart , p ... )
n += len ( p )
p = nil
}
}
if neededBytes := int ( w . driver . ChunkSize ) - len ( w . pendingPart ) ; neededBytes > 0 {
if len ( p ) >= neededBytes {
w . pendingPart = append ( w . pendingPart , p [ : neededBytes ] ... )
n += neededBytes
p = p [ neededBytes : ]
err := w . flushPart ( )
if err != nil {
w . size += int64 ( n )
return n , err
}
} else {
w . pendingPart = append ( w . pendingPart , p ... )
n += len ( p )
p = nil
}
}
}
w . size += int64 ( n )
return n , nil
}
func ( w * writer ) Size ( ) int64 {
return w . size
}
func ( w * writer ) Close ( ) error {
if w . closed {
return fmt . Errorf ( "already closed" )
}
w . closed = true
return w . flushPart ( )
}
func ( w * writer ) Cancel ( ) error {
if w . closed {
return fmt . Errorf ( "already closed" )
} else if w . committed {
return fmt . Errorf ( "already committed" )
}
w . cancelled = true
err := w . multi . Abort ( )
return err
}
func ( w * writer ) Commit ( ) error {
if w . closed {
return fmt . Errorf ( "already closed" )
} else if w . committed {
return fmt . Errorf ( "already committed" )
} else if w . cancelled {
return fmt . Errorf ( "already cancelled" )
}
err := w . flushPart ( )
if err != nil {
return err
}
w . committed = true
err = w . multi . Complete ( w . parts )
if err != nil {
w . multi . Abort ( )
return err
}
return nil
}
// flushPart flushes buffers to write a part to S3.
// Only called by Write (with both buffers full) and Close/Commit (always)
func ( w * writer ) flushPart ( ) error {
if len ( w . readyPart ) == 0 && len ( w . pendingPart ) == 0 {
// nothing to write
return nil
}
if len ( w . pendingPart ) < int ( w . driver . ChunkSize ) {
// closing with a small pending part
// combine ready and pending to avoid writing a small part
w . readyPart = append ( w . readyPart , w . pendingPart ... )
w . pendingPart = nil
}
part , err := w . multi . PutPart ( len ( w . parts ) + 1 , bytes . NewReader ( w . readyPart ) )
if err != nil {
return err
}
w . parts = append ( w . parts , part )
w . readyPart = w . pendingPart
w . pendingPart = nil
return nil
2015-04-23 00:07:18 +02:00
}