Skip to content

Files

533 lines (467 loc) · 12.4 KB

s3.go

File metadata and controls

533 lines (467 loc) · 12.4 KB
88250Zuoqiu-Yingyi
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 (
Nov 1, 2024
Nov 1, 2024
20
"bytes"
Dec 20, 2024
Dec 20, 2024
22
"errors"
Apr 21, 2023
Apr 21, 2023
24
"math"
Nov 15, 2022
Nov 15, 2022
25
"net/http"
26
"os"
27
"path"
28
"path/filepath"
29
"sort"
30
"strings"
Nov 16, 2022
Nov 16, 2022
31
"sync"
32
"time"
33
34
"github.com/88250/gulu"
Dec 20, 2024
Dec 20, 2024
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"
Nov 16, 2022
Nov 16, 2022
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
Nov 16, 2022
Nov 16, 2022
49
HTTPClient *http.Client
Nov 16, 2022
Nov 16, 2022
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
Sep 9, 2023
Sep 9, 2023
68
func (s3 *S3) UploadObject(filePath string, overwrite bool) (length int64, err error) {
Dec 4, 2022
Dec 4, 2022
70
ctx, cancelFn := context.WithTimeout(context.Background(), time.Duration(s3.S3.Timeout)*time.Second)
Sep 9, 2023
Sep 9, 2023
72
73
absFilePath := filepath.Join(s3.Conf.RepoPath, filePath)
Sep 9, 2023
Sep 9, 2023
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()
Nov 10, 2023
Nov 10, 2023
86
key := path.Join("repo", filePath)
Dec 20, 2024
Dec 20, 2024
87
_, err = svc.PutObject(ctx, &as3.PutObjectInput{
Nov 12, 2023
Nov 12, 2023
88
Bucket: aws.String(s3.Conf.S3.Bucket),
89
Key: aws.String(key),
90
CacheControl: aws.String("no-cache"),
91
Body: file,
Nov 10, 2023
Nov 10, 2023
93
if nil != err {
94
return
95
}
Nov 10, 2023
Nov 10, 2023
96
Nov 13, 2024
Nov 13, 2024
97
//logging.LogInfof("uploaded object [%s]", key)
Nov 1, 2024
Nov 1, 2024
101
func (s3 *S3) UploadBytes(filePath string, data []byte, overwrite bool) (length int64, err error) {