2022-07-17 18:16:00 +00:00
|
|
|
// Copyright (C) 2019-2022 Nicola Murino
|
|
|
|
//
|
|
|
|
// This program is free software: you can redistribute it and/or modify
|
|
|
|
// it under the terms of the GNU Affero General Public License as published
|
|
|
|
// by the Free Software Foundation, version 3.
|
|
|
|
//
|
|
|
|
// This program is distributed in the hope that it will be useful,
|
|
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
// GNU Affero General Public License for more details.
|
|
|
|
//
|
|
|
|
// You should have received a copy of the GNU Affero General Public License
|
|
|
|
// along with this program. If not, see <https://www.gnu.org/licenses/>.
|
|
|
|
|
2021-08-19 13:51:43 +00:00
|
|
|
//go:build !nos3
|
2020-05-23 09:58:05 +00:00
|
|
|
// +build !nos3
|
|
|
|
|
2020-01-19 06:41:05 +00:00
|
|
|
package vfs
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2022-03-15 18:16:50 +00:00
|
|
|
"errors"
|
2020-01-19 06:41:05 +00:00
|
|
|
"fmt"
|
2020-10-25 07:18:48 +00:00
|
|
|
"mime"
|
2022-03-15 18:16:50 +00:00
|
|
|
"net"
|
|
|
|
"net/http"
|
2020-12-24 10:13:42 +00:00
|
|
|
"net/url"
|
2020-01-19 06:41:05 +00:00
|
|
|
"os"
|
|
|
|
"path"
|
2020-06-26 21:38:29 +00:00
|
|
|
"path/filepath"
|
2022-06-30 16:22:58 +00:00
|
|
|
"sort"
|
2020-01-19 06:41:05 +00:00
|
|
|
"strings"
|
2022-06-30 16:22:58 +00:00
|
|
|
"sync"
|
|
|
|
"sync/atomic"
|
2020-01-19 06:41:05 +00:00
|
|
|
"time"
|
|
|
|
|
2022-03-15 18:16:50 +00:00
|
|
|
"github.com/aws/aws-sdk-go-v2/aws"
|
|
|
|
awshttp "github.com/aws/aws-sdk-go-v2/aws/transport/http"
|
|
|
|
"github.com/aws/aws-sdk-go-v2/config"
|
|
|
|
"github.com/aws/aws-sdk-go-v2/credentials"
|
|
|
|
"github.com/aws/aws-sdk-go-v2/credentials/stscreds"
|
|
|
|
"github.com/aws/aws-sdk-go-v2/feature/s3/manager"
|
|
|
|
"github.com/aws/aws-sdk-go-v2/service/s3"
|
|
|
|
"github.com/aws/aws-sdk-go-v2/service/s3/types"
|
|
|
|
"github.com/aws/aws-sdk-go-v2/service/sts"
|
2020-05-06 17:36:34 +00:00
|
|
|
"github.com/eikenb/pipeat"
|
2021-02-11 18:45:52 +00:00
|
|
|
"github.com/pkg/sftp"
|
2020-05-06 17:36:34 +00:00
|
|
|
|
2022-07-24 14:18:54 +00:00
|
|
|
"github.com/drakkan/sftpgo/v2/internal/logger"
|
|
|
|
"github.com/drakkan/sftpgo/v2/internal/metric"
|
|
|
|
"github.com/drakkan/sftpgo/v2/internal/plugin"
|
|
|
|
"github.com/drakkan/sftpgo/v2/internal/util"
|
|
|
|
"github.com/drakkan/sftpgo/v2/internal/version"
|
2020-01-19 06:41:05 +00:00
|
|
|
)
|
|
|
|
|
2022-03-15 18:16:50 +00:00
|
|
|
const (
|
|
|
|
// using this mime type for directories improves compatibility with s3fs-fuse
|
|
|
|
s3DirMimeType = "application/x-directory"
|
|
|
|
s3TransferBufferSize = 256 * 1024
|
|
|
|
)
|
2021-06-08 11:52:36 +00:00
|
|
|
|
2022-11-16 20:38:27 +00:00
|
|
|
var (
|
|
|
|
s3DirMimeTypes = []string{s3DirMimeType, "httpd/unix-directory"}
|
|
|
|
)
|
|
|
|
|
2020-10-26 18:01:17 +00:00
|
|
|
// S3Fs is a Fs implementation for AWS S3 compatible object storages
|
2020-01-19 06:41:05 +00:00
|
|
|
type S3Fs struct {
|
2021-03-21 18:15:47 +00:00
|
|
|
connectionID string
|
|
|
|
localTempDir string
|
|
|
|
// if not empty this fs is mouted as virtual folder in the specified path
|
2022-03-15 18:16:50 +00:00
|
|
|
mountPath string
|
|
|
|
config *S3FsConfig
|
|
|
|
svc *s3.Client
|
|
|
|
ctxTimeout time.Duration
|
2020-01-19 06:41:05 +00:00
|
|
|
}
|
|
|
|
|
2020-05-23 09:58:05 +00:00
|
|
|
func init() {
|
2020-06-19 15:08:51 +00:00
|
|
|
version.AddFeature("+s3")
|
2020-05-23 09:58:05 +00:00
|
|
|
}
|
|
|
|
|
2020-01-19 06:41:05 +00:00
|
|
|
// NewS3Fs returns an S3Fs object that allows to interact with an s3 compatible
|
|
|
|
// object storage
|
2022-03-15 18:16:50 +00:00
|
|
|
func NewS3Fs(connectionID, localTempDir, mountPath string, s3Config S3FsConfig) (Fs, error) {
|
2021-03-21 18:15:47 +00:00
|
|
|
if localTempDir == "" {
|
2021-05-27 13:38:27 +00:00
|
|
|
if tempPath != "" {
|
|
|
|
localTempDir = tempPath
|
|
|
|
} else {
|
|
|
|
localTempDir = filepath.Clean(os.TempDir())
|
|
|
|
}
|
2021-03-21 18:15:47 +00:00
|
|
|
}
|
2020-11-02 18:16:12 +00:00
|
|
|
fs := &S3Fs{
|
2022-03-15 18:16:50 +00:00
|
|
|
connectionID: connectionID,
|
|
|
|
localTempDir: localTempDir,
|
2022-04-02 16:32:46 +00:00
|
|
|
mountPath: getMountPath(mountPath),
|
2022-03-15 18:16:50 +00:00
|
|
|
config: &s3Config,
|
|
|
|
ctxTimeout: 30 * time.Second,
|
2020-01-19 06:41:05 +00:00
|
|
|
}
|
2022-04-28 10:55:01 +00:00
|
|
|
if err := fs.config.validate(); err != nil {
|
2020-01-19 06:41:05 +00:00
|
|
|
return fs, err
|
|
|
|
}
|
2022-03-15 18:16:50 +00:00
|
|
|
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
|
|
|
|
defer cancel()
|
2020-02-15 21:06:19 +00:00
|
|
|
|
2022-03-15 18:16:50 +00:00
|
|
|
awsConfig, err := config.LoadDefaultConfig(ctx, config.WithHTTPClient(getAWSHTTPClient(0, 30*time.Second)))
|
|
|
|
if err != nil {
|
|
|
|
return fs, fmt.Errorf("unable to get AWS config: %w", err)
|
|
|
|
}
|
2020-10-25 07:18:48 +00:00
|
|
|
if fs.config.Region != "" {
|
2022-03-15 18:16:50 +00:00
|
|
|
awsConfig.Region = fs.config.Region
|
2020-01-19 06:41:05 +00:00
|
|
|
}
|
2020-11-30 20:46:34 +00:00
|
|
|
if !fs.config.AccessSecret.IsEmpty() {
|
2021-03-27 18:10:27 +00:00
|
|
|
if err := fs.config.AccessSecret.TryDecrypt(); err != nil {
|
|
|
|
return fs, err
|
2020-02-15 21:06:19 +00:00
|
|
|
}
|
2022-03-15 18:16:50 +00:00
|
|
|
awsConfig.Credentials = aws.NewCredentialsCache(
|
2022-03-27 14:32:21 +00:00
|
|
|
credentials.NewStaticCredentialsProvider(fs.config.AccessKey, fs.config.AccessSecret.GetPayload(), ""))
|
2020-01-19 06:41:05 +00:00
|
|
|
}
|
2020-10-25 07:18:48 +00:00
|
|
|
if fs.config.Endpoint != "" {
|
2022-05-19 17:49:51 +00:00
|
|
|
endpointResolver := aws.EndpointResolverWithOptionsFunc(func(service, region string, options ...any) (aws.Endpoint, error) {
|
2022-03-15 18:16:50 +00:00
|
|
|
return aws.Endpoint{
|
|
|
|
URL: fs.config.Endpoint,
|
|
|
|
HostnameImmutable: fs.config.ForcePathStyle,
|
|
|
|
PartitionID: "aws",
|
|
|
|
SigningRegion: fs.config.Region,
|
|
|
|
Source: aws.EndpointSourceCustom,
|
|
|
|
}, nil
|
|
|
|
})
|
|
|
|
awsConfig.EndpointResolverWithOptions = endpointResolver
|
2020-01-19 06:41:05 +00:00
|
|
|
}
|
2022-03-15 18:16:50 +00:00
|
|
|
|
2022-02-24 15:11:35 +00:00
|
|
|
fs.setConfigDefaults()
|
2020-03-13 07:54:36 +00:00
|
|
|
|
2022-02-28 19:19:13 +00:00
|
|
|
if fs.config.RoleARN != "" {
|
2022-03-15 18:16:50 +00:00
|
|
|
client := sts.NewFromConfig(awsConfig)
|
|
|
|
creds := stscreds.NewAssumeRoleProvider(client, fs.config.RoleARN)
|
|
|
|
awsConfig.Credentials = creds
|
2022-02-28 19:19:13 +00:00
|
|
|
}
|
2022-03-15 18:16:50 +00:00
|
|
|
fs.svc = s3.NewFromConfig(awsConfig, func(o *s3.Options) {
|
|
|
|
o.UsePathStyle = fs.config.ForcePathStyle
|
|
|
|
})
|
2020-01-19 06:41:05 +00:00
|
|
|
return fs, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Name returns the name for the Fs implementation
|
2020-11-02 18:16:12 +00:00
|
|
|
func (fs *S3Fs) Name() string {
|
2022-10-10 17:34:15 +00:00
|
|
|
return fmt.Sprintf("%s bucket %q", s3fsName, fs.config.Bucket)
|
2020-01-19 06:41:05 +00:00
|
|
|
}
|
|
|
|
|
2020-10-25 07:18:48 +00:00
|
|
|
// ConnectionID returns the connection ID associated to this Fs implementation
|
2020-11-02 18:16:12 +00:00
|
|
|
func (fs *S3Fs) ConnectionID() string {
|
2020-01-19 06:41:05 +00:00
|
|
|
return fs.connectionID
|
|
|
|
}
|
|
|
|
|
|
|
|
// Stat returns a FileInfo describing the named file
|
2020-11-02 18:16:12 +00:00
|
|
|
func (fs *S3Fs) Stat(name string) (os.FileInfo, error) {
|
2021-01-23 09:32:15 +00:00
|
|
|
var result *FileInfo
|
2022-03-15 18:16:50 +00:00
|
|
|
if name == "" || name == "/" || name == "." {
|
2022-08-16 15:59:13 +00:00
|
|
|
return updateFileInfoModTime(fs.getStorageID(), name, NewFileInfo(name, true, 0, time.Unix(0, 0), false))
|
2020-01-19 06:41:05 +00:00
|
|
|
}
|
2022-03-15 18:16:50 +00:00
|
|
|
if fs.config.KeyPrefix == name+"/" {
|
2022-08-16 15:59:13 +00:00
|
|
|
return updateFileInfoModTime(fs.getStorageID(), name, NewFileInfo(name, true, 0, time.Unix(0, 0), false))
|
2020-01-19 22:23:09 +00:00
|
|
|
}
|
2020-11-02 18:16:12 +00:00
|
|
|
obj, err := fs.headObject(name)
|
|
|
|
if err == nil {
|
2022-11-16 20:38:27 +00:00
|
|
|
// Some S3 providers (like SeaweedFS) remove the trailing '/' from object keys.
|
|
|
|
// So we check some common content types to detect if this is a "directory".
|
|
|
|
isDir := util.Contains(s3DirMimeTypes, util.GetStringFromPointer(obj.ContentType))
|
2022-11-12 22:18:40 +00:00
|
|
|
return updateFileInfoModTime(fs.getStorageID(), name, NewFileInfo(name, isDir, obj.ContentLength,
|
2022-03-15 18:16:50 +00:00
|
|
|
util.GetTimeFromPointer(obj.LastModified), false))
|
2020-11-02 18:16:12 +00:00
|
|
|
}
|
|
|
|
if !fs.IsNotExist(err) {
|
|
|
|
return result, err
|
|
|
|
}
|
|
|
|
// now check if this is a prefix (virtual directory)
|
|
|
|
hasContents, err := fs.hasContents(name)
|
|
|
|
if err == nil && hasContents {
|
2022-08-16 15:59:13 +00:00
|
|
|
return updateFileInfoModTime(fs.getStorageID(), name, NewFileInfo(name, true, 0, time.Unix(0, 0), false))
|
2020-11-02 18:16:12 +00:00
|
|
|
} else if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2021-01-14 11:01:25 +00:00
|
|
|
// the requested file may still be a directory as a zero bytes key
|
|
|
|
// with a trailing forward slash (created using mkdir).
|
|
|
|
// S3 doesn't return content type when listing objects, so we have
|
|
|
|
// create "dirs" adding a trailing "/" to the key
|
|
|
|
return fs.getStatForDir(name)
|
2020-11-02 18:16:12 +00:00
|
|
|
}
|
|
|
|
|
2021-01-14 11:01:25 +00:00
|
|
|
func (fs *S3Fs) getStatForDir(name string) (os.FileInfo, error) {
|
2021-01-23 09:32:15 +00:00
|
|
|
var result *FileInfo
|
2021-01-14 11:01:25 +00:00
|
|
|
obj, err := fs.headObject(name + "/")
|
|
|
|
if err != nil {
|
|
|
|
return result, err
|
2020-01-19 06:41:05 +00:00
|
|
|
}
|
2022-03-15 18:16:50 +00:00
|
|
|
return updateFileInfoModTime(fs.getStorageID(), name, NewFileInfo(name, true, obj.ContentLength,
|
|
|
|
util.GetTimeFromPointer(obj.LastModified), false))
|
2020-01-19 06:41:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Lstat returns a FileInfo describing the named file
|
2020-11-02 18:16:12 +00:00
|
|
|
func (fs *S3Fs) Lstat(name string) (os.FileInfo, error) {
|
2020-01-19 06:41:05 +00:00
|
|
|
return fs.Stat(name)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Open opens the named file for reading
|
2020-11-17 18:36:39 +00:00
|
|
|
func (fs *S3Fs) Open(name string, offset int64) (File, *pipeat.PipeReaderAt, func(), error) {
|
2020-08-11 21:56:10 +00:00
|
|
|
r, w, err := pipeat.PipeInDir(fs.localTempDir)
|
2020-01-19 06:41:05 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, nil, nil, err
|
|
|
|
}
|
|
|
|
ctx, cancelFn := context.WithCancel(context.Background())
|
2022-03-15 18:16:50 +00:00
|
|
|
downloader := manager.NewDownloader(fs.svc, func(d *manager.Downloader) {
|
|
|
|
d.Concurrency = fs.config.DownloadConcurrency
|
|
|
|
d.PartSize = fs.config.DownloadPartSize
|
|
|
|
if offset == 0 && fs.config.DownloadPartMaxTime > 0 {
|
|
|
|
d.ClientOptions = append(d.ClientOptions, func(o *s3.Options) {
|
|
|
|
o.HTTPClient = getAWSHTTPClient(fs.config.DownloadPartMaxTime, 100*time.Millisecond)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
2020-08-03 16:03:09 +00:00
|
|
|
var streamRange *string
|
|
|
|
if offset > 0 {
|
|
|
|
streamRange = aws.String(fmt.Sprintf("bytes=%v-", offset))
|
|
|
|
}
|
|
|
|
|
2020-01-19 06:41:05 +00:00
|
|
|
go func() {
|
|
|
|
defer cancelFn()
|
2022-03-04 17:46:17 +00:00
|
|
|
|
2022-03-15 18:16:50 +00:00
|
|
|
n, err := downloader.Download(ctx, w, &s3.GetObjectInput{
|
2020-01-19 06:41:05 +00:00
|
|
|
Bucket: aws.String(fs.config.Bucket),
|
2020-08-03 16:03:09 +00:00
|
|
|
Key: aws.String(name),
|
|
|
|
Range: streamRange,
|
2020-01-19 06:41:05 +00:00
|
|
|
})
|
2020-08-11 21:56:10 +00:00
|
|
|
w.CloseWithError(err) //nolint:errcheck
|
2022-03-15 18:16:50 +00:00
|
|
|
fsLog(fs, logger.LevelDebug, "download completed, path: %#v size: %v, err: %+v", name, n, err)
|
2021-07-11 13:26:51 +00:00
|
|
|
metric.S3TransferCompleted(n, 1, err)
|
2020-01-19 06:41:05 +00:00
|
|
|
}()
|
|
|
|
return nil, r, cancelFn, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Create creates or opens the named file for writing
|
2020-11-17 18:36:39 +00:00
|
|
|
func (fs *S3Fs) Create(name string, flag int) (File, *PipeWriter, func(), error) {
|
2020-01-19 06:41:05 +00:00
|
|
|
r, w, err := pipeat.PipeInDir(fs.localTempDir)
|
|
|
|
if err != nil {
|
|
|
|
return nil, nil, nil, err
|
|
|
|
}
|
2020-05-19 17:17:43 +00:00
|
|
|
p := NewPipeWriter(w)
|
2020-01-19 06:41:05 +00:00
|
|
|
ctx, cancelFn := context.WithCancel(context.Background())
|
2022-03-15 18:16:50 +00:00
|
|
|
uploader := manager.NewUploader(fs.svc, func(u *manager.Uploader) {
|
|
|
|
u.Concurrency = fs.config.UploadConcurrency
|
|
|
|
u.PartSize = fs.config.UploadPartSize
|
|
|
|
if fs.config.UploadPartMaxTime > 0 {
|
|
|
|
u.ClientOptions = append(u.ClientOptions, func(o *s3.Options) {
|
|
|
|
o.HTTPClient = getAWSHTTPClient(fs.config.UploadPartMaxTime, 100*time.Millisecond)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
2020-01-19 06:41:05 +00:00
|
|
|
go func() {
|
|
|
|
defer cancelFn()
|
2022-03-04 17:46:17 +00:00
|
|
|
|
2020-10-25 07:18:48 +00:00
|
|
|
var contentType string
|
|
|
|
if flag == -1 {
|
2021-06-08 11:52:36 +00:00
|
|
|
contentType = s3DirMimeType
|
2020-10-25 07:18:48 +00:00
|
|
|
} else {
|
|
|
|
contentType = mime.TypeByExtension(path.Ext(name))
|
|
|
|
}
|
2022-03-15 18:16:50 +00:00
|
|
|
_, err := uploader.Upload(ctx, &s3.PutObjectInput{
|
2020-11-02 18:16:12 +00:00
|
|
|
Bucket: aws.String(fs.config.Bucket),
|
2022-03-15 18:16:50 +00:00
|
|
|
Key: aws.String(name),
|
2020-11-02 18:16:12 +00:00
|
|
|
Body: r,
|
2022-03-15 18:16:50 +00:00
|
|
|
ACL: types.ObjectCannedACL(fs.config.ACL),
|
|
|
|
StorageClass: types.StorageClass(fs.config.StorageClass),
|
2021-07-11 13:26:51 +00:00
|
|
|
ContentType: util.NilIfEmpty(contentType),
|
2020-01-19 06:41:05 +00:00
|
|
|
})
|
2020-08-11 21:56:10 +00:00
|
|
|
r.CloseWithError(err) //nolint:errcheck
|
2020-07-24 21:39:38 +00:00
|
|
|
p.Done(err)
|
2022-03-15 18:16:50 +00:00
|
|
|
fsLog(fs, logger.LevelDebug, "upload completed, path: %#v, acl: %#v, readed bytes: %v, err: %+v",
|
|
|
|
name, fs.config.ACL, r.GetReadedBytes(), err)
|
2021-07-11 13:26:51 +00:00
|
|
|
metric.S3TransferCompleted(r.GetReadedBytes(), 0, err)
|
2020-01-19 06:41:05 +00:00
|
|
|
}()
|
2020-05-19 17:17:43 +00:00
|
|
|
return nil, p, cancelFn, nil
|
2020-01-19 06:41:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Rename renames (moves) source to target.
|
|
|
|
// We don't support renaming non empty directories since we should
|
|
|
|
// rename all the contents too and this could take long time: think
|
|
|
|
// about directories with thousands of files, for each file we should
|
|
|
|
// execute a CopyObject call.
|
2020-11-02 18:16:12 +00:00
|
|
|
func (fs *S3Fs) Rename(source, target string) error {
|
2020-01-19 06:41:05 +00:00
|
|
|
if source == target {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
fi, err := fs.Stat(source)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if fi.IsDir() {
|
2020-11-02 18:16:12 +00:00
|
|
|
hasContents, err := fs.hasContents(source)
|
2020-01-19 06:41:05 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2020-11-02 18:16:12 +00:00
|
|
|
if hasContents {
|
2022-11-20 12:06:58 +00:00
|
|
|
return fmt.Errorf("cannot rename non empty directory: %q", source)
|
2020-01-19 06:41:05 +00:00
|
|
|
}
|
2022-11-20 12:06:58 +00:00
|
|
|
if err := fs.mkdirInternal(target); err != nil {
|
|
|
|
return err
|
2020-01-19 06:41:05 +00:00
|
|
|
}
|
2020-11-02 18:16:12 +00:00
|
|
|
} else {
|
2022-11-20 12:06:58 +00:00
|
|
|
contentType := mime.TypeByExtension(path.Ext(source))
|
|
|
|
copySource := pathEscape(fs.Join(fs.config.Bucket, source))
|
2022-03-04 17:46:17 +00:00
|
|
|
|
2022-11-20 12:06:58 +00:00
|
|
|
if fi.Size() > 500*1024*1024 {
|
|
|
|
fsLog(fs, logger.LevelDebug, "renaming file %q with size %d using multipart copy",
|
|
|
|
source, fi.Size())
|
|
|
|
err = fs.doMultipartCopy(copySource, target, contentType, fi.Size())
|
|
|
|
} else {
|
|
|
|
ctx, cancelFn := context.WithDeadline(context.Background(), time.Now().Add(fs.ctxTimeout))
|
|
|
|
defer cancelFn()
|
|
|
|
|
|
|
|
_, err = fs.svc.CopyObject(ctx, &s3.CopyObjectInput{
|
|
|
|
Bucket: aws.String(fs.config.Bucket),
|
|
|
|
CopySource: aws.String(copySource),
|
|
|
|
Key: aws.String(target),
|
|
|
|
StorageClass: types.StorageClass(fs.config.StorageClass),
|
|
|
|
ACL: types.ObjectCannedACL(fs.config.ACL),
|
|
|
|
ContentType: util.NilIfEmpty(contentType),
|
|
|
|
})
|
|
|
|
}
|
|
|
|
if err != nil {
|
|
|
|
metric.S3CopyObjectCompleted(err)
|
|
|
|
return err
|
|
|
|
}
|
2022-03-15 18:16:50 +00:00
|
|
|
|
2022-11-20 12:06:58 +00:00
|
|
|
waiter := s3.NewObjectExistsWaiter(fs.svc)
|
|
|
|
err = waiter.Wait(context.Background(), &s3.HeadObjectInput{
|
|
|
|
Bucket: aws.String(fs.config.Bucket),
|
|
|
|
Key: aws.String(target),
|
|
|
|
}, 10*time.Second)
|
2021-07-23 09:41:31 +00:00
|
|
|
metric.S3CopyObjectCompleted(err)
|
2022-11-20 12:06:58 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if plugin.Handler.HasMetadater() {
|
2021-12-16 17:18:36 +00:00
|
|
|
err = plugin.Handler.SetModificationTime(fs.getStorageID(), ensureAbsPath(target),
|
|
|
|
util.GetTimeAsMsSinceEpoch(fi.ModTime()))
|
|
|
|
if err != nil {
|
2022-03-15 18:16:50 +00:00
|
|
|
fsLog(fs, logger.LevelWarn, "unable to preserve modification time after renaming %#v -> %#v: %+v",
|
2021-12-16 17:18:36 +00:00
|
|
|
source, target, err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2020-01-19 06:41:05 +00:00
|
|
|
return fs.Remove(source, fi.IsDir())
|
|
|
|
}
|
|
|
|
|
|
|
|
// Remove removes the named file or (empty) directory.
|
2020-11-02 18:16:12 +00:00
|
|
|
func (fs *S3Fs) Remove(name string, isDir bool) error {
|
2020-01-19 06:41:05 +00:00
|
|
|
if isDir {
|
2020-11-02 18:16:12 +00:00
|
|
|
hasContents, err := fs.hasContents(name)
|
2020-01-19 06:41:05 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2020-11-02 18:16:12 +00:00
|
|
|
if hasContents {
|
2021-03-21 18:15:47 +00:00
|
|
|
return fmt.Errorf("cannot remove non empty directory: %#v", name)
|
2020-01-19 06:41:05 +00:00
|
|
|
}
|
|
|
|
if !strings.HasSuffix(name, "/") {
|
|
|
|
name += "/"
|
|
|
|
}
|
|
|
|
}
|
|
|
|
ctx, cancelFn := context.WithDeadline(context.Background(), time.Now().Add(fs.ctxTimeout))
|
|
|
|
defer cancelFn()
|
2022-03-04 17:46:17 +00:00
|
|
|
|
2022-03-15 18:16:50 +00:00
|
|
|
_, err := fs.svc.DeleteObject(ctx, &s3.DeleteObjectInput{
|
2020-01-19 06:41:05 +00:00
|
|
|
Bucket: aws.String(fs.config.Bucket),
|
|
|
|
Key: aws.String(name),
|
|
|
|
})
|
2021-07-11 13:26:51 +00:00
|
|
|
metric.S3DeleteObjectCompleted(err)
|
2021-12-16 17:18:36 +00:00
|
|
|
if plugin.Handler.HasMetadater() && err == nil && !isDir {
|
|
|
|
if errMetadata := plugin.Handler.RemoveMetadata(fs.getStorageID(), ensureAbsPath(name)); errMetadata != nil {
|
2022-03-15 18:16:50 +00:00
|
|
|
fsLog(fs, logger.LevelWarn, "unable to remove metadata for path %#v: %+v", name, errMetadata)
|
2021-12-16 17:18:36 +00:00
|
|
|
}
|
|
|
|
}
|
2020-01-19 06:41:05 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Mkdir creates a new directory with the specified name and default permissions
|
2020-11-02 18:16:12 +00:00
|
|
|
func (fs *S3Fs) Mkdir(name string) error {
|
2020-01-19 06:41:05 +00:00
|
|
|
_, err := fs.Stat(name)
|
|
|
|
if !fs.IsNotExist(err) {
|
|
|
|
return err
|
|
|
|
}
|
2022-11-20 12:06:58 +00:00
|
|
|
return fs.mkdirInternal(name)
|
2020-01-19 06:41:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Symlink creates source as a symbolic link to target.
|
2020-11-02 18:16:12 +00:00
|
|
|
func (*S3Fs) Symlink(source, target string) error {
|
2020-11-12 09:39:46 +00:00
|
|
|
return ErrVfsUnsupported
|
2020-01-19 06:41:05 +00:00
|
|
|
}
|
|
|
|
|
2020-08-22 12:52:17 +00:00
|
|
|
// Readlink returns the destination of the named symbolic link
|
2020-11-02 18:16:12 +00:00
|
|
|
func (*S3Fs) Readlink(name string) (string, error) {
|
2020-11-12 09:39:46 +00:00
|
|
|
return "", ErrVfsUnsupported
|
2020-08-22 12:52:17 +00:00
|
|
|
}
|
|
|
|
|
2020-01-19 06:41:05 +00:00
|
|
|
// Chown changes the numeric uid and gid of the named file.
|
2020-11-02 18:16:12 +00:00
|
|
|
func (*S3Fs) Chown(name string, uid int, gid int) error {
|
2020-11-12 09:39:46 +00:00
|
|
|
return ErrVfsUnsupported
|
2020-01-19 06:41:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Chmod changes the mode of the named file to mode.
|
2020-11-02 18:16:12 +00:00
|
|
|
func (*S3Fs) Chmod(name string, mode os.FileMode) error {
|
2020-11-12 09:39:46 +00:00
|
|
|
return ErrVfsUnsupported
|
2020-01-19 06:41:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Chtimes changes the access and modification times of the named file.
|
2021-12-16 17:18:36 +00:00
|
|
|
func (fs *S3Fs) Chtimes(name string, atime, mtime time.Time, isUploading bool) error {
|
|
|
|
if !plugin.Handler.HasMetadater() {
|
|
|
|
return ErrVfsUnsupported
|
|
|
|
}
|
|
|
|
if !isUploading {
|
|
|
|
info, err := fs.Stat(name)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if info.IsDir() {
|
|
|
|
return ErrVfsUnsupported
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return plugin.Handler.SetModificationTime(fs.getStorageID(), ensureAbsPath(name),
|
|
|
|
util.GetTimeAsMsSinceEpoch(mtime))
|
2020-01-19 06:41:05 +00:00
|
|
|
}
|
|
|
|
|
2020-08-20 11:54:36 +00:00
|
|
|
// Truncate changes the size of the named file.
|
|
|
|
// Truncate by path is not supported, while truncating an opened
|
|
|
|
// file is handled inside base transfer
|
2020-11-02 18:16:12 +00:00
|
|
|
func (*S3Fs) Truncate(name string, size int64) error {
|
2020-11-12 09:39:46 +00:00
|
|
|
return ErrVfsUnsupported
|
2020-08-20 11:54:36 +00:00
|
|
|
}
|
|
|
|
|
2020-01-19 06:41:05 +00:00
|
|
|
// ReadDir reads the directory named by dirname and returns
|
|
|
|
// a list of directory entries.
|
2020-11-02 18:16:12 +00:00
|
|
|
func (fs *S3Fs) ReadDir(dirname string) ([]os.FileInfo, error) {
|
2020-01-19 06:41:05 +00:00
|
|
|
var result []os.FileInfo
|
2020-02-19 08:41:15 +00:00
|
|
|
// dirname must be already cleaned
|
2022-03-15 18:16:50 +00:00
|
|
|
prefix := fs.getPrefix(dirname)
|
2020-11-02 18:16:12 +00:00
|
|
|
|
2021-12-16 17:18:36 +00:00
|
|
|
modTimes, err := getFolderModTimes(fs.getStorageID(), dirname)
|
|
|
|
if err != nil {
|
|
|
|
return result, err
|
|
|
|
}
|
2020-11-02 18:16:12 +00:00
|
|
|
prefixes := make(map[string]bool)
|
2022-03-04 17:46:17 +00:00
|
|
|
|
2022-03-15 18:16:50 +00:00
|
|
|
paginator := s3.NewListObjectsV2Paginator(fs.svc, &s3.ListObjectsV2Input{
|
2020-01-19 06:41:05 +00:00
|
|
|
Bucket: aws.String(fs.config.Bucket),
|
|
|
|
Prefix: aws.String(prefix),
|
|
|
|
Delimiter: aws.String("/"),
|
2022-03-15 18:16:50 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
for paginator.HasMorePages() {
|
|
|
|
ctx, cancelFn := context.WithDeadline(context.Background(), time.Now().Add(fs.ctxTimeout))
|
|
|
|
defer cancelFn()
|
|
|
|
|
|
|
|
page, err := paginator.NextPage(ctx)
|
|
|
|
if err != nil {
|
|
|
|
metric.S3ListObjectsCompleted(err)
|
|
|
|
return result, err
|
|
|
|
}
|
2020-01-19 06:41:05 +00:00
|
|
|
for _, p := range page.CommonPrefixes {
|
2020-11-02 18:16:12 +00:00
|
|
|
// prefixes have a trailing slash
|
|
|
|
name, _ := fs.resolve(p.Prefix, prefix)
|
|
|
|
if name == "" {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
if _, ok := prefixes[name]; ok {
|
|
|
|
continue
|
|
|
|
}
|
2022-08-16 15:59:13 +00:00
|
|
|
result = append(result, NewFileInfo(name, true, 0, time.Unix(0, 0), false))
|
2020-11-02 18:16:12 +00:00
|
|
|
prefixes[name] = true
|
2020-01-19 06:41:05 +00:00
|
|
|
}
|
|
|
|
for _, fileObject := range page.Contents {
|
2022-03-15 18:16:50 +00:00
|
|
|
objectModTime := util.GetTimeFromPointer(fileObject.LastModified)
|
2020-01-19 06:41:05 +00:00
|
|
|
name, isDir := fs.resolve(fileObject.Key, prefix)
|
2022-03-15 18:16:50 +00:00
|
|
|
if name == "" || name == "/" {
|
2020-01-19 06:41:05 +00:00
|
|
|
continue
|
|
|
|
}
|
2020-11-02 18:16:12 +00:00
|
|
|
if isDir {
|
|
|
|
if _, ok := prefixes[name]; ok {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
prefixes[name] = true
|
|
|
|
}
|
2021-12-16 17:18:36 +00:00
|
|
|
if t, ok := modTimes[name]; ok {
|
|
|
|
objectModTime = util.GetTimeFromMsecSinceEpoch(t)
|
|
|
|
}
|
2022-03-15 18:16:50 +00:00
|
|
|
result = append(result, NewFileInfo(name, (isDir && fileObject.Size == 0), fileObject.Size,
|
|
|
|
objectModTime, false))
|
2020-01-19 06:41:05 +00:00
|
|
|
}
|
2022-03-15 18:16:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
metric.S3ListObjectsCompleted(nil)
|
|
|
|
return result, nil
|
2020-01-19 06:41:05 +00:00
|
|
|
}
|
|
|
|
|
2021-04-03 14:00:55 +00:00
|
|
|
// IsUploadResumeSupported returns true if resuming uploads is supported.
|
|
|
|
// Resuming uploads is not supported on S3
|
2020-11-02 18:16:12 +00:00
|
|
|
func (*S3Fs) IsUploadResumeSupported() bool {
|
2020-01-19 06:41:05 +00:00
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
// IsAtomicUploadSupported returns true if atomic upload is supported.
|
|
|
|
// S3 uploads are already atomic, we don't need to upload to a temporary
|
|
|
|
// file
|
2020-11-02 18:16:12 +00:00
|
|
|
func (*S3Fs) IsAtomicUploadSupported() bool {
|
2020-01-19 06:41:05 +00:00
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
// IsNotExist returns a boolean indicating whether the error is known to
|
|
|
|
// report that a file or directory does not exist
|
2020-11-02 18:16:12 +00:00
|
|
|
func (*S3Fs) IsNotExist(err error) bool {
|
2020-01-19 06:41:05 +00:00
|
|
|
if err == nil {
|
|
|
|
return false
|
|
|
|
}
|
2022-03-15 18:16:50 +00:00
|
|
|
|
|
|
|
var re *awshttp.ResponseError
|
|
|
|
if errors.As(err, &re) {
|
|
|
|
if re.Response != nil {
|
|
|
|
return re.Response.StatusCode == http.StatusNotFound
|
2020-01-19 06:41:05 +00:00
|
|
|
}
|
|
|
|
}
|
2022-03-15 18:16:50 +00:00
|
|
|
return false
|
2020-01-19 06:41:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// IsPermission returns a boolean indicating whether the error is known to
|
|
|
|
// report that permission is denied.
|
2020-11-02 18:16:12 +00:00
|
|
|
func (*S3Fs) IsPermission(err error) bool {
|
2020-01-19 06:41:05 +00:00
|
|
|
if err == nil {
|
|
|
|
return false
|
|
|
|
}
|
2022-03-15 18:16:50 +00:00
|
|
|
|
|
|
|
var re *awshttp.ResponseError
|
|
|
|
if errors.As(err, &re) {
|
|
|
|
if re.Response != nil {
|
|
|
|
return re.Response.StatusCode == http.StatusForbidden ||
|
|
|
|
re.Response.StatusCode == http.StatusUnauthorized
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false
|
2020-01-19 06:41:05 +00:00
|
|
|
}
|
|
|
|
|
2020-11-12 09:39:46 +00:00
|
|
|
// IsNotSupported returns true if the error indicate an unsupported operation
|
|
|
|
func (*S3Fs) IsNotSupported(err error) bool {
|
|
|
|
if err == nil {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
return err == ErrVfsUnsupported
|
|
|
|
}
|
|
|
|
|
2020-08-11 21:56:10 +00:00
|
|
|
// CheckRootPath creates the specified local root directory if it does not exists
|
2020-11-02 18:16:12 +00:00
|
|
|
func (fs *S3Fs) CheckRootPath(username string, uid int, gid int) bool {
|
2020-01-19 06:41:05 +00:00
|
|
|
// we need a local directory for temporary files
|
2021-03-21 18:15:47 +00:00
|
|
|
osFs := NewOsFs(fs.ConnectionID(), fs.localTempDir, "")
|
2020-08-11 21:56:10 +00:00
|
|
|
return osFs.CheckRootPath(username, uid, gid)
|
2020-01-19 06:41:05 +00:00
|
|
|
}
|
|
|
|
|
2020-01-19 12:58:55 +00:00
|
|
|
// ScanRootDirContents returns the number of files contained in the bucket,
|
2020-01-19 06:41:05 +00:00
|
|
|
// and their size
|
2020-11-02 18:16:12 +00:00
|
|
|
func (fs *S3Fs) ScanRootDirContents() (int, int64, error) {
|
2020-01-19 06:41:05 +00:00
|
|
|
numFiles := 0
|
|
|
|
size := int64(0)
|
2022-03-04 17:46:17 +00:00
|
|
|
|
2022-03-15 18:16:50 +00:00
|
|
|
paginator := s3.NewListObjectsV2Paginator(fs.svc, &s3.ListObjectsV2Input{
|
2020-01-19 06:41:05 +00:00
|
|
|
Bucket: aws.String(fs.config.Bucket),
|
2020-01-19 22:23:09 +00:00
|
|
|
Prefix: aws.String(fs.config.KeyPrefix),
|
2022-03-15 18:16:50 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
for paginator.HasMorePages() {
|
|
|
|
ctx, cancelFn := context.WithDeadline(context.Background(), time.Now().Add(fs.ctxTimeout))
|
|
|
|
defer cancelFn()
|
|
|
|
|
|
|
|
page, err := paginator.NextPage(ctx)
|
|
|
|
if err != nil {
|
|
|
|
metric.S3ListObjectsCompleted(err)
|
|
|
|
return numFiles, size, err
|
|
|
|
}
|
2020-01-19 06:41:05 +00:00
|
|
|
for _, fileObject := range page.Contents {
|
2022-03-15 18:16:50 +00:00
|
|
|
isDir := strings.HasSuffix(util.GetStringFromPointer(fileObject.Key), "/")
|
|
|
|
if isDir && fileObject.Size == 0 {
|
2020-10-25 07:18:48 +00:00
|
|
|
continue
|
|
|
|
}
|
2020-01-19 06:41:05 +00:00
|
|
|
numFiles++
|
2022-03-15 18:16:50 +00:00
|
|
|
size += fileObject.Size
|
2022-11-01 11:22:54 +00:00
|
|
|
if numFiles%1000 == 0 {
|
|
|
|
fsLog(fs, logger.LevelDebug, "root dir scan in progress, files: %d, size: %d", numFiles, size)
|
|
|
|
}
|
2020-01-19 06:41:05 +00:00
|
|
|
}
|
2022-03-15 18:16:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
metric.S3ListObjectsCompleted(nil)
|
|
|
|
return numFiles, size, nil
|
2020-01-19 06:41:05 +00:00
|
|
|
}
|
|
|
|
|
2021-12-16 17:18:36 +00:00
|
|
|
func (fs *S3Fs) getFileNamesInPrefix(fsPrefix string) (map[string]bool, error) {
|
|
|
|
fileNames := make(map[string]bool)
|
|
|
|
prefix := ""
|
|
|
|
if fsPrefix != "/" {
|
|
|
|
prefix = strings.TrimPrefix(fsPrefix, "/")
|
|
|
|
}
|
|
|
|
|
2022-03-15 18:16:50 +00:00
|
|
|
paginator := s3.NewListObjectsV2Paginator(fs.svc, &s3.ListObjectsV2Input{
|
2021-12-16 17:18:36 +00:00
|
|
|
Bucket: aws.String(fs.config.Bucket),
|
|
|
|
Prefix: aws.String(prefix),
|
|
|
|
Delimiter: aws.String("/"),
|
2022-03-15 18:16:50 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
for paginator.HasMorePages() {
|
|
|
|
ctx, cancelFn := context.WithDeadline(context.Background(), time.Now().Add(fs.ctxTimeout))
|
|
|
|
defer cancelFn()
|
|
|
|
|
|
|
|
page, err := paginator.NextPage(ctx)
|
|
|
|
if err != nil {
|
|
|
|
metric.S3ListObjectsCompleted(err)
|
|
|
|
if err != nil {
|
|
|
|
fsLog(fs, logger.LevelError, "unable to get content for prefix %#v: %+v", prefix, err)
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return fileNames, err
|
|
|
|
}
|
2021-12-16 17:18:36 +00:00
|
|
|
for _, fileObject := range page.Contents {
|
|
|
|
name, isDir := fs.resolve(fileObject.Key, prefix)
|
|
|
|
if name != "" && !isDir {
|
|
|
|
fileNames[name] = true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2022-03-15 18:16:50 +00:00
|
|
|
|
|
|
|
metric.S3ListObjectsCompleted(nil)
|
|
|
|
return fileNames, nil
|
2021-12-16 17:18:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// CheckMetadata checks the metadata consistency
|
|
|
|
func (fs *S3Fs) CheckMetadata() error {
|
|
|
|
return fsMetadataCheck(fs, fs.getStorageID(), fs.config.KeyPrefix)
|
|
|
|
}
|
|
|
|
|
2020-06-07 21:30:18 +00:00
|
|
|
// GetDirSize returns the number of files and the size for a folder
|
|
|
|
// including any subfolders
|
2020-11-02 18:16:12 +00:00
|
|
|
func (*S3Fs) GetDirSize(dirname string) (int, int64, error) {
|
2020-11-12 09:39:46 +00:00
|
|
|
return 0, 0, ErrVfsUnsupported
|
2020-06-07 21:30:18 +00:00
|
|
|
}
|
|
|
|
|
2020-01-19 06:41:05 +00:00
|
|
|
// GetAtomicUploadPath returns the path to use for an atomic upload.
|
|
|
|
// S3 uploads are already atomic, we never call this method for S3
|
2020-11-02 18:16:12 +00:00
|
|
|
func (*S3Fs) GetAtomicUploadPath(name string) string {
|
2020-01-19 06:41:05 +00:00
|
|
|
return ""
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetRelativePath returns the path for a file relative to the user's home dir.
|
2020-10-25 07:18:48 +00:00
|
|
|
// This is the path as seen by SFTPGo users
|
2020-11-02 18:16:12 +00:00
|
|
|
func (fs *S3Fs) GetRelativePath(name string) string {
|
2020-01-19 22:23:09 +00:00
|
|
|
rel := path.Clean(name)
|
|
|
|
if rel == "." {
|
2020-01-19 06:41:05 +00:00
|
|
|
rel = ""
|
|
|
|
}
|
2020-12-12 09:31:09 +00:00
|
|
|
if !path.IsAbs(rel) {
|
2022-03-15 18:16:50 +00:00
|
|
|
rel = "/" + rel
|
2020-01-19 06:41:05 +00:00
|
|
|
}
|
2020-11-02 18:16:12 +00:00
|
|
|
if fs.config.KeyPrefix != "" {
|
2020-01-19 22:23:09 +00:00
|
|
|
if !strings.HasPrefix(rel, "/"+fs.config.KeyPrefix) {
|
|
|
|
rel = "/"
|
|
|
|
}
|
|
|
|
rel = path.Clean("/" + strings.TrimPrefix(rel, "/"+fs.config.KeyPrefix))
|
|
|
|
}
|
2021-03-21 18:15:47 +00:00
|
|
|
if fs.mountPath != "" {
|
|
|
|
rel = path.Join(fs.mountPath, rel)
|
|
|
|
}
|
2020-01-19 06:41:05 +00:00
|
|
|
return rel
|
|
|
|
}
|
|
|
|
|
2020-06-26 21:38:29 +00:00
|
|
|
// Walk walks the file tree rooted at root, calling walkFn for each file or
|
2020-08-11 21:56:10 +00:00
|
|
|
// directory in the tree, including root. The result are unordered
|
2020-11-02 18:16:12 +00:00
|
|
|
func (fs *S3Fs) Walk(root string, walkFn filepath.WalkFunc) error {
|
2022-03-15 18:16:50 +00:00
|
|
|
prefix := fs.getPrefix(root)
|
2022-03-04 17:46:17 +00:00
|
|
|
|
2022-03-15 18:16:50 +00:00
|
|
|
paginator := s3.NewListObjectsV2Paginator(fs.svc, &s3.ListObjectsV2Input{
|
2020-08-11 21:56:10 +00:00
|
|
|
Bucket: aws.String(fs.config.Bucket),
|
|
|
|
Prefix: aws.String(prefix),
|
2022-03-15 18:16:50 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
for paginator.HasMorePages() {
|
|
|
|
ctx, cancelFn := context.WithDeadline(context.Background(), time.Now().Add(fs.ctxTimeout))
|
|
|
|
defer cancelFn()
|
|
|
|
|
|
|
|
page, err := paginator.NextPage(ctx)
|
|
|
|
if err != nil {
|
|
|
|
metric.S3ListObjectsCompleted(err)
|
2022-08-16 15:59:13 +00:00
|
|
|
walkFn(root, NewFileInfo(root, true, 0, time.Unix(0, 0), false), err) //nolint:errcheck
|
2022-03-15 18:16:50 +00:00
|
|
|
return err
|
|
|
|
}
|
2020-08-11 21:56:10 +00:00
|
|
|
for _, fileObject := range page.Contents {
|
2022-03-15 18:16:50 +00:00
|
|
|
name, isDir := fs.resolve(fileObject.Key, prefix)
|
|
|
|
if name == "" {
|
2020-08-11 21:56:10 +00:00
|
|
|
continue
|
|
|
|
}
|
2022-03-15 18:16:50 +00:00
|
|
|
err := walkFn(util.GetStringFromPointer(fileObject.Key),
|
|
|
|
NewFileInfo(name, isDir, fileObject.Size, util.GetTimeFromPointer(fileObject.LastModified), false), nil)
|
2020-08-11 21:56:10 +00:00
|
|
|
if err != nil {
|
2022-03-15 18:16:50 +00:00
|
|
|
return err
|
2020-08-11 21:56:10 +00:00
|
|
|
}
|
|
|
|
}
|
2022-03-15 18:16:50 +00:00
|
|
|
}
|
2020-08-11 21:56:10 +00:00
|
|
|
|
2022-03-15 18:16:50 +00:00
|
|
|
metric.S3ListObjectsCompleted(nil)
|
2022-08-16 15:59:13 +00:00
|
|
|
walkFn(root, NewFileInfo(root, true, 0, time.Unix(0, 0), false), nil) //nolint:errcheck
|
2022-03-15 18:16:50 +00:00
|
|
|
return nil
|
2020-06-26 21:38:29 +00:00
|
|
|
}
|
|
|
|
|
2020-01-19 06:41:05 +00:00
|
|
|
// Join joins any number of path elements into a single path
|
2020-11-02 18:16:12 +00:00
|
|
|
func (*S3Fs) Join(elem ...string) string {
|
2022-03-15 18:16:50 +00:00
|
|
|
return strings.TrimPrefix(path.Join(elem...), "/")
|
2020-01-19 06:41:05 +00:00
|
|
|
}
|
|
|
|
|
2020-07-31 17:24:57 +00:00
|
|
|
// HasVirtualFolders returns true if folders are emulated
|
2020-11-02 18:16:12 +00:00
|
|
|
func (*S3Fs) HasVirtualFolders() bool {
|
2020-07-31 17:24:57 +00:00
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
2020-10-25 07:18:48 +00:00
|
|
|
// ResolvePath returns the matching filesystem path for the specified virtual path
|
2020-11-02 18:16:12 +00:00
|
|
|
func (fs *S3Fs) ResolvePath(virtualPath string) (string, error) {
|
2021-03-21 18:15:47 +00:00
|
|
|
if fs.mountPath != "" {
|
|
|
|
virtualPath = strings.TrimPrefix(virtualPath, fs.mountPath)
|
|
|
|
}
|
2020-10-25 07:18:48 +00:00
|
|
|
if !path.IsAbs(virtualPath) {
|
|
|
|
virtualPath = path.Clean("/" + virtualPath)
|
2020-01-19 22:23:09 +00:00
|
|
|
}
|
2022-03-15 18:16:50 +00:00
|
|
|
return fs.Join(fs.config.KeyPrefix, strings.TrimPrefix(virtualPath, "/")), nil
|
2020-01-19 06:41:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (fs *S3Fs) resolve(name *string, prefix string) (string, bool) {
|
2022-03-15 18:16:50 +00:00
|
|
|
result := strings.TrimPrefix(util.GetStringFromPointer(name), prefix)
|
2020-01-19 06:41:05 +00:00
|
|
|
isDir := strings.HasSuffix(result, "/")
|
|
|
|
if isDir {
|
|
|
|
result = strings.TrimSuffix(result, "/")
|
|
|
|
}
|
|
|
|
return result, isDir
|
|
|
|
}
|
|
|
|
|
2022-02-24 15:11:35 +00:00
|
|
|
func (fs *S3Fs) setConfigDefaults() {
|
|
|
|
if fs.config.UploadPartSize == 0 {
|
2022-03-15 18:16:50 +00:00
|
|
|
fs.config.UploadPartSize = manager.DefaultUploadPartSize
|
2022-02-24 15:11:35 +00:00
|
|
|
} else {
|
|
|
|
if fs.config.UploadPartSize < 1024*1024 {
|
|
|
|
fs.config.UploadPartSize *= 1024 * 1024
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if fs.config.UploadConcurrency == 0 {
|
2022-03-15 18:16:50 +00:00
|
|
|
fs.config.UploadConcurrency = manager.DefaultUploadConcurrency
|
2022-02-24 15:11:35 +00:00
|
|
|
}
|
|
|
|
if fs.config.DownloadPartSize == 0 {
|
2022-03-15 18:16:50 +00:00
|
|
|
fs.config.DownloadPartSize = manager.DefaultDownloadPartSize
|
2022-02-24 15:11:35 +00:00
|
|
|
} else {
|
|
|
|
if fs.config.DownloadPartSize < 1024*1024 {
|
|
|
|
fs.config.DownloadPartSize *= 1024 * 1024
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if fs.config.DownloadConcurrency == 0 {
|
2022-03-15 18:16:50 +00:00
|
|
|
fs.config.DownloadConcurrency = manager.DefaultDownloadConcurrency
|
2022-02-24 15:11:35 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-11-20 12:06:58 +00:00
|
|
|
func (fs *S3Fs) mkdirInternal(name string) error {
|
|
|
|
if !strings.HasSuffix(name, "/") {
|
|
|
|
name += "/"
|
|
|
|
}
|
|
|
|
_, w, _, err := fs.Create(name, -1)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return w.Close()
|
|
|
|
}
|
|
|
|
|
2020-11-02 18:16:12 +00:00
|
|
|
func (fs *S3Fs) hasContents(name string) (bool, error) {
|
2022-03-15 18:16:50 +00:00
|
|
|
prefix := fs.getPrefix(name)
|
|
|
|
paginator := s3.NewListObjectsV2Paginator(fs.svc, &s3.ListObjectsV2Input{
|
|
|
|
Bucket: aws.String(fs.config.Bucket),
|
|
|
|
Prefix: aws.String(prefix),
|
|
|
|
MaxKeys: 2,
|
|
|
|
})
|
|
|
|
|
|
|
|
if paginator.HasMorePages() {
|
|
|
|
ctx, cancelFn := context.WithDeadline(context.Background(), time.Now().Add(fs.ctxTimeout))
|
|
|
|
defer cancelFn()
|
|
|
|
|
|
|
|
page, err := paginator.NextPage(ctx)
|
|
|
|
metric.S3ListObjectsCompleted(err)
|
|
|
|
if err != nil {
|
|
|
|
return false, err
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, obj := range page.Contents {
|
|
|
|
name, _ := fs.resolve(obj.Key, prefix)
|
|
|
|
if name == "" || name == "/" {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
return true, nil
|
2020-11-02 18:16:12 +00:00
|
|
|
}
|
2022-03-15 18:16:50 +00:00
|
|
|
return false, nil
|
2020-11-02 18:16:12 +00:00
|
|
|
}
|
2022-03-15 18:16:50 +00:00
|
|
|
|
|
|
|
metric.S3ListObjectsCompleted(nil)
|
|
|
|
return false, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (fs *S3Fs) doMultipartCopy(source, target, contentType string, fileSize int64) error {
|
2020-11-02 18:16:12 +00:00
|
|
|
ctx, cancelFn := context.WithDeadline(context.Background(), time.Now().Add(fs.ctxTimeout))
|
|
|
|
defer cancelFn()
|
2022-03-04 17:46:17 +00:00
|
|
|
|
2022-03-15 18:16:50 +00:00
|
|
|
res, err := fs.svc.CreateMultipartUpload(ctx, &s3.CreateMultipartUploadInput{
|
|
|
|
Bucket: aws.String(fs.config.Bucket),
|
|
|
|
Key: aws.String(target),
|
|
|
|
StorageClass: types.StorageClass(fs.config.StorageClass),
|
|
|
|
ACL: types.ObjectCannedACL(fs.config.ACL),
|
|
|
|
ContentType: util.NilIfEmpty(contentType),
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("unable to create multipart copy request: %w", err)
|
|
|
|
}
|
|
|
|
uploadID := util.GetStringFromPointer(res.UploadId)
|
|
|
|
if uploadID == "" {
|
|
|
|
return errors.New("unable to get multipart copy upload ID")
|
|
|
|
}
|
2022-06-30 16:22:58 +00:00
|
|
|
// We use 32 MB part size and copy 10 parts in parallel.
|
|
|
|
// These values are arbitrary. We don't want to start too many goroutines
|
|
|
|
maxPartSize := int64(32 * 1024 * 1024)
|
|
|
|
if fileSize > int64(100*1024*1024*1024) {
|
|
|
|
maxPartSize = int64(500 * 1024 * 1024)
|
|
|
|
}
|
|
|
|
guard := make(chan struct{}, 10)
|
|
|
|
finished := false
|
|
|
|
var completedParts []types.CompletedPart
|
|
|
|
var partMutex sync.Mutex
|
|
|
|
var wg sync.WaitGroup
|
2022-08-30 13:47:41 +00:00
|
|
|
var hasError atomic.Bool
|
2022-06-30 16:22:58 +00:00
|
|
|
var errOnce sync.Once
|
|
|
|
var copyError error
|
|
|
|
var partNumber int32
|
|
|
|
var offset int64
|
|
|
|
|
|
|
|
opCtx, opCancel := context.WithCancel(context.Background())
|
|
|
|
defer opCancel()
|
|
|
|
|
|
|
|
for partNumber = 1; !finished; partNumber++ {
|
|
|
|
start := offset
|
|
|
|
end := offset + maxPartSize
|
|
|
|
if end >= fileSize {
|
|
|
|
end = fileSize
|
|
|
|
finished = true
|
|
|
|
}
|
|
|
|
offset = end
|
|
|
|
|
|
|
|
guard <- struct{}{}
|
2022-08-30 13:47:41 +00:00
|
|
|
if hasError.Load() {
|
2022-06-30 16:22:58 +00:00
|
|
|
fsLog(fs, logger.LevelDebug, "previous multipart copy error, copy for part %d not started", partNumber)
|
|
|
|
break
|
|
|
|
}
|
|
|
|
|
|
|
|
wg.Add(1)
|
|
|
|
go func(partNum int32, partStart, partEnd int64) {
|
|
|
|
defer func() {
|
|
|
|
<-guard
|
|
|
|
wg.Done()
|
|
|
|
}()
|
|
|
|
|
|
|
|
innerCtx, innerCancelFn := context.WithDeadline(opCtx, time.Now().Add(fs.ctxTimeout))
|
|
|
|
defer innerCancelFn()
|
|
|
|
|
|
|
|
partResp, err := fs.svc.UploadPartCopy(innerCtx, &s3.UploadPartCopyInput{
|
|
|
|
Bucket: aws.String(fs.config.Bucket),
|
|
|
|
CopySource: aws.String(source),
|
|
|
|
Key: aws.String(target),
|
|
|
|
PartNumber: partNum,
|
|
|
|
UploadId: aws.String(uploadID),
|
|
|
|
CopySourceRange: aws.String(fmt.Sprintf("bytes=%d-%d", partStart, partEnd-1)),
|
2022-03-15 18:16:50 +00:00
|
|
|
})
|
2022-06-30 16:22:58 +00:00
|
|
|
if err != nil {
|
|
|
|
errOnce.Do(func() {
|
|
|
|
fsLog(fs, logger.LevelError, "unable to copy part number %d: %+v", partNum, err)
|
2022-08-30 13:47:41 +00:00
|
|
|
hasError.Store(true)
|
2022-06-30 16:22:58 +00:00
|
|
|
copyError = fmt.Errorf("error copying part number %d: %w", partNum, err)
|
|
|
|
opCancel()
|
|
|
|
|
|
|
|
abortCtx, abortCancelFn := context.WithDeadline(context.Background(), time.Now().Add(fs.ctxTimeout))
|
|
|
|
defer abortCancelFn()
|
|
|
|
|
|
|
|
_, errAbort := fs.svc.AbortMultipartUpload(abortCtx, &s3.AbortMultipartUploadInput{
|
|
|
|
Bucket: aws.String(fs.config.Bucket),
|
|
|
|
Key: aws.String(target),
|
|
|
|
UploadId: aws.String(uploadID),
|
|
|
|
})
|
|
|
|
if errAbort != nil {
|
|
|
|
fsLog(fs, logger.LevelError, "unable to abort multipart copy: %+v", errAbort)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
return
|
2022-03-15 18:16:50 +00:00
|
|
|
}
|
2022-06-30 16:22:58 +00:00
|
|
|
|
|
|
|
partMutex.Lock()
|
|
|
|
completedParts = append(completedParts, types.CompletedPart{
|
|
|
|
ETag: partResp.CopyPartResult.ETag,
|
|
|
|
PartNumber: partNum,
|
|
|
|
})
|
|
|
|
partMutex.Unlock()
|
|
|
|
}(partNumber, start, end)
|
2022-03-15 18:16:50 +00:00
|
|
|
}
|
|
|
|
|
2022-06-30 16:22:58 +00:00
|
|
|
wg.Wait()
|
|
|
|
close(guard)
|
|
|
|
|
|
|
|
if copyError != nil {
|
|
|
|
return copyError
|
|
|
|
}
|
|
|
|
sort.Slice(completedParts, func(i, j int) bool {
|
|
|
|
return completedParts[i].PartNumber < completedParts[j].PartNumber
|
|
|
|
})
|
|
|
|
|
2022-03-15 18:16:50 +00:00
|
|
|
completeCtx, completeCancelFn := context.WithDeadline(context.Background(), time.Now().Add(fs.ctxTimeout))
|
|
|
|
defer completeCancelFn()
|
|
|
|
|
|
|
|
_, err = fs.svc.CompleteMultipartUpload(completeCtx, &s3.CompleteMultipartUploadInput{
|
|
|
|
Bucket: aws.String(fs.config.Bucket),
|
|
|
|
Key: aws.String(target),
|
|
|
|
UploadId: aws.String(uploadID),
|
|
|
|
MultipartUpload: &types.CompletedMultipartUpload{
|
|
|
|
Parts: completedParts,
|
|
|
|
},
|
2020-11-02 18:16:12 +00:00
|
|
|
})
|
|
|
|
if err != nil {
|
2022-03-15 18:16:50 +00:00
|
|
|
return fmt.Errorf("unable to complete multipart upload: %w", err)
|
2020-11-02 18:16:12 +00:00
|
|
|
}
|
2022-03-15 18:16:50 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (fs *S3Fs) getPrefix(name string) string {
|
|
|
|
prefix := ""
|
|
|
|
if name != "" && name != "." && name != "/" {
|
|
|
|
prefix = strings.TrimPrefix(name, "/")
|
|
|
|
if !strings.HasSuffix(prefix, "/") {
|
|
|
|
prefix += "/"
|
2020-11-23 14:36:42 +00:00
|
|
|
}
|
|
|
|
}
|
2022-03-15 18:16:50 +00:00
|
|
|
return prefix
|
2020-11-02 18:16:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (fs *S3Fs) headObject(name string) (*s3.HeadObjectOutput, error) {
|
2020-08-11 21:56:10 +00:00
|
|
|
ctx, cancelFn := context.WithDeadline(context.Background(), time.Now().Add(fs.ctxTimeout))
|
|
|
|
defer cancelFn()
|
2022-03-04 17:46:17 +00:00
|
|
|
|
2022-03-15 18:16:50 +00:00
|
|
|
obj, err := fs.svc.HeadObject(ctx, &s3.HeadObjectInput{
|
2020-08-11 21:56:10 +00:00
|
|
|
Bucket: aws.String(fs.config.Bucket),
|
|
|
|
Key: aws.String(name),
|
|
|
|
})
|
2021-07-11 13:26:51 +00:00
|
|
|
metric.S3HeadObjectCompleted(err)
|
2020-11-02 18:16:12 +00:00
|
|
|
return obj, err
|
|
|
|
}
|
|
|
|
|
2020-11-04 18:11:40 +00:00
|
|
|
// GetMimeType returns the content type
|
2020-11-02 18:16:12 +00:00
|
|
|
func (fs *S3Fs) GetMimeType(name string) (string, error) {
|
|
|
|
obj, err := fs.headObject(name)
|
2020-08-11 21:56:10 +00:00
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
2022-03-15 18:16:50 +00:00
|
|
|
return util.GetStringFromPointer(obj.ContentType), nil
|
2020-08-11 21:56:10 +00:00
|
|
|
}
|
2020-12-12 09:31:09 +00:00
|
|
|
|
|
|
|
// Close closes the fs
|
|
|
|
func (*S3Fs) Close() error {
|
|
|
|
return nil
|
|
|
|
}
|
2020-12-25 10:14:08 +00:00
|
|
|
|
2022-05-15 05:30:36 +00:00
|
|
|
// GetAvailableDiskSize returns the available size for the specified path
|
2021-02-11 18:45:52 +00:00
|
|
|
func (*S3Fs) GetAvailableDiskSize(dirName string) (*sftp.StatVFS, error) {
|
|
|
|
return nil, ErrStorageSizeUnavailable
|
2020-12-25 10:14:08 +00:00
|
|
|
}
|
2021-07-23 09:41:31 +00:00
|
|
|
|
2021-12-16 17:18:36 +00:00
|
|
|
func (fs *S3Fs) getStorageID() string {
|
|
|
|
if fs.config.Endpoint != "" {
|
|
|
|
if !strings.HasSuffix(fs.config.Endpoint, "/") {
|
|
|
|
return fmt.Sprintf("s3://%v/%v", fs.config.Endpoint, fs.config.Bucket)
|
|
|
|
}
|
|
|
|
return fmt.Sprintf("s3://%v%v", fs.config.Endpoint, fs.config.Bucket)
|
|
|
|
}
|
|
|
|
return fmt.Sprintf("s3://%v", fs.config.Bucket)
|
|
|
|
}
|
|
|
|
|
2022-03-15 18:16:50 +00:00
|
|
|
func getAWSHTTPClient(timeout int, idleConnectionTimeout time.Duration) *awshttp.BuildableClient {
|
|
|
|
c := awshttp.NewBuildableClient().
|
|
|
|
WithDialerOptions(func(d *net.Dialer) {
|
|
|
|
d.Timeout = 8 * time.Second
|
|
|
|
}).
|
|
|
|
WithTransportOptions(func(tr *http.Transport) {
|
|
|
|
tr.IdleConnTimeout = idleConnectionTimeout
|
|
|
|
tr.WriteBufferSize = s3TransferBufferSize
|
|
|
|
tr.ReadBufferSize = s3TransferBufferSize
|
|
|
|
})
|
|
|
|
if timeout > 0 {
|
|
|
|
c = c.WithTimeout(time.Duration(timeout) * time.Second)
|
|
|
|
}
|
|
|
|
return c
|
|
|
|
}
|
|
|
|
|
2021-07-23 09:41:31 +00:00
|
|
|
// ideally we should simply use url.PathEscape:
|
|
|
|
//
|
|
|
|
// https://github.com/awsdocs/aws-doc-sdk-examples/blob/master/go/example_code/s3/s3_copy_object.go#L65
|
|
|
|
//
|
|
|
|
// but this cause issue with some vendors, see #483, the code below is copied from rclone
|
|
|
|
func pathEscape(in string) string {
|
|
|
|
var u url.URL
|
|
|
|
u.Path = in
|
|
|
|
return strings.ReplaceAll(u.String(), "+", "%2B")
|
|
|
|
}
|