-
Notifications
You must be signed in to change notification settings - Fork 21
Files
/
Copy paths3.go
Latest commit
533 lines (467 loc) · 12.4 KB
1
// DejaVu - Data snapshot and sync.
2
// Copyright (c) 2022-present, b3log.org
3
//
4
// This program is free software: you can redistribute it and/or modify
5
// it under the terms of the GNU Affero General Public License as published by
6
// the Free Software Foundation, either version 3 of the License, or
7
// (at your option) any later version.
8
//
9
// This program is distributed in the hope that it will be useful,
10
// but WITHOUT ANY WARRANTY; without even the implied warranty of
11
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12
// GNU Affero General Public License for more details.
13
//
14
// You should have received a copy of the GNU Affero General Public License
15
// along with this program. If not, see <https://www.gnu.org/licenses/>.
16
17
package cloud
18
19
import (
20
"bytes"
21
"context"
22
"errors"
23
"io"
24
"math"
25
"net/http"
26
"os"
27
"path"
28
"path/filepath"
29
"sort"
30
"strings"
31
"sync"
32
"time"
33
34
"github.com/88250/gulu"
35
"github.com/aws/aws-sdk-go-v2/aws"
36
"github.com/aws/aws-sdk-go-v2/config"
37
"github.com/aws/aws-sdk-go-v2/credentials"
38
as3 "github.com/aws/aws-sdk-go-v2/service/s3"
39
as3Types "github.com/aws/aws-sdk-go-v2/service/s3/types"
40
"github.com/aws/smithy-go"
41
"github.com/panjf2000/ants/v2"
42
"github.com/siyuan-note/dejavu/entity"
43
"github.com/siyuan-note/logging"
44
)
45
46
// S3 描述了 S3 协议兼容的对象存储服务实现。
47
type S3 struct {
48
*BaseCloud
49
HTTPClient *http.Client
50
}
51
52
func NewS3(baseCloud *BaseCloud, httpClient *http.Client) *S3 {
53
return &S3{baseCloud, httpClient}
54
}
55
56
func (s3 *S3) GetRepos() (repos []*Repo, size int64, err error) {
57
repos, err = s3.listRepos()
58
if nil != err {
59
return
60
}
61
62
for _, repo := range repos {
63
size += repo.Size
64
}
65
return
66
}
67
68
func (s3 *S3) UploadObject(filePath string, overwrite bool) (length int64, err error) {
69
svc := s3.getService()
70
ctx, cancelFn := context.WithTimeout(context.Background(), time.Duration(s3.S3.Timeout)*time.Second)
71
defer cancelFn()
72
73
absFilePath := filepath.Join(s3.Conf.RepoPath, filePath)
74
info, err := os.Stat(absFilePath)
75
if nil != err {
76
logging.LogErrorf("stat failed: %s", err)
77
return
78
}
79
length = info.Size()
80
81
file, err := os.Open(absFilePath)
82
if nil != err {
83
return
84
}
85
defer file.Close()
86
key := path.Join("repo", filePath)
87
_, err = svc.PutObject(ctx, &as3.PutObjectInput{
88
Bucket: aws.String(s3.Conf.S3.Bucket),
89
Key: aws.String(key),
90
CacheControl: aws.String("no-cache"),
91
Body: file,
92
})
93
if nil != err {
94
return
95
}
96
97
//logging.LogInfof("uploaded object [%s]", key)
98
return
99
}
100
101
func (s3 *S3) UploadBytes(filePath string, data []byte, overwrite bool) (length int64, err error) {