-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy paths3.go
99 lines (77 loc) · 1.68 KB
/
s3.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
package backend
import (
"bytes"
"context"
"errors"
"io"
"log"
"strconv"
minio "github.com/minio/minio-go/v7"
)
var (
errNoSuchKey = errors.New("The specified key does not exist.") // Minio doesn't export errors
)
type S3Backend struct {
ctx context.Context
client *minio.Client
bucket string
prefix string
size int64
verbose bool
}
func NewS3Backend(
ctx context.Context,
client *minio.Client,
bucket string,
prefix string,
size int64,
verbose bool,
) *S3Backend {
return &S3Backend{
ctx: ctx,
client: client,
bucket: bucket,
prefix: prefix,
size: size,
verbose: verbose,
}
}
func (b *S3Backend) ReadAt(p []byte, off int64) (n int, err error) {
if b.verbose {
log.Printf("ReadAt(len(p) = %v, off = %v)", len(p), off)
}
obj, err := b.client.GetObject(b.ctx, b.bucket, b.prefix+"-"+strconv.FormatInt(off, 10), minio.GetObjectOptions{})
if err != nil {
if err.Error() == errNoSuchKey.Error() {
return len(p), nil
}
return 0, err
}
nn, err := obj.Read(p)
if err != nil && nn == len(p) && err != io.EOF {
if err.Error() == errNoSuchKey.Error() {
return len(p), nil
}
return 0, err
}
return int(nn), nil
}
func (b *S3Backend) WriteAt(p []byte, off int64) (n int, err error) {
if b.verbose {
log.Printf("WriteAt(len(p) = %v, off = %v)", len(p), off)
}
nn, err := b.client.PutObject(b.ctx, b.bucket, b.prefix+"-"+strconv.FormatInt(off, 10), bytes.NewReader(p), int64(len(p)), minio.PutObjectOptions{})
return int(nn.Size), err
}
func (b *S3Backend) Size() (int64, error) {
if b.verbose {
log.Println("Size()")
}
return b.size, nil
}
func (b *S3Backend) Sync() error {
if b.verbose {
log.Println("Sync()")
}
return nil
}