2022-09-03 13:38:43 +00:00
|
|
|
|
package s3
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"bytes"
|
|
|
|
|
"context"
|
|
|
|
|
"fmt"
|
2024-08-14 11:34:48 +00:00
|
|
|
|
"github.com/alist-org/alist/v3/server/common"
|
2022-09-03 13:38:43 +00:00
|
|
|
|
"io"
|
|
|
|
|
"net/url"
|
|
|
|
|
stdpath "path"
|
2023-04-09 11:25:52 +00:00
|
|
|
|
"strings"
|
2022-09-03 13:38:43 +00:00
|
|
|
|
"time"
|
|
|
|
|
|
2023-11-06 08:56:55 +00:00
|
|
|
|
"github.com/alist-org/alist/v3/internal/stream"
|
2024-03-27 06:22:26 +00:00
|
|
|
|
"github.com/alist-org/alist/v3/pkg/cron"
|
2023-11-06 08:56:55 +00:00
|
|
|
|
|
2022-09-03 13:38:43 +00:00
|
|
|
|
"github.com/alist-org/alist/v3/internal/driver"
|
|
|
|
|
"github.com/alist-org/alist/v3/internal/model"
|
|
|
|
|
"github.com/aws/aws-sdk-go/aws/session"
|
|
|
|
|
"github.com/aws/aws-sdk-go/service/s3"
|
|
|
|
|
"github.com/aws/aws-sdk-go/service/s3/s3manager"
|
|
|
|
|
log "github.com/sirupsen/logrus"
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
type S3 struct {
|
|
|
|
|
model.Storage
|
|
|
|
|
Addition
|
|
|
|
|
Session *session.Session
|
|
|
|
|
client *s3.S3
|
|
|
|
|
linkClient *s3.S3
|
2024-03-25 14:53:44 +00:00
|
|
|
|
|
|
|
|
|
config driver.Config
|
2024-03-27 06:22:26 +00:00
|
|
|
|
cron *cron.Cron
|
2022-09-03 13:38:43 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (d *S3) Config() driver.Config {
|
2024-03-25 14:53:44 +00:00
|
|
|
|
return d.config
|
2022-09-03 13:38:43 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (d *S3) GetAddition() driver.Additional {
|
2022-12-13 10:03:30 +00:00
|
|
|
|
return &d.Addition
|
2022-09-03 13:38:43 +00:00
|
|
|
|
}
|
|
|
|
|
|
2022-12-13 10:03:30 +00:00
|
|
|
|
func (d *S3) Init(ctx context.Context) error {
|
2022-09-03 13:38:43 +00:00
|
|
|
|
if d.Region == "" {
|
|
|
|
|
d.Region = "alist"
|
|
|
|
|
}
|
2024-03-27 06:22:26 +00:00
|
|
|
|
if d.config.Name == "Doge" {
|
2024-03-29 06:56:49 +00:00
|
|
|
|
// 多吉云每次临时生成的秘钥有效期为 2h,所以这里设置为 118 分钟重新生成一次
|
2024-03-27 06:22:26 +00:00
|
|
|
|
d.cron = cron.NewCron(time.Minute * 118)
|
|
|
|
|
d.cron.Do(func() {
|
|
|
|
|
err := d.initSession()
|
|
|
|
|
if err != nil {
|
|
|
|
|
log.Errorln("Doge init session error:", err)
|
|
|
|
|
}
|
2024-03-29 06:56:49 +00:00
|
|
|
|
d.client = d.getClient(false)
|
|
|
|
|
d.linkClient = d.getClient(true)
|
2024-03-27 06:22:26 +00:00
|
|
|
|
})
|
|
|
|
|
}
|
2022-12-13 10:03:30 +00:00
|
|
|
|
err := d.initSession()
|
2022-09-03 13:38:43 +00:00
|
|
|
|
if err != nil {
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
d.client = d.getClient(false)
|
|
|
|
|
d.linkClient = d.getClient(true)
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (d *S3) Drop(ctx context.Context) error {
|
2024-03-27 06:22:26 +00:00
|
|
|
|
if d.cron != nil {
|
|
|
|
|
d.cron.Stop()
|
|
|
|
|
}
|
2022-09-03 13:38:43 +00:00
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (d *S3) List(ctx context.Context, dir model.Obj, args model.ListArgs) ([]model.Obj, error) {
|
|
|
|
|
if d.ListObjectVersion == "v2" {
|
2023-07-10 06:55:19 +00:00
|
|
|
|
return d.listV2(dir.GetPath(), args)
|
2022-09-03 13:38:43 +00:00
|
|
|
|
}
|
2023-07-10 06:55:19 +00:00
|
|
|
|
return d.listV1(dir.GetPath(), args)
|
2022-09-03 13:38:43 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (d *S3) Link(ctx context.Context, file model.Obj, args model.LinkArgs) (*model.Link, error) {
|
|
|
|
|
path := getKey(file.GetPath(), false)
|
2023-03-14 04:27:42 +00:00
|
|
|
|
filename := stdpath.Base(path)
|
2023-06-06 14:47:27 +00:00
|
|
|
|
disposition := fmt.Sprintf(`attachment; filename*=UTF-8''%s`, url.PathEscape(filename))
|
|
|
|
|
if d.AddFilenameToDisposition {
|
|
|
|
|
disposition = fmt.Sprintf(`attachment; filename="%s"; filename*=UTF-8''%s`, filename, url.PathEscape(filename))
|
|
|
|
|
}
|
2022-09-03 13:38:43 +00:00
|
|
|
|
input := &s3.GetObjectInput{
|
|
|
|
|
Bucket: &d.Bucket,
|
|
|
|
|
Key: &path,
|
|
|
|
|
//ResponseContentDisposition: &disposition,
|
|
|
|
|
}
|
|
|
|
|
if d.CustomHost == "" {
|
|
|
|
|
input.ResponseContentDisposition = &disposition
|
|
|
|
|
}
|
|
|
|
|
req, _ := d.linkClient.GetObjectRequest(input)
|
2024-08-14 11:34:48 +00:00
|
|
|
|
var link model.Link
|
2022-09-03 13:38:43 +00:00
|
|
|
|
var err error
|
|
|
|
|
if d.CustomHost != "" {
|
2024-12-25 13:13:23 +00:00
|
|
|
|
if d.EnableCustomHostPresign {
|
|
|
|
|
link.URL, err = req.Presign(time.Hour * time.Duration(d.SignURLExpire))
|
|
|
|
|
} else {
|
|
|
|
|
err = req.Build()
|
|
|
|
|
link.URL = req.HTTPRequest.URL.String()
|
|
|
|
|
}
|
2023-04-09 11:25:52 +00:00
|
|
|
|
if d.RemoveBucket {
|
2024-08-14 11:34:48 +00:00
|
|
|
|
link.URL = strings.Replace(link.URL, "/"+d.Bucket, "", 1)
|
2023-04-09 11:25:52 +00:00
|
|
|
|
}
|
2022-09-03 13:38:43 +00:00
|
|
|
|
} else {
|
2024-08-14 11:34:48 +00:00
|
|
|
|
if common.ShouldProxy(d, filename) {
|
|
|
|
|
err = req.Sign()
|
|
|
|
|
link.URL = req.HTTPRequest.URL.String()
|
|
|
|
|
link.Header = req.HTTPRequest.Header
|
|
|
|
|
} else {
|
|
|
|
|
link.URL, err = req.Presign(time.Hour * time.Duration(d.SignURLExpire))
|
|
|
|
|
}
|
2022-09-03 13:38:43 +00:00
|
|
|
|
}
|
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
2024-08-14 11:34:48 +00:00
|
|
|
|
return &link, nil
|
2022-09-03 13:38:43 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (d *S3) MakeDir(ctx context.Context, parentDir model.Obj, dirName string) error {
|
|
|
|
|
return d.Put(ctx, &model.Object{
|
|
|
|
|
Path: stdpath.Join(parentDir.GetPath(), dirName),
|
2023-08-27 13:14:23 +00:00
|
|
|
|
}, &stream.FileStream{
|
2022-09-03 13:38:43 +00:00
|
|
|
|
Obj: &model.Object{
|
|
|
|
|
Name: getPlaceholderName(d.Placeholder),
|
|
|
|
|
Modified: time.Now(),
|
|
|
|
|
},
|
2023-08-27 13:14:23 +00:00
|
|
|
|
Reader: io.NopCloser(bytes.NewReader([]byte{})),
|
|
|
|
|
Mimetype: "application/octet-stream",
|
2023-11-06 08:56:55 +00:00
|
|
|
|
}, func(float64) {})
|
2022-09-03 13:38:43 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (d *S3) Move(ctx context.Context, srcObj, dstDir model.Obj) error {
|
|
|
|
|
err := d.Copy(ctx, srcObj, dstDir)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
return d.Remove(ctx, srcObj)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (d *S3) Rename(ctx context.Context, srcObj model.Obj, newName string) error {
|
2022-09-10 05:42:03 +00:00
|
|
|
|
err := d.copy(ctx, srcObj.GetPath(), stdpath.Join(stdpath.Dir(srcObj.GetPath()), newName), srcObj.IsDir())
|
2022-09-03 13:38:43 +00:00
|
|
|
|
if err != nil {
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
return d.Remove(ctx, srcObj)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (d *S3) Copy(ctx context.Context, srcObj, dstDir model.Obj) error {
|
2022-09-10 05:42:03 +00:00
|
|
|
|
return d.copy(ctx, srcObj.GetPath(), stdpath.Join(dstDir.GetPath(), srcObj.GetName()), srcObj.IsDir())
|
2022-09-03 13:38:43 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (d *S3) Remove(ctx context.Context, obj model.Obj) error {
|
2022-09-16 13:25:55 +00:00
|
|
|
|
if obj.IsDir() {
|
|
|
|
|
return d.removeDir(ctx, obj.GetPath())
|
2022-09-03 13:38:43 +00:00
|
|
|
|
}
|
2022-09-16 13:25:55 +00:00
|
|
|
|
return d.removeFile(obj.GetPath())
|
2022-09-03 13:38:43 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (d *S3) Put(ctx context.Context, dstDir model.Obj, stream model.FileStreamer, up driver.UpdateProgress) error {
|
|
|
|
|
uploader := s3manager.NewUploader(d.Session)
|
2023-04-02 08:09:27 +00:00
|
|
|
|
if stream.GetSize() > s3manager.MaxUploadParts*s3manager.DefaultUploadPartSize {
|
|
|
|
|
uploader.PartSize = stream.GetSize() / (s3manager.MaxUploadParts - 1)
|
|
|
|
|
}
|
2022-09-03 13:38:43 +00:00
|
|
|
|
key := getKey(stdpath.Join(dstDir.GetPath(), stream.GetName()), false)
|
2023-05-24 10:02:49 +00:00
|
|
|
|
contentType := stream.GetMimetype()
|
2022-09-03 13:38:43 +00:00
|
|
|
|
log.Debugln("key:", key)
|
|
|
|
|
input := &s3manager.UploadInput{
|
2023-05-24 10:02:49 +00:00
|
|
|
|
Bucket: &d.Bucket,
|
|
|
|
|
Key: &key,
|
|
|
|
|
Body: stream,
|
|
|
|
|
ContentType: &contentType,
|
2022-09-03 13:38:43 +00:00
|
|
|
|
}
|
2022-12-21 07:03:09 +00:00
|
|
|
|
_, err := uploader.UploadWithContext(ctx, input)
|
2022-09-03 13:38:43 +00:00
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var _ driver.Driver = (*S3)(nil)
|