-
Notifications
You must be signed in to change notification settings - Fork 351
/
adapter.go
164 lines (139 loc) · 4.46 KB
/
adapter.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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
package transient
import (
"context"
"crypto/rand"
"crypto/sha256"
"encoding/hex"
"io"
"net/http"
"net/url"
"time"
"github.com/google/uuid"
"github.com/treeverse/lakefs/pkg/block"
)
type Adapter struct{}
func New(_ context.Context) *Adapter {
return &Adapter{}
}
func (a *Adapter) Put(_ context.Context, _ block.ObjectPointer, _ int64, reader io.Reader, _ block.PutOpts) error {
_, err := io.Copy(io.Discard, reader)
return err
}
func (a *Adapter) Get(_ context.Context, _ block.ObjectPointer, expectedSize int64) (io.ReadCloser, error) {
if expectedSize < 0 {
return nil, io.ErrUnexpectedEOF
}
return io.NopCloser(&io.LimitedReader{R: rand.Reader, N: expectedSize}), nil
}
func (a *Adapter) GetWalker(_ *url.URL) (block.Walker, error) {
return nil, block.ErrOperationNotSupported
}
func (a *Adapter) GetPreSignedURL(_ context.Context, _ block.ObjectPointer, _ block.PreSignMode) (string, time.Time, error) {
return "", time.Time{}, block.ErrOperationNotSupported
}
func (a *Adapter) Exists(_ context.Context, _ block.ObjectPointer) (bool, error) {
return true, nil
}
func (a *Adapter) GetRange(_ context.Context, _ block.ObjectPointer, startPosition int64, endPosition int64) (io.ReadCloser, error) {
n := endPosition - startPosition
if n < 0 {
return nil, io.ErrUnexpectedEOF
}
reader := &io.LimitedReader{
R: rand.Reader,
N: n,
}
return io.NopCloser(reader), nil
}
func (a *Adapter) GetProperties(_ context.Context, _ block.ObjectPointer) (block.Properties, error) {
return block.Properties{}, nil
}
func (a *Adapter) Remove(_ context.Context, _ block.ObjectPointer) error {
return nil
}
func (a *Adapter) Copy(_ context.Context, _, _ block.ObjectPointer) error {
return nil
}
func (a *Adapter) UploadCopyPart(_ context.Context, _, _ block.ObjectPointer, _ string, _ int) (*block.UploadPartResponse, error) {
h := sha256.New()
code := h.Sum(nil)
etag := hex.EncodeToString(code)
return &block.UploadPartResponse{
ETag: etag,
}, nil
}
func (a *Adapter) UploadCopyPartRange(_ context.Context, _, _ block.ObjectPointer, _ string, _ int, startPosition, endPosition int64) (*block.UploadPartResponse, error) {
n := endPosition - startPosition
if n < 0 {
return nil, io.ErrUnexpectedEOF
}
h := sha256.New()
code := h.Sum(nil)
etag := hex.EncodeToString(code)
return &block.UploadPartResponse{
ETag: etag,
}, nil
}
func (a *Adapter) CreateMultiPartUpload(_ context.Context, _ block.ObjectPointer, _ *http.Request, _ block.CreateMultiPartUploadOpts) (*block.CreateMultiPartUploadResponse, error) {
uid := uuid.New()
uploadID := hex.EncodeToString(uid[:])
return &block.CreateMultiPartUploadResponse{
UploadID: uploadID,
}, nil
}
func (a *Adapter) UploadPart(_ context.Context, _ block.ObjectPointer, _ int64, reader io.Reader, _ string, _ int) (*block.UploadPartResponse, error) {
data, err := io.ReadAll(reader)
if err != nil {
return nil, err
}
h := sha256.New()
_, err = h.Write(data)
if err != nil {
return nil, err
}
code := h.Sum(nil)
etag := hex.EncodeToString(code)
return &block.UploadPartResponse{
ETag: etag,
}, nil
}
func (a *Adapter) AbortMultiPartUpload(context.Context, block.ObjectPointer, string) error {
return nil
}
func (a *Adapter) CompleteMultiPartUpload(context.Context, block.ObjectPointer, string, *block.MultipartUploadCompletion) (*block.CompleteMultiPartUploadResponse, error) {
const dataSize = 1024
data := make([]byte, dataSize)
if _, err := rand.Read(data); err != nil {
return nil, err
}
h := sha256.New()
_, err := h.Write(data)
if err != nil {
return nil, err
}
code := h.Sum(nil)
codeHex := hex.EncodeToString(code)
return &block.CompleteMultiPartUploadResponse{
ETag: codeHex,
ContentLength: dataSize,
}, nil
}
func (a *Adapter) BlockstoreType() string {
return block.BlockstoreTypeTransient
}
func (a *Adapter) GetStorageNamespaceInfo() block.StorageNamespaceInfo {
info := block.DefaultStorageNamespaceInfo(block.BlockstoreTypeTransient)
info.PreSignSupport = false
info.PreSignSupportUI = false
info.ImportSupport = false
return info
}
func (a *Adapter) ResolveNamespace(storageNamespace, key string, identifierType block.IdentifierType) (block.QualifiedKey, error) {
return block.DefaultResolveNamespace(storageNamespace, key, identifierType)
}
func (a *Adapter) RuntimeStats() map[string]string {
return nil
}
func (a *Adapter) GetPresignUploadPartURL(ctx context.Context, obj block.ObjectPointer, uploadID string, partNumber int) (string, error) {
return "", block.ErrOperationNotSupported
}